From 87d6f6c7c90bf7b427397d6bd2e2c70d610678e3 Mon Sep 17 00:00:00 2001 From: Son Ho Date: Tue, 4 Jul 2023 14:57:51 +0200 Subject: Reorganize the Lean tests --- tests/lean/.gitignore | 4 +- tests/lean/BetreeMain.lean | 1 + tests/lean/BetreeMain/Funs.lean | 1071 ++++++++++++++++++++ tests/lean/BetreeMain/Opaque.lean | 31 + tests/lean/BetreeMain/Types.lean | 62 ++ tests/lean/Constants.lean | 132 +++ tests/lean/External.lean | 1 + tests/lean/External/Funs.lean | 88 ++ tests/lean/External/Opaque.lean | 28 + tests/lean/External/Types.lean | 11 + tests/lean/Hashmap.lean | 1 + tests/lean/Hashmap/Funs.lean | 474 +++++++++ tests/lean/Hashmap/Types.lean | 17 + tests/lean/HashmapMain.lean | 1 + tests/lean/HashmapMain/Funs.lean | 557 ++++++++++ tests/lean/HashmapMain/Opaque.lean | 16 + tests/lean/HashmapMain/Types.lean | 20 + tests/lean/Loops.lean | 1 + tests/lean/Loops/Funs.lean | 624 ++++++++++++ tests/lean/Loops/Types.lean | 10 + tests/lean/Makefile | 31 +- tests/lean/NoNestedBorrows.lean | 541 ++++++++++ tests/lean/Paper.lean | 125 +++ tests/lean/PoloniusList.lean | 33 + tests/lean/Tests.lean | 9 + tests/lean/hashmap/Base/Primitives.lean | 583 ----------- tests/lean/hashmap/Hashmap.lean | 1 - tests/lean/hashmap/Hashmap/Clauses/Clauses.lean | 107 -- tests/lean/hashmap/Hashmap/Clauses/Template.lean | 108 -- tests/lean/hashmap/Hashmap/Funs.lean | 513 ---------- tests/lean/hashmap/Hashmap/Types.lean | 16 - tests/lean/hashmap/lake-manifest.json | 27 - tests/lean/hashmap/lakefile.lean | 12 - tests/lean/hashmap/lean-toolchain | 1 - tests/lean/hashmap_on_disk/.gitignore | 5 - tests/lean/hashmap_on_disk/Base/Primitives.lean | 583 ----------- tests/lean/hashmap_on_disk/HashmapMain.lean | 1 - .../HashmapMain/Clauses/Clauses.lean | 110 -- .../HashmapMain/Clauses/Template.lean | 112 -- .../hashmap_on_disk/HashmapMain/ExternalFuns.lean | 5 - tests/lean/hashmap_on_disk/HashmapMain/Funs.lean | 590 ----------- tests/lean/hashmap_on_disk/HashmapMain/Opaque.lean | 15 - tests/lean/hashmap_on_disk/HashmapMain/Types.lean | 19 - tests/lean/hashmap_on_disk/lake-manifest.json | 27 - tests/lean/hashmap_on_disk/lakefile.lean | 12 - tests/lean/hashmap_on_disk/lean-toolchain | 1 - tests/lean/lake-manifest.json | 34 + tests/lean/lakefile.lean | 14 + tests/lean/lean-toolchain | 2 +- tests/lean/misc-constants/Base/Primitives.lean | 583 ----------- tests/lean/misc-constants/Constants.lean | 131 --- tests/lean/misc-constants/lake-manifest.json | 27 - tests/lean/misc-constants/lakefile.lean | 12 - tests/lean/misc-constants/lean-toolchain | 1 - tests/lean/misc-external/Base/Primitives.lean | 583 ----------- tests/lean/misc-external/External.lean | 1 - .../lean/misc-external/External/ExternalFuns.lean | 5 - tests/lean/misc-external/External/Funs.lean | 84 -- tests/lean/misc-external/External/Opaque.lean | 27 - tests/lean/misc-external/External/Types.lean | 10 - tests/lean/misc-external/lake-manifest.json | 27 - tests/lean/misc-external/lakefile.lean | 12 - tests/lean/misc-external/lean-toolchain | 1 - tests/lean/misc-loops/Base/Primitives.lean | 583 ----------- tests/lean/misc-loops/Loops.lean | 1 - tests/lean/misc-loops/Loops/Clauses/Clauses.lean | 205 ---- tests/lean/misc-loops/Loops/Clauses/Template.lean | 205 ---- tests/lean/misc-loops/Loops/Funs.lean | 705 ------------- tests/lean/misc-loops/Loops/Types.lean | 9 - tests/lean/misc-loops/lake-manifest.json | 27 - tests/lean/misc-loops/lakefile.lean | 12 - tests/lean/misc-loops/lean-toolchain | 1 - .../misc-no_nested_borrows/Base/Primitives.lean | 583 ----------- .../misc-no_nested_borrows/NoNestedBorrows.lean | 538 ---------- .../lean/misc-no_nested_borrows/lake-manifest.json | 27 - tests/lean/misc-no_nested_borrows/lakefile.lean | 12 - tests/lean/misc-no_nested_borrows/lean-toolchain | 1 - tests/lean/misc-paper/Base/Primitives.lean | 583 ----------- tests/lean/misc-paper/Paper.lean | 123 --- tests/lean/misc-paper/lake-manifest.json | 27 - tests/lean/misc-paper/lakefile.lean | 12 - tests/lean/misc-paper/lean-toolchain | 1 - tests/lean/misc-polonius_list/Base/Primitives.lean | 583 ----------- tests/lean/misc-polonius_list/PoloniusList.lean | 31 - tests/lean/misc-polonius_list/lake-manifest.json | 27 - tests/lean/misc-polonius_list/lakefile.lean | 12 - tests/lean/misc-polonius_list/lean-toolchain | 1 - 87 files changed, 3918 insertions(+), 8682 deletions(-) create mode 100644 tests/lean/BetreeMain.lean create mode 100644 tests/lean/BetreeMain/Funs.lean create mode 100644 tests/lean/BetreeMain/Opaque.lean create mode 100644 tests/lean/BetreeMain/Types.lean create mode 100644 tests/lean/Constants.lean create mode 100644 tests/lean/External.lean create mode 100644 tests/lean/External/Funs.lean create mode 100644 tests/lean/External/Opaque.lean create mode 100644 tests/lean/External/Types.lean create mode 100644 tests/lean/Hashmap.lean create mode 100644 tests/lean/Hashmap/Funs.lean create mode 100644 tests/lean/Hashmap/Types.lean create mode 100644 tests/lean/HashmapMain.lean create mode 100644 tests/lean/HashmapMain/Funs.lean create mode 100644 tests/lean/HashmapMain/Opaque.lean create mode 100644 tests/lean/HashmapMain/Types.lean create mode 100644 tests/lean/Loops.lean create mode 100644 tests/lean/Loops/Funs.lean create mode 100644 tests/lean/Loops/Types.lean create mode 100644 tests/lean/NoNestedBorrows.lean create mode 100644 tests/lean/Paper.lean create mode 100644 tests/lean/PoloniusList.lean create mode 100644 tests/lean/Tests.lean delete mode 100644 tests/lean/hashmap/Base/Primitives.lean delete mode 100644 tests/lean/hashmap/Hashmap.lean delete mode 100644 tests/lean/hashmap/Hashmap/Clauses/Clauses.lean delete mode 100644 tests/lean/hashmap/Hashmap/Clauses/Template.lean delete mode 100644 tests/lean/hashmap/Hashmap/Funs.lean delete mode 100644 tests/lean/hashmap/Hashmap/Types.lean delete mode 100644 tests/lean/hashmap/lake-manifest.json delete mode 100644 tests/lean/hashmap/lakefile.lean delete mode 100644 tests/lean/hashmap/lean-toolchain delete mode 100644 tests/lean/hashmap_on_disk/.gitignore delete mode 100644 tests/lean/hashmap_on_disk/Base/Primitives.lean delete mode 100644 tests/lean/hashmap_on_disk/HashmapMain.lean delete mode 100644 tests/lean/hashmap_on_disk/HashmapMain/Clauses/Clauses.lean delete mode 100644 tests/lean/hashmap_on_disk/HashmapMain/Clauses/Template.lean delete mode 100644 tests/lean/hashmap_on_disk/HashmapMain/ExternalFuns.lean delete mode 100644 tests/lean/hashmap_on_disk/HashmapMain/Funs.lean delete mode 100644 tests/lean/hashmap_on_disk/HashmapMain/Opaque.lean delete mode 100644 tests/lean/hashmap_on_disk/HashmapMain/Types.lean delete mode 100644 tests/lean/hashmap_on_disk/lake-manifest.json delete mode 100644 tests/lean/hashmap_on_disk/lakefile.lean delete mode 100644 tests/lean/hashmap_on_disk/lean-toolchain create mode 100644 tests/lean/lake-manifest.json create mode 100644 tests/lean/lakefile.lean delete mode 100644 tests/lean/misc-constants/Base/Primitives.lean delete mode 100644 tests/lean/misc-constants/Constants.lean delete mode 100644 tests/lean/misc-constants/lake-manifest.json delete mode 100644 tests/lean/misc-constants/lakefile.lean delete mode 100644 tests/lean/misc-constants/lean-toolchain delete mode 100644 tests/lean/misc-external/Base/Primitives.lean delete mode 100644 tests/lean/misc-external/External.lean delete mode 100644 tests/lean/misc-external/External/ExternalFuns.lean delete mode 100644 tests/lean/misc-external/External/Funs.lean delete mode 100644 tests/lean/misc-external/External/Opaque.lean delete mode 100644 tests/lean/misc-external/External/Types.lean delete mode 100644 tests/lean/misc-external/lake-manifest.json delete mode 100644 tests/lean/misc-external/lakefile.lean delete mode 100644 tests/lean/misc-external/lean-toolchain delete mode 100644 tests/lean/misc-loops/Base/Primitives.lean delete mode 100644 tests/lean/misc-loops/Loops.lean delete mode 100644 tests/lean/misc-loops/Loops/Clauses/Clauses.lean delete mode 100644 tests/lean/misc-loops/Loops/Clauses/Template.lean delete mode 100644 tests/lean/misc-loops/Loops/Funs.lean delete mode 100644 tests/lean/misc-loops/Loops/Types.lean delete mode 100644 tests/lean/misc-loops/lake-manifest.json delete mode 100644 tests/lean/misc-loops/lakefile.lean delete mode 100644 tests/lean/misc-loops/lean-toolchain delete mode 100644 tests/lean/misc-no_nested_borrows/Base/Primitives.lean delete mode 100644 tests/lean/misc-no_nested_borrows/NoNestedBorrows.lean delete mode 100644 tests/lean/misc-no_nested_borrows/lake-manifest.json delete mode 100644 tests/lean/misc-no_nested_borrows/lakefile.lean delete mode 100644 tests/lean/misc-no_nested_borrows/lean-toolchain delete mode 100644 tests/lean/misc-paper/Base/Primitives.lean delete mode 100644 tests/lean/misc-paper/Paper.lean delete mode 100644 tests/lean/misc-paper/lake-manifest.json delete mode 100644 tests/lean/misc-paper/lakefile.lean delete mode 100644 tests/lean/misc-paper/lean-toolchain delete mode 100644 tests/lean/misc-polonius_list/Base/Primitives.lean delete mode 100644 tests/lean/misc-polonius_list/PoloniusList.lean delete mode 100644 tests/lean/misc-polonius_list/lake-manifest.json delete mode 100644 tests/lean/misc-polonius_list/lakefile.lean delete mode 100644 tests/lean/misc-polonius_list/lean-toolchain (limited to 'tests/lean') diff --git a/tests/lean/.gitignore b/tests/lean/.gitignore index e74f9899..4d1c5853 100644 --- a/tests/lean/.gitignore +++ b/tests/lean/.gitignore @@ -1,2 +1,2 @@ -*/lake-packages/ -*/build/ \ No newline at end of file +lake-packages +build \ No newline at end of file diff --git a/tests/lean/BetreeMain.lean b/tests/lean/BetreeMain.lean new file mode 100644 index 00000000..5f307877 --- /dev/null +++ b/tests/lean/BetreeMain.lean @@ -0,0 +1 @@ +import BetreeMain.Funs diff --git a/tests/lean/BetreeMain/Funs.lean b/tests/lean/BetreeMain/Funs.lean new file mode 100644 index 00000000..fb48b3a6 --- /dev/null +++ b/tests/lean/BetreeMain/Funs.lean @@ -0,0 +1,1071 @@ +-- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS +-- [betree_main]: function definitions +import Base +import BetreeMain.Types +import BetreeMain.ExternalFuns +open Primitives + +/- [betree_main::betree::load_internal_node] -/ +def betree_load_internal_node_fwd + (id : U64) (st : State) : + Result (State × (betree_list_t (U64 × betree_message_t))) + := + opaque_defs.betree_utils_load_internal_node_fwd id st + +/- [betree_main::betree::store_internal_node] -/ +def betree_store_internal_node_fwd + (id : U64) (content : betree_list_t (U64 × betree_message_t)) (st : State) : + Result (State × Unit) + := + do + let (st0, _) ← + opaque_defs.betree_utils_store_internal_node_fwd id content st + Result.ret (st0, ()) + +/- [betree_main::betree::load_leaf_node] -/ +def betree_load_leaf_node_fwd + (id : U64) (st : State) : Result (State × (betree_list_t (U64 × U64))) := + opaque_defs.betree_utils_load_leaf_node_fwd id st + +/- [betree_main::betree::store_leaf_node] -/ +def betree_store_leaf_node_fwd + (id : U64) (content : betree_list_t (U64 × U64)) (st : State) : + Result (State × Unit) + := + do + let (st0, _) ← opaque_defs.betree_utils_store_leaf_node_fwd id content st + Result.ret (st0, ()) + +/- [betree_main::betree::fresh_node_id] -/ +def betree_fresh_node_id_fwd (counter : U64) : Result U64 := + do + let _ ← counter + (U64.ofInt 1 (by intlit)) + Result.ret counter + +/- [betree_main::betree::fresh_node_id] -/ +def betree_fresh_node_id_back (counter : U64) : Result U64 := + counter + (U64.ofInt 1 (by intlit)) + +/- [betree_main::betree::NodeIdCounter::{0}::new] -/ +def betree_node_id_counter_new_fwd : Result betree_node_id_counter_t := + Result.ret + { betree_node_id_counter_next_node_id := (U64.ofInt 0 (by intlit)) } + +/- [betree_main::betree::NodeIdCounter::{0}::fresh_id] -/ +def betree_node_id_counter_fresh_id_fwd + (self : betree_node_id_counter_t) : Result U64 := + do + let _ ← self.betree_node_id_counter_next_node_id + + (U64.ofInt 1 (by intlit)) + Result.ret self.betree_node_id_counter_next_node_id + +/- [betree_main::betree::NodeIdCounter::{0}::fresh_id] -/ +def betree_node_id_counter_fresh_id_back + (self : betree_node_id_counter_t) : Result betree_node_id_counter_t := + do + let i ← self.betree_node_id_counter_next_node_id + + (U64.ofInt 1 (by intlit)) + Result.ret { betree_node_id_counter_next_node_id := i } + +/- [core::num::u64::{10}::MAX] -/ +def core_num_u64_max_body : Result U64 := + Result.ret (U64.ofInt 18446744073709551615 (by intlit)) +def core_num_u64_max_c : U64 := eval_global core_num_u64_max_body (by simp) + +/- [betree_main::betree::upsert_update] -/ +def betree_upsert_update_fwd + (prev : Option U64) (st : betree_upsert_fun_state_t) : Result U64 := + match h: prev with + | Option.none => + match h: st with + | betree_upsert_fun_state_t.Add v => Result.ret v + | betree_upsert_fun_state_t.Sub i => Result.ret (U64.ofInt 0 (by intlit)) + | Option.some prev0 => + match h: st with + | betree_upsert_fun_state_t.Add v => + do + let margin ← core_num_u64_max_c - prev0 + if margin >= v + then prev0 + v + else Result.ret core_num_u64_max_c + | betree_upsert_fun_state_t.Sub v => + if prev0 >= v + then prev0 - v + else Result.ret (U64.ofInt 0 (by intlit)) + +/- [betree_main::betree::List::{1}::len] -/ +divergent def betree_list_len_fwd + (T : Type) (self : betree_list_t T) : Result U64 := + match h: self with + | betree_list_t.Cons t tl => + do + let i ← betree_list_len_fwd T tl + (U64.ofInt 1 (by intlit)) + i + | betree_list_t.Nil => Result.ret (U64.ofInt 0 (by intlit)) + +/- [betree_main::betree::List::{1}::split_at] -/ +divergent def betree_list_split_at_fwd + (T : Type) (self : betree_list_t T) (n : U64) : + Result ((betree_list_t T) × (betree_list_t T)) + := + if n = (U64.ofInt 0 (by intlit)) + then Result.ret (betree_list_t.Nil, self) + else + match h: self with + | betree_list_t.Cons hd tl => + do + let i ← n - (U64.ofInt 1 (by intlit)) + let p ← betree_list_split_at_fwd T tl i + let (ls0, ls1) := p + let l := ls0 + Result.ret (betree_list_t.Cons hd l, ls1) + | betree_list_t.Nil => Result.fail Error.panic + +/- [betree_main::betree::List::{1}::push_front] -/ +def betree_list_push_front_fwd_back + (T : Type) (self : betree_list_t T) (x : T) : Result (betree_list_t T) := + let tl := mem_replace_fwd (betree_list_t T) self betree_list_t.Nil + let l := tl + Result.ret (betree_list_t.Cons x l) + +/- [betree_main::betree::List::{1}::pop_front] -/ +def betree_list_pop_front_fwd (T : Type) (self : betree_list_t T) : Result T := + let ls := mem_replace_fwd (betree_list_t T) self betree_list_t.Nil + match h: ls with + | betree_list_t.Cons x tl => Result.ret x + | betree_list_t.Nil => Result.fail Error.panic + +/- [betree_main::betree::List::{1}::pop_front] -/ +def betree_list_pop_front_back + (T : Type) (self : betree_list_t T) : Result (betree_list_t T) := + let ls := mem_replace_fwd (betree_list_t T) self betree_list_t.Nil + match h: ls with + | betree_list_t.Cons x tl => Result.ret tl + | betree_list_t.Nil => Result.fail Error.panic + +/- [betree_main::betree::List::{1}::hd] -/ +def betree_list_hd_fwd (T : Type) (self : betree_list_t T) : Result T := + match h: self with + | betree_list_t.Cons hd l => Result.ret hd + | betree_list_t.Nil => Result.fail Error.panic + +/- [betree_main::betree::List::{2}::head_has_key] -/ +def betree_list_head_has_key_fwd + (T : Type) (self : betree_list_t (U64 × T)) (key : U64) : Result Bool := + match h: self with + | betree_list_t.Cons hd l => let (i, _) := hd + Result.ret (i = key) + | betree_list_t.Nil => Result.ret false + +/- [betree_main::betree::List::{2}::partition_at_pivot] -/ +divergent def betree_list_partition_at_pivot_fwd + (T : Type) (self : betree_list_t (U64 × T)) (pivot : U64) : + Result ((betree_list_t (U64 × T)) × (betree_list_t (U64 × T))) + := + match h: self with + | betree_list_t.Cons hd tl => + let (i, t) := hd + if i >= pivot + then Result.ret (betree_list_t.Nil, betree_list_t.Cons (i, t) tl) + else + do + let p ← betree_list_partition_at_pivot_fwd T tl pivot + let (ls0, ls1) := p + let l := ls0 + Result.ret (betree_list_t.Cons (i, t) l, ls1) + | betree_list_t.Nil => Result.ret (betree_list_t.Nil, betree_list_t.Nil) + +/- [betree_main::betree::Leaf::{3}::split] -/ +def betree_leaf_split_fwd + (self : betree_leaf_t) (content : betree_list_t (U64 × U64)) + (params : betree_params_t) (node_id_cnt : betree_node_id_counter_t) + (st : State) : + Result (State × betree_internal_t) + := + do + let p ← + betree_list_split_at_fwd (U64 × U64) content + params.betree_params_split_size + let (content0, content1) := p + let p0 ← betree_list_hd_fwd (U64 × U64) content1 + let (pivot, _) := p0 + let id0 ← betree_node_id_counter_fresh_id_fwd node_id_cnt + let node_id_cnt0 ← betree_node_id_counter_fresh_id_back node_id_cnt + let id1 ← betree_node_id_counter_fresh_id_fwd node_id_cnt0 + let (st0, _) ← betree_store_leaf_node_fwd id0 content0 st + let (st1, _) ← betree_store_leaf_node_fwd id1 content1 st0 + let n := betree_node_t.Leaf + { + betree_leaf_id := id0, + betree_leaf_size := params.betree_params_split_size + } + let n0 := betree_node_t.Leaf + { + betree_leaf_id := id1, + betree_leaf_size := params.betree_params_split_size + } + Result.ret (st1, mkbetree_internal_t self.betree_leaf_id pivot n n0) + +/- [betree_main::betree::Leaf::{3}::split] -/ +def betree_leaf_split_back + (self : betree_leaf_t) (content : betree_list_t (U64 × U64)) + (params : betree_params_t) (node_id_cnt : betree_node_id_counter_t) + (st : State) : + Result betree_node_id_counter_t + := + do + let p ← + betree_list_split_at_fwd (U64 × U64) content + params.betree_params_split_size + let (content0, content1) := p + let _ ← betree_list_hd_fwd (U64 × U64) content1 + let id0 ← betree_node_id_counter_fresh_id_fwd node_id_cnt + let node_id_cnt0 ← betree_node_id_counter_fresh_id_back node_id_cnt + let id1 ← betree_node_id_counter_fresh_id_fwd node_id_cnt0 + let (st0, _) ← betree_store_leaf_node_fwd id0 content0 st + let _ ← betree_store_leaf_node_fwd id1 content1 st0 + betree_node_id_counter_fresh_id_back node_id_cnt0 + +/- [betree_main::betree::Node::{5}::lookup_in_bindings] -/ +divergent def betree_node_lookup_in_bindings_fwd + (key : U64) (bindings : betree_list_t (U64 × U64)) : Result (Option U64) := + match h: bindings with + | betree_list_t.Cons hd tl => + let (i, i0) := hd + if i = key + then Result.ret (Option.some i0) + else + if i > key + then Result.ret Option.none + else betree_node_lookup_in_bindings_fwd key tl + | betree_list_t.Nil => Result.ret Option.none + +/- [betree_main::betree::Node::{5}::lookup_first_message_for_key] -/ +divergent def betree_node_lookup_first_message_for_key_fwd + (key : U64) (msgs : betree_list_t (U64 × betree_message_t)) : + Result (betree_list_t (U64 × betree_message_t)) + := + match h: msgs with + | betree_list_t.Cons x next_msgs => + let (i, m) := x + if i >= key + then Result.ret (betree_list_t.Cons (i, m) next_msgs) + else betree_node_lookup_first_message_for_key_fwd key next_msgs + | betree_list_t.Nil => Result.ret betree_list_t.Nil + +/- [betree_main::betree::Node::{5}::lookup_first_message_for_key] -/ +divergent def betree_node_lookup_first_message_for_key_back + (key : U64) (msgs : betree_list_t (U64 × betree_message_t)) + (ret0 : betree_list_t (U64 × betree_message_t)) : + Result (betree_list_t (U64 × betree_message_t)) + := + match h: msgs with + | betree_list_t.Cons x next_msgs => + let (i, m) := x + if i >= key + then Result.ret ret0 + else + do + let next_msgs0 ← + betree_node_lookup_first_message_for_key_back key next_msgs ret0 + Result.ret (betree_list_t.Cons (i, m) next_msgs0) + | betree_list_t.Nil => Result.ret ret0 + +/- [betree_main::betree::Node::{5}::apply_upserts] -/ +divergent def betree_node_apply_upserts_fwd + (msgs : betree_list_t (U64 × betree_message_t)) (prev : Option U64) + (key : U64) (st : State) : + Result (State × U64) + := + do + let b ← betree_list_head_has_key_fwd betree_message_t msgs key + if b + then + do + let msg ← betree_list_pop_front_fwd (U64 × betree_message_t) msgs + let (_, m) := msg + match h: m with + | betree_message_t.Insert i => Result.fail Error.panic + | betree_message_t.Delete => Result.fail Error.panic + | betree_message_t.Upsert s => + do + let v ← betree_upsert_update_fwd prev s + let msgs0 ← + betree_list_pop_front_back (U64 × betree_message_t) msgs + betree_node_apply_upserts_fwd msgs0 (Option.some v) key st + else + do + let (st0, v) ← opaque_defs.core_option_option_unwrap_fwd U64 prev st + let _ ← + betree_list_push_front_fwd_back (U64 × betree_message_t) msgs (key, + betree_message_t.Insert v) + Result.ret (st0, v) + +/- [betree_main::betree::Node::{5}::apply_upserts] -/ +divergent def betree_node_apply_upserts_back + (msgs : betree_list_t (U64 × betree_message_t)) (prev : Option U64) + (key : U64) (st : State) : + Result (betree_list_t (U64 × betree_message_t)) + := + do + let b ← betree_list_head_has_key_fwd betree_message_t msgs key + if b + then + do + let msg ← betree_list_pop_front_fwd (U64 × betree_message_t) msgs + let (_, m) := msg + match h: m with + | betree_message_t.Insert i => Result.fail Error.panic + | betree_message_t.Delete => Result.fail Error.panic + | betree_message_t.Upsert s => + do + let v ← betree_upsert_update_fwd prev s + let msgs0 ← + betree_list_pop_front_back (U64 × betree_message_t) msgs + betree_node_apply_upserts_back msgs0 (Option.some v) key st + else + do + let (_, v) ← opaque_defs.core_option_option_unwrap_fwd U64 prev st + betree_list_push_front_fwd_back (U64 × betree_message_t) msgs (key, + betree_message_t.Insert v) + +/- [betree_main::betree::Node::{5}::lookup] -/ +mutual divergent def betree_node_lookup_fwd + (self : betree_node_t) (key : U64) (st : State) : + Result (State × (Option U64)) + := + match h: self with + | betree_node_t.Internal node => + do + let (mkbetree_internal_t i i0 n n0) := node + let (st0, msgs) ← betree_load_internal_node_fwd i st + let pending ← betree_node_lookup_first_message_for_key_fwd key msgs + match h: pending with + | betree_list_t.Cons p l => + let (k, msg) := p + if k != key + then + do + let (st1, opt) ← + betree_internal_lookup_in_children_fwd (mkbetree_internal_t i i0 + n n0) key st0 + let _ ← + betree_node_lookup_first_message_for_key_back key msgs + (betree_list_t.Cons (k, msg) l) + Result.ret (st1, opt) + else + match h: msg with + | betree_message_t.Insert v => + do + let _ ← + betree_node_lookup_first_message_for_key_back key msgs + (betree_list_t.Cons (k, betree_message_t.Insert v) l) + Result.ret (st0, Option.some v) + | betree_message_t.Delete => + do + let _ ← + betree_node_lookup_first_message_for_key_back key msgs + (betree_list_t.Cons (k, betree_message_t.Delete) l) + Result.ret (st0, Option.none) + | betree_message_t.Upsert ufs => + do + let (st1, v) ← + betree_internal_lookup_in_children_fwd (mkbetree_internal_t i + i0 n n0) key st0 + let (st2, v0) ← + betree_node_apply_upserts_fwd (betree_list_t.Cons (k, + betree_message_t.Upsert ufs) l) v key st1 + let node0 ← + betree_internal_lookup_in_children_back (mkbetree_internal_t i + i0 n n0) key st0 + let (mkbetree_internal_t i1 _ _ _) := node0 + let pending0 ← + betree_node_apply_upserts_back (betree_list_t.Cons (k, + betree_message_t.Upsert ufs) l) v key st1 + let msgs0 ← + betree_node_lookup_first_message_for_key_back key msgs pending0 + let (st3, _) ← betree_store_internal_node_fwd i1 msgs0 st2 + Result.ret (st3, Option.some v0) + | betree_list_t.Nil => + do + let (st1, opt) ← + betree_internal_lookup_in_children_fwd (mkbetree_internal_t i i0 n + n0) key st0 + let _ ← + betree_node_lookup_first_message_for_key_back key msgs + betree_list_t.Nil + Result.ret (st1, opt) + | betree_node_t.Leaf node => + do + let (st0, bindings) ← betree_load_leaf_node_fwd node.betree_leaf_id st + let opt ← betree_node_lookup_in_bindings_fwd key bindings + Result.ret (st0, opt) + +/- [betree_main::betree::Node::{5}::lookup] -/ +divergent def betree_node_lookup_back + (self : betree_node_t) (key : U64) (st : State) : Result betree_node_t := + match h: self with + | betree_node_t.Internal node => + do + let (mkbetree_internal_t i i0 n n0) := node + let (st0, msgs) ← betree_load_internal_node_fwd i st + let pending ← betree_node_lookup_first_message_for_key_fwd key msgs + match h: pending with + | betree_list_t.Cons p l => + let (k, msg) := p + if k != key + then + do + let _ ← + betree_node_lookup_first_message_for_key_back key msgs + (betree_list_t.Cons (k, msg) l) + let node0 ← + betree_internal_lookup_in_children_back (mkbetree_internal_t i i0 + n n0) key st0 + Result.ret (betree_node_t.Internal node0) + else + match h: msg with + | betree_message_t.Insert v => + do + let _ ← + betree_node_lookup_first_message_for_key_back key msgs + (betree_list_t.Cons (k, betree_message_t.Insert v) l) + Result.ret (betree_node_t.Internal (mkbetree_internal_t i i0 n + n0)) + | betree_message_t.Delete => + do + let _ ← + betree_node_lookup_first_message_for_key_back key msgs + (betree_list_t.Cons (k, betree_message_t.Delete) l) + Result.ret (betree_node_t.Internal (mkbetree_internal_t i i0 n + n0)) + | betree_message_t.Upsert ufs => + do + let (st1, v) ← + betree_internal_lookup_in_children_fwd (mkbetree_internal_t i + i0 n n0) key st0 + let (st2, _) ← + betree_node_apply_upserts_fwd (betree_list_t.Cons (k, + betree_message_t.Upsert ufs) l) v key st1 + let node0 ← + betree_internal_lookup_in_children_back (mkbetree_internal_t i + i0 n n0) key st0 + let (mkbetree_internal_t i1 i2 n1 n2) := node0 + let pending0 ← + betree_node_apply_upserts_back (betree_list_t.Cons (k, + betree_message_t.Upsert ufs) l) v key st1 + let msgs0 ← + betree_node_lookup_first_message_for_key_back key msgs pending0 + let _ ← betree_store_internal_node_fwd i1 msgs0 st2 + Result.ret (betree_node_t.Internal (mkbetree_internal_t i1 i2 n1 + n2)) + | betree_list_t.Nil => + do + let _ ← + betree_node_lookup_first_message_for_key_back key msgs + betree_list_t.Nil + let node0 ← + betree_internal_lookup_in_children_back (mkbetree_internal_t i i0 n + n0) key st0 + Result.ret (betree_node_t.Internal node0) + | betree_node_t.Leaf node => + do + let (_, bindings) ← betree_load_leaf_node_fwd node.betree_leaf_id st + let _ ← betree_node_lookup_in_bindings_fwd key bindings + Result.ret (betree_node_t.Leaf node) + +/- [betree_main::betree::Internal::{4}::lookup_in_children] -/ +divergent def betree_internal_lookup_in_children_fwd + (self : betree_internal_t) (key : U64) (st : State) : + Result (State × (Option U64)) + := + let (mkbetree_internal_t _ i n n0) := self + if key < i + then betree_node_lookup_fwd n key st + else betree_node_lookup_fwd n0 key st + +/- [betree_main::betree::Internal::{4}::lookup_in_children] -/ +divergent def betree_internal_lookup_in_children_back + (self : betree_internal_t) (key : U64) (st : State) : + Result betree_internal_t + := + let (mkbetree_internal_t i i0 n n0) := self + if key < i0 + then + do + let n1 ← betree_node_lookup_back n key st + Result.ret (mkbetree_internal_t i i0 n1 n0) + else + do + let n1 ← betree_node_lookup_back n0 key st + Result.ret (mkbetree_internal_t i i0 n n1) + +end + +/- [betree_main::betree::Node::{5}::lookup_mut_in_bindings] -/ +divergent def betree_node_lookup_mut_in_bindings_fwd + (key : U64) (bindings : betree_list_t (U64 × U64)) : + Result (betree_list_t (U64 × U64)) + := + match h: bindings with + | betree_list_t.Cons hd tl => + let (i, i0) := hd + if i >= key + then Result.ret (betree_list_t.Cons (i, i0) tl) + else betree_node_lookup_mut_in_bindings_fwd key tl + | betree_list_t.Nil => Result.ret betree_list_t.Nil + +/- [betree_main::betree::Node::{5}::lookup_mut_in_bindings] -/ +divergent def betree_node_lookup_mut_in_bindings_back + (key : U64) (bindings : betree_list_t (U64 × U64)) + (ret0 : betree_list_t (U64 × U64)) : + Result (betree_list_t (U64 × U64)) + := + match h: bindings with + | betree_list_t.Cons hd tl => + let (i, i0) := hd + if i >= key + then Result.ret ret0 + else + do + let tl0 ← betree_node_lookup_mut_in_bindings_back key tl ret0 + Result.ret (betree_list_t.Cons (i, i0) tl0) + | betree_list_t.Nil => Result.ret ret0 + +/- [betree_main::betree::Node::{5}::apply_to_leaf] -/ +def betree_node_apply_to_leaf_fwd_back + (bindings : betree_list_t (U64 × U64)) (key : U64) + (new_msg : betree_message_t) : + Result (betree_list_t (U64 × U64)) + := + do + let bindings0 ← betree_node_lookup_mut_in_bindings_fwd key bindings + let b ← betree_list_head_has_key_fwd U64 bindings0 key + if b + then + do + let hd ← betree_list_pop_front_fwd (U64 × U64) bindings0 + match h: new_msg with + | betree_message_t.Insert v => + do + let bindings1 ← betree_list_pop_front_back (U64 × U64) bindings0 + let bindings2 ← + betree_list_push_front_fwd_back (U64 × U64) bindings1 (key, v) + betree_node_lookup_mut_in_bindings_back key bindings bindings2 + | betree_message_t.Delete => + do + let bindings1 ← betree_list_pop_front_back (U64 × U64) bindings0 + betree_node_lookup_mut_in_bindings_back key bindings bindings1 + | betree_message_t.Upsert s => + do + let (_, i) := hd + let v ← betree_upsert_update_fwd (Option.some i) s + let bindings1 ← betree_list_pop_front_back (U64 × U64) bindings0 + let bindings2 ← + betree_list_push_front_fwd_back (U64 × U64) bindings1 (key, v) + betree_node_lookup_mut_in_bindings_back key bindings bindings2 + else + match h: new_msg with + | betree_message_t.Insert v => + do + let bindings1 ← + betree_list_push_front_fwd_back (U64 × U64) bindings0 (key, v) + betree_node_lookup_mut_in_bindings_back key bindings bindings1 + | betree_message_t.Delete => + betree_node_lookup_mut_in_bindings_back key bindings bindings0 + | betree_message_t.Upsert s => + do + let v ← betree_upsert_update_fwd Option.none s + let bindings1 ← + betree_list_push_front_fwd_back (U64 × U64) bindings0 (key, v) + betree_node_lookup_mut_in_bindings_back key bindings bindings1 + +/- [betree_main::betree::Node::{5}::apply_messages_to_leaf] -/ +divergent def betree_node_apply_messages_to_leaf_fwd_back + (bindings : betree_list_t (U64 × U64)) + (new_msgs : betree_list_t (U64 × betree_message_t)) : + Result (betree_list_t (U64 × U64)) + := + match h: new_msgs with + | betree_list_t.Cons new_msg new_msgs_tl => + do + let (i, m) := new_msg + let bindings0 ← betree_node_apply_to_leaf_fwd_back bindings i m + betree_node_apply_messages_to_leaf_fwd_back bindings0 new_msgs_tl + | betree_list_t.Nil => Result.ret bindings + +/- [betree_main::betree::Node::{5}::filter_messages_for_key] -/ +divergent def betree_node_filter_messages_for_key_fwd_back + (key : U64) (msgs : betree_list_t (U64 × betree_message_t)) : + Result (betree_list_t (U64 × betree_message_t)) + := + match h: msgs with + | betree_list_t.Cons p l => + let (k, m) := p + if k = key + then + do + let msgs0 ← + betree_list_pop_front_back (U64 × betree_message_t) + (betree_list_t.Cons (k, m) l) + betree_node_filter_messages_for_key_fwd_back key msgs0 + else Result.ret (betree_list_t.Cons (k, m) l) + | betree_list_t.Nil => Result.ret betree_list_t.Nil + +/- [betree_main::betree::Node::{5}::lookup_first_message_after_key] -/ +divergent def betree_node_lookup_first_message_after_key_fwd + (key : U64) (msgs : betree_list_t (U64 × betree_message_t)) : + Result (betree_list_t (U64 × betree_message_t)) + := + match h: msgs with + | betree_list_t.Cons p next_msgs => + let (k, m) := p + if k = key + then betree_node_lookup_first_message_after_key_fwd key next_msgs + else Result.ret (betree_list_t.Cons (k, m) next_msgs) + | betree_list_t.Nil => Result.ret betree_list_t.Nil + +/- [betree_main::betree::Node::{5}::lookup_first_message_after_key] -/ +divergent def betree_node_lookup_first_message_after_key_back + (key : U64) (msgs : betree_list_t (U64 × betree_message_t)) + (ret0 : betree_list_t (U64 × betree_message_t)) : + Result (betree_list_t (U64 × betree_message_t)) + := + match h: msgs with + | betree_list_t.Cons p next_msgs => + let (k, m) := p + if k = key + then + do + let next_msgs0 ← + betree_node_lookup_first_message_after_key_back key next_msgs ret0 + Result.ret (betree_list_t.Cons (k, m) next_msgs0) + else Result.ret ret0 + | betree_list_t.Nil => Result.ret ret0 + +/- [betree_main::betree::Node::{5}::apply_to_internal] -/ +def betree_node_apply_to_internal_fwd_back + (msgs : betree_list_t (U64 × betree_message_t)) (key : U64) + (new_msg : betree_message_t) : + Result (betree_list_t (U64 × betree_message_t)) + := + do + let msgs0 ← betree_node_lookup_first_message_for_key_fwd key msgs + let b ← betree_list_head_has_key_fwd betree_message_t msgs0 key + if b + then + match h: new_msg with + | betree_message_t.Insert i => + do + let msgs1 ← betree_node_filter_messages_for_key_fwd_back key msgs0 + let msgs2 ← + betree_list_push_front_fwd_back (U64 × betree_message_t) msgs1 + (key, betree_message_t.Insert i) + betree_node_lookup_first_message_for_key_back key msgs msgs2 + | betree_message_t.Delete => + do + let msgs1 ← betree_node_filter_messages_for_key_fwd_back key msgs0 + let msgs2 ← + betree_list_push_front_fwd_back (U64 × betree_message_t) msgs1 + (key, betree_message_t.Delete) + betree_node_lookup_first_message_for_key_back key msgs msgs2 + | betree_message_t.Upsert s => + do + let p ← betree_list_hd_fwd (U64 × betree_message_t) msgs0 + let (_, m) := p + match h: m with + | betree_message_t.Insert prev => + do + let v ← betree_upsert_update_fwd (Option.some prev) s + let msgs1 ← + betree_list_pop_front_back (U64 × betree_message_t) msgs0 + let msgs2 ← + betree_list_push_front_fwd_back (U64 × betree_message_t) msgs1 + (key, betree_message_t.Insert v) + betree_node_lookup_first_message_for_key_back key msgs msgs2 + | betree_message_t.Delete => + do + let v ← betree_upsert_update_fwd Option.none s + let msgs1 ← + betree_list_pop_front_back (U64 × betree_message_t) msgs0 + let msgs2 ← + betree_list_push_front_fwd_back (U64 × betree_message_t) msgs1 + (key, betree_message_t.Insert v) + betree_node_lookup_first_message_for_key_back key msgs msgs2 + | betree_message_t.Upsert ufs => + do + let msgs1 ← + betree_node_lookup_first_message_after_key_fwd key msgs0 + let msgs2 ← + betree_list_push_front_fwd_back (U64 × betree_message_t) msgs1 + (key, betree_message_t.Upsert s) + let msgs3 ← + betree_node_lookup_first_message_after_key_back key msgs0 msgs2 + betree_node_lookup_first_message_for_key_back key msgs msgs3 + else + do + let msgs1 ← + betree_list_push_front_fwd_back (U64 × betree_message_t) msgs0 (key, + new_msg) + betree_node_lookup_first_message_for_key_back key msgs msgs1 + +/- [betree_main::betree::Node::{5}::apply_messages_to_internal] -/ +divergent def betree_node_apply_messages_to_internal_fwd_back + (msgs : betree_list_t (U64 × betree_message_t)) + (new_msgs : betree_list_t (U64 × betree_message_t)) : + Result (betree_list_t (U64 × betree_message_t)) + := + match h: new_msgs with + | betree_list_t.Cons new_msg new_msgs_tl => + do + let (i, m) := new_msg + let msgs0 ← betree_node_apply_to_internal_fwd_back msgs i m + betree_node_apply_messages_to_internal_fwd_back msgs0 new_msgs_tl + | betree_list_t.Nil => Result.ret msgs + +/- [betree_main::betree::Node::{5}::apply_messages] -/ +mutual divergent def betree_node_apply_messages_fwd + (self : betree_node_t) (params : betree_params_t) + (node_id_cnt : betree_node_id_counter_t) + (msgs : betree_list_t (U64 × betree_message_t)) (st : State) : + Result (State × Unit) + := + match h: self with + | betree_node_t.Internal node => + do + let (mkbetree_internal_t i i0 n n0) := node + let (st0, content) ← betree_load_internal_node_fwd i st + let content0 ← + betree_node_apply_messages_to_internal_fwd_back content msgs + let num_msgs ← betree_list_len_fwd (U64 × betree_message_t) content0 + if num_msgs >= params.betree_params_min_flush_size + then + do + let (st1, content1) ← + betree_internal_flush_fwd (mkbetree_internal_t i i0 n n0) params + node_id_cnt content0 st0 + let (node0, _) ← + betree_internal_flush_back (mkbetree_internal_t i i0 n n0) params + node_id_cnt content0 st0 + let (mkbetree_internal_t i1 _ _ _) := node0 + let (st2, _) ← betree_store_internal_node_fwd i1 content1 st1 + Result.ret (st2, ()) + else + do + let (st1, _) ← betree_store_internal_node_fwd i content0 st0 + Result.ret (st1, ()) + | betree_node_t.Leaf node => + do + let (st0, content) ← betree_load_leaf_node_fwd node.betree_leaf_id st + let content0 ← betree_node_apply_messages_to_leaf_fwd_back content msgs + let len ← betree_list_len_fwd (U64 × U64) content0 + let i ← (U64.ofInt 2 (by intlit)) * params.betree_params_split_size + if len >= i + then + do + let (st1, _) ← + betree_leaf_split_fwd node content0 params node_id_cnt st0 + let (st2, _) ← + betree_store_leaf_node_fwd node.betree_leaf_id betree_list_t.Nil + st1 + Result.ret (st2, ()) + else + do + let (st1, _) ← + betree_store_leaf_node_fwd node.betree_leaf_id content0 st0 + Result.ret (st1, ()) + +/- [betree_main::betree::Node::{5}::apply_messages] -/ +divergent def betree_node_apply_messages_back + (self : betree_node_t) (params : betree_params_t) + (node_id_cnt : betree_node_id_counter_t) + (msgs : betree_list_t (U64 × betree_message_t)) (st : State) : + Result (betree_node_t × betree_node_id_counter_t) + := + match h: self with + | betree_node_t.Internal node => + do + let (mkbetree_internal_t i i0 n n0) := node + let (st0, content) ← betree_load_internal_node_fwd i st + let content0 ← + betree_node_apply_messages_to_internal_fwd_back content msgs + let num_msgs ← betree_list_len_fwd (U64 × betree_message_t) content0 + if num_msgs >= params.betree_params_min_flush_size + then + do + let (st1, content1) ← + betree_internal_flush_fwd (mkbetree_internal_t i i0 n n0) params + node_id_cnt content0 st0 + let (node0, node_id_cnt0) ← + betree_internal_flush_back (mkbetree_internal_t i i0 n n0) params + node_id_cnt content0 st0 + let (mkbetree_internal_t i1 i2 n1 n2) := node0 + let _ ← betree_store_internal_node_fwd i1 content1 st1 + Result.ret (betree_node_t.Internal (mkbetree_internal_t i1 i2 n1 n2), + node_id_cnt0) + else + do + let _ ← betree_store_internal_node_fwd i content0 st0 + Result.ret (betree_node_t.Internal (mkbetree_internal_t i i0 n n0), + node_id_cnt) + | betree_node_t.Leaf node => + do + let (st0, content) ← betree_load_leaf_node_fwd node.betree_leaf_id st + let content0 ← betree_node_apply_messages_to_leaf_fwd_back content msgs + let len ← betree_list_len_fwd (U64 × U64) content0 + let i ← (U64.ofInt 2 (by intlit)) * params.betree_params_split_size + if len >= i + then + do + let (st1, new_node) ← + betree_leaf_split_fwd node content0 params node_id_cnt st0 + let _ ← + betree_store_leaf_node_fwd node.betree_leaf_id betree_list_t.Nil + st1 + let node_id_cnt0 ← + betree_leaf_split_back node content0 params node_id_cnt st0 + Result.ret (betree_node_t.Internal new_node, node_id_cnt0) + else + do + let _ ← betree_store_leaf_node_fwd node.betree_leaf_id content0 st0 + Result.ret (betree_node_t.Leaf { node with betree_leaf_size := len }, + node_id_cnt) + +/- [betree_main::betree::Internal::{4}::flush] -/ +divergent def betree_internal_flush_fwd + (self : betree_internal_t) (params : betree_params_t) + (node_id_cnt : betree_node_id_counter_t) + (content : betree_list_t (U64 × betree_message_t)) (st : State) : + Result (State × (betree_list_t (U64 × betree_message_t))) + := + do + let (mkbetree_internal_t _ i n n0) := self + let p ← betree_list_partition_at_pivot_fwd betree_message_t content i + let (msgs_left, msgs_right) := p + let len_left ← betree_list_len_fwd (U64 × betree_message_t) msgs_left + if len_left >= params.betree_params_min_flush_size + then + do + let (st0, _) ← + betree_node_apply_messages_fwd n params node_id_cnt msgs_left st + let (_, node_id_cnt0) ← + betree_node_apply_messages_back n params node_id_cnt msgs_left st + let len_right ← + betree_list_len_fwd (U64 × betree_message_t) msgs_right + if len_right >= params.betree_params_min_flush_size + then + do + let (st1, _) ← + betree_node_apply_messages_fwd n0 params node_id_cnt0 msgs_right + st0 + let _ ← + betree_node_apply_messages_back n0 params node_id_cnt0 msgs_right + st0 + Result.ret (st1, betree_list_t.Nil) + else Result.ret (st0, msgs_right) + else + do + let (st0, _) ← + betree_node_apply_messages_fwd n0 params node_id_cnt msgs_right st + let _ ← + betree_node_apply_messages_back n0 params node_id_cnt msgs_right st + Result.ret (st0, msgs_left) + +/- [betree_main::betree::Internal::{4}::flush] -/ +divergent def betree_internal_flush_back + (self : betree_internal_t) (params : betree_params_t) + (node_id_cnt : betree_node_id_counter_t) + (content : betree_list_t (U64 × betree_message_t)) (st : State) : + Result (betree_internal_t × betree_node_id_counter_t) + := + do + let (mkbetree_internal_t i i0 n n0) := self + let p ← betree_list_partition_at_pivot_fwd betree_message_t content i0 + let (msgs_left, msgs_right) := p + let len_left ← betree_list_len_fwd (U64 × betree_message_t) msgs_left + if len_left >= params.betree_params_min_flush_size + then + do + let (st0, _) ← + betree_node_apply_messages_fwd n params node_id_cnt msgs_left st + let (n1, node_id_cnt0) ← + betree_node_apply_messages_back n params node_id_cnt msgs_left st + let len_right ← + betree_list_len_fwd (U64 × betree_message_t) msgs_right + if len_right >= params.betree_params_min_flush_size + then + do + let (n2, node_id_cnt1) ← + betree_node_apply_messages_back n0 params node_id_cnt0 msgs_right + st0 + Result.ret (mkbetree_internal_t i i0 n1 n2, node_id_cnt1) + else Result.ret (mkbetree_internal_t i i0 n1 n0, node_id_cnt0) + else + do + let (n1, node_id_cnt0) ← + betree_node_apply_messages_back n0 params node_id_cnt msgs_right st + Result.ret (mkbetree_internal_t i i0 n n1, node_id_cnt0) + +end + +/- [betree_main::betree::Node::{5}::apply] -/ +def betree_node_apply_fwd + (self : betree_node_t) (params : betree_params_t) + (node_id_cnt : betree_node_id_counter_t) (key : U64) + (new_msg : betree_message_t) (st : State) : + Result (State × Unit) + := + do + let l := betree_list_t.Nil + let (st0, _) ← + betree_node_apply_messages_fwd self params node_id_cnt + (betree_list_t.Cons (key, new_msg) l) st + let _ ← + betree_node_apply_messages_back self params node_id_cnt + (betree_list_t.Cons (key, new_msg) l) st + Result.ret (st0, ()) + +/- [betree_main::betree::Node::{5}::apply] -/ +def betree_node_apply_back + (self : betree_node_t) (params : betree_params_t) + (node_id_cnt : betree_node_id_counter_t) (key : U64) + (new_msg : betree_message_t) (st : State) : + Result (betree_node_t × betree_node_id_counter_t) + := + let l := betree_list_t.Nil + betree_node_apply_messages_back self params node_id_cnt (betree_list_t.Cons + (key, new_msg) l) st + +/- [betree_main::betree::BeTree::{6}::new] -/ +def betree_be_tree_new_fwd + (min_flush_size : U64) (split_size : U64) (st : State) : + Result (State × betree_be_tree_t) + := + do + let node_id_cnt ← betree_node_id_counter_new_fwd + let id ← betree_node_id_counter_fresh_id_fwd node_id_cnt + let (st0, _) ← betree_store_leaf_node_fwd id betree_list_t.Nil st + let node_id_cnt0 ← betree_node_id_counter_fresh_id_back node_id_cnt + Result.ret (st0, + { + betree_be_tree_params := + { + betree_params_min_flush_size := min_flush_size, + betree_params_split_size := split_size + }, + betree_be_tree_node_id_cnt := node_id_cnt0, + betree_be_tree_root := + (betree_node_t.Leaf + { + betree_leaf_id := id, + betree_leaf_size := (U64.ofInt 0 (by intlit)) + }) + }) + +/- [betree_main::betree::BeTree::{6}::apply] -/ +def betree_be_tree_apply_fwd + (self : betree_be_tree_t) (key : U64) (msg : betree_message_t) (st : State) : + Result (State × Unit) + := + do + let (st0, _) ← + betree_node_apply_fwd self.betree_be_tree_root self.betree_be_tree_params + self.betree_be_tree_node_id_cnt key msg st + let _ ← + betree_node_apply_back self.betree_be_tree_root + self.betree_be_tree_params self.betree_be_tree_node_id_cnt key msg st + Result.ret (st0, ()) + +/- [betree_main::betree::BeTree::{6}::apply] -/ +def betree_be_tree_apply_back + (self : betree_be_tree_t) (key : U64) (msg : betree_message_t) (st : State) : + Result betree_be_tree_t + := + do + let (n, nic) ← + betree_node_apply_back self.betree_be_tree_root + self.betree_be_tree_params self.betree_be_tree_node_id_cnt key msg st + Result.ret + { self with betree_be_tree_node_id_cnt := nic, betree_be_tree_root := n } + +/- [betree_main::betree::BeTree::{6}::insert] -/ +def betree_be_tree_insert_fwd + (self : betree_be_tree_t) (key : U64) (value : U64) (st : State) : + Result (State × Unit) + := + do + let (st0, _) ← + betree_be_tree_apply_fwd self key (betree_message_t.Insert value) st + let _ ← + betree_be_tree_apply_back self key (betree_message_t.Insert value) st + Result.ret (st0, ()) + +/- [betree_main::betree::BeTree::{6}::insert] -/ +def betree_be_tree_insert_back + (self : betree_be_tree_t) (key : U64) (value : U64) (st : State) : + Result betree_be_tree_t + := + betree_be_tree_apply_back self key (betree_message_t.Insert value) st + +/- [betree_main::betree::BeTree::{6}::delete] -/ +def betree_be_tree_delete_fwd + (self : betree_be_tree_t) (key : U64) (st : State) : + Result (State × Unit) + := + do + let (st0, _) ← + betree_be_tree_apply_fwd self key betree_message_t.Delete st + let _ ← betree_be_tree_apply_back self key betree_message_t.Delete st + Result.ret (st0, ()) + +/- [betree_main::betree::BeTree::{6}::delete] -/ +def betree_be_tree_delete_back + (self : betree_be_tree_t) (key : U64) (st : State) : + Result betree_be_tree_t + := + betree_be_tree_apply_back self key betree_message_t.Delete st + +/- [betree_main::betree::BeTree::{6}::upsert] -/ +def betree_be_tree_upsert_fwd + (self : betree_be_tree_t) (key : U64) (upd : betree_upsert_fun_state_t) + (st : State) : + Result (State × Unit) + := + do + let (st0, _) ← + betree_be_tree_apply_fwd self key (betree_message_t.Upsert upd) st + let _ ← + betree_be_tree_apply_back self key (betree_message_t.Upsert upd) st + Result.ret (st0, ()) + +/- [betree_main::betree::BeTree::{6}::upsert] -/ +def betree_be_tree_upsert_back + (self : betree_be_tree_t) (key : U64) (upd : betree_upsert_fun_state_t) + (st : State) : + Result betree_be_tree_t + := + betree_be_tree_apply_back self key (betree_message_t.Upsert upd) st + +/- [betree_main::betree::BeTree::{6}::lookup] -/ +def betree_be_tree_lookup_fwd + (self : betree_be_tree_t) (key : U64) (st : State) : + Result (State × (Option U64)) + := + betree_node_lookup_fwd self.betree_be_tree_root key st + +/- [betree_main::betree::BeTree::{6}::lookup] -/ +def betree_be_tree_lookup_back + (self : betree_be_tree_t) (key : U64) (st : State) : + Result betree_be_tree_t + := + do + let n ← betree_node_lookup_back self.betree_be_tree_root key st + Result.ret { self with betree_be_tree_root := n } + +/- [betree_main::main] -/ +def main_fwd : Result Unit := + Result.ret () + +/- Unit test for [betree_main::main] -/ +#assert (main_fwd == .ret ()) + diff --git a/tests/lean/BetreeMain/Opaque.lean b/tests/lean/BetreeMain/Opaque.lean new file mode 100644 index 00000000..c8226d4e --- /dev/null +++ b/tests/lean/BetreeMain/Opaque.lean @@ -0,0 +1,31 @@ +-- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS +-- [betree_main]: opaque function definitions +import Base +import BetreeMain.Types +open Primitives + +structure OpaqueDefs where + + /- [betree_main::betree_utils::load_internal_node] -/ + betree_utils_load_internal_node_fwd + : + U64 -> State -> Result (State × (betree_list_t (U64 × betree_message_t))) + + /- [betree_main::betree_utils::store_internal_node] -/ + betree_utils_store_internal_node_fwd + : + U64 -> betree_list_t (U64 × betree_message_t) -> State -> Result (State × + Unit) + + /- [betree_main::betree_utils::load_leaf_node] -/ + betree_utils_load_leaf_node_fwd + : U64 -> State -> Result (State × (betree_list_t (U64 × U64))) + + /- [betree_main::betree_utils::store_leaf_node] -/ + betree_utils_store_leaf_node_fwd + : U64 -> betree_list_t (U64 × U64) -> State -> Result (State × Unit) + + /- [core::option::Option::{0}::unwrap] -/ + core_option_option_unwrap_fwd + (T : Type) : Option T -> State -> Result (State × T) + diff --git a/tests/lean/BetreeMain/Types.lean b/tests/lean/BetreeMain/Types.lean new file mode 100644 index 00000000..4875a8ba --- /dev/null +++ b/tests/lean/BetreeMain/Types.lean @@ -0,0 +1,62 @@ +-- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS +-- [betree_main]: type definitions +import Base +open Primitives + +/- [betree_main::betree::List] -/ +inductive betree_list_t (T : Type) := +| Cons : T -> betree_list_t T -> betree_list_t T +| Nil : betree_list_t T + +/- [betree_main::betree::UpsertFunState] -/ +inductive betree_upsert_fun_state_t := +| Add : U64 -> betree_upsert_fun_state_t +| Sub : U64 -> betree_upsert_fun_state_t + +/- [betree_main::betree::Message] -/ +inductive betree_message_t := +| Insert : U64 -> betree_message_t +| Delete : betree_message_t +| Upsert : betree_upsert_fun_state_t -> betree_message_t + +/- [betree_main::betree::Leaf] -/ +structure betree_leaf_t where + betree_leaf_id : U64 + betree_leaf_size : U64 + +mutual + +/- [betree_main::betree::Node] -/ +inductive betree_node_t := +| Internal : betree_internal_t -> betree_node_t +| Leaf : betree_leaf_t -> betree_node_t + +/- [betree_main::betree::Internal] -/ +inductive betree_internal_t := +| mkbetree_internal_t : + U64 -> + U64 -> + betree_node_t -> + betree_node_t -> + betree_internal_t + +end + +/- [betree_main::betree::Params] -/ +structure betree_params_t where + betree_params_min_flush_size : U64 + betree_params_split_size : U64 + +/- [betree_main::betree::NodeIdCounter] -/ +structure betree_node_id_counter_t where + betree_node_id_counter_next_node_id : U64 + +/- [betree_main::betree::BeTree] -/ +structure betree_be_tree_t where + betree_be_tree_params : betree_params_t + betree_be_tree_node_id_cnt : betree_node_id_counter_t + betree_be_tree_root : betree_node_t + +/- The state type used in the state-error monad -/ +axiom State : Type + diff --git a/tests/lean/Constants.lean b/tests/lean/Constants.lean new file mode 100644 index 00000000..cd2f88f5 --- /dev/null +++ b/tests/lean/Constants.lean @@ -0,0 +1,132 @@ +-- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS +-- [constants] +import Base +open Primitives + +/- [constants::X0] -/ +def x0_body : Result U32 := Result.ret (U32.ofInt 0 (by intlit)) +def x0_c : U32 := eval_global x0_body (by simp) + +/- [core::num::u32::{9}::MAX] -/ +def core_num_u32_max_body : Result U32 := + Result.ret (U32.ofInt 4294967295 (by intlit)) +def core_num_u32_max_c : U32 := eval_global core_num_u32_max_body (by simp) + +/- [constants::X1] -/ +def x1_body : Result U32 := Result.ret core_num_u32_max_c +def x1_c : U32 := eval_global x1_body (by simp) + +/- [constants::X2] -/ +def x2_body : Result U32 := Result.ret (U32.ofInt 3 (by intlit)) +def x2_c : U32 := eval_global x2_body (by simp) + +/- [constants::incr] -/ +def incr_fwd (n : U32) : Result U32 := + n + (U32.ofInt 1 (by intlit)) + +/- [constants::X3] -/ +def x3_body : Result U32 := incr_fwd (U32.ofInt 32 (by intlit)) +def x3_c : U32 := eval_global x3_body (by simp) + +/- [constants::mk_pair0] -/ +def mk_pair0_fwd (x : U32) (y : U32) : Result (U32 × U32) := + Result.ret (x, y) + +/- [constants::Pair] -/ +structure pair_t (T1 T2 : Type) where + pair_x : T1 + pair_y : T2 + +/- [constants::mk_pair1] -/ +def mk_pair1_fwd (x : U32) (y : U32) : Result (pair_t U32 U32) := + Result.ret { pair_x := x, pair_y := y } + +/- [constants::P0] -/ +def p0_body : Result (U32 × U32) := + mk_pair0_fwd (U32.ofInt 0 (by intlit)) (U32.ofInt 1 (by intlit)) +def p0_c : (U32 × U32) := eval_global p0_body (by simp) + +/- [constants::P1] -/ +def p1_body : Result (pair_t U32 U32) := + mk_pair1_fwd (U32.ofInt 0 (by intlit)) (U32.ofInt 1 (by intlit)) +def p1_c : pair_t U32 U32 := eval_global p1_body (by simp) + +/- [constants::P2] -/ +def p2_body : Result (U32 × U32) := + Result.ret ((U32.ofInt 0 (by intlit)), (U32.ofInt 1 (by intlit))) +def p2_c : (U32 × U32) := eval_global p2_body (by simp) + +/- [constants::P3] -/ +def p3_body : Result (pair_t U32 U32) := + Result.ret + { pair_x := (U32.ofInt 0 (by intlit)), pair_y := (U32.ofInt 1 (by intlit)) } +def p3_c : pair_t U32 U32 := eval_global p3_body (by simp) + +/- [constants::Wrap] -/ +structure wrap_t (T : Type) where + wrap_val : T + +/- [constants::Wrap::{0}::new] -/ +def wrap_new_fwd (T : Type) (val : T) : Result (wrap_t T) := + Result.ret { wrap_val := val } + +/- [constants::Y] -/ +def y_body : Result (wrap_t I32) := wrap_new_fwd I32 (I32.ofInt 2 (by intlit)) +def y_c : wrap_t I32 := eval_global y_body (by simp) + +/- [constants::unwrap_y] -/ +def unwrap_y_fwd : Result I32 := + Result.ret y_c.wrap_val + +/- [constants::YVAL] -/ +def yval_body : Result I32 := unwrap_y_fwd +def yval_c : I32 := eval_global yval_body (by simp) + +/- [constants::get_z1::Z1] -/ +def get_z1_z1_body : Result I32 := Result.ret (I32.ofInt 3 (by intlit)) +def get_z1_z1_c : I32 := eval_global get_z1_z1_body (by simp) + +/- [constants::get_z1] -/ +def get_z1_fwd : Result I32 := + Result.ret get_z1_z1_c + +/- [constants::add] -/ +def add_fwd (a : I32) (b : I32) : Result I32 := + a + b + +/- [constants::Q1] -/ +def q1_body : Result I32 := Result.ret (I32.ofInt 5 (by intlit)) +def q1_c : I32 := eval_global q1_body (by simp) + +/- [constants::Q2] -/ +def q2_body : Result I32 := Result.ret q1_c +def q2_c : I32 := eval_global q2_body (by simp) + +/- [constants::Q3] -/ +def q3_body : Result I32 := add_fwd q2_c (I32.ofInt 3 (by intlit)) +def q3_c : I32 := eval_global q3_body (by simp) + +/- [constants::get_z2] -/ +def get_z2_fwd : Result I32 := + do + let i ← get_z1_fwd + let i0 ← add_fwd i q3_c + add_fwd q1_c i0 + +/- [constants::S1] -/ +def s1_body : Result U32 := Result.ret (U32.ofInt 6 (by intlit)) +def s1_c : U32 := eval_global s1_body (by simp) + +/- [constants::S2] -/ +def s2_body : Result U32 := incr_fwd s1_c +def s2_c : U32 := eval_global s2_body (by simp) + +/- [constants::S3] -/ +def s3_body : Result (pair_t U32 U32) := Result.ret p3_c +def s3_c : pair_t U32 U32 := eval_global s3_body (by simp) + +/- [constants::S4] -/ +def s4_body : Result (pair_t U32 U32) := + mk_pair1_fwd (U32.ofInt 7 (by intlit)) (U32.ofInt 8 (by intlit)) +def s4_c : pair_t U32 U32 := eval_global s4_body (by simp) + diff --git a/tests/lean/External.lean b/tests/lean/External.lean new file mode 100644 index 00000000..b95db309 --- /dev/null +++ b/tests/lean/External.lean @@ -0,0 +1 @@ +import External.Funs diff --git a/tests/lean/External/Funs.lean b/tests/lean/External/Funs.lean new file mode 100644 index 00000000..73e45938 --- /dev/null +++ b/tests/lean/External/Funs.lean @@ -0,0 +1,88 @@ +-- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS +-- [external]: function definitions +import Base +import External.Types +import External.ExternalFuns +open Primitives + +/- [external::swap] -/ +def swap_fwd + (T : Type) (x : T) (y : T) (st : State) : Result (State × Unit) := + do + let (st0, _) ← opaque_defs.core_mem_swap_fwd T x y st + let (st1, _) ← opaque_defs.core_mem_swap_back0 T x y st st0 + let (st2, _) ← opaque_defs.core_mem_swap_back1 T x y st st1 + Result.ret (st2, ()) + +/- [external::swap] -/ +def swap_back + (T : Type) (x : T) (y : T) (st : State) (st0 : State) : + Result (State × (T × T)) + := + do + let (st1, _) ← opaque_defs.core_mem_swap_fwd T x y st + let (st2, x0) ← opaque_defs.core_mem_swap_back0 T x y st st1 + let (_, y0) ← opaque_defs.core_mem_swap_back1 T x y st st2 + Result.ret (st0, (x0, y0)) + +/- [external::test_new_non_zero_u32] -/ +def test_new_non_zero_u32_fwd + (x : U32) (st : State) : Result (State × core_num_nonzero_non_zero_u32_t) := + do + let (st0, opt) ← opaque_defs.core_num_nonzero_non_zero_u32_new_fwd x st + opaque_defs.core_option_option_unwrap_fwd core_num_nonzero_non_zero_u32_t + opt st0 + +/- [external::test_vec] -/ +def test_vec_fwd : Result Unit := + do + let v := vec_new U32 + let _ ← vec_push_back U32 v (U32.ofInt 0 (by intlit)) + Result.ret () + +/- Unit test for [external::test_vec] -/ +#assert (test_vec_fwd == .ret ()) + +/- [external::custom_swap] -/ +def custom_swap_fwd + (T : Type) (x : T) (y : T) (st : State) : Result (State × T) := + do + let (st0, _) ← opaque_defs.core_mem_swap_fwd T x y st + let (st1, x0) ← opaque_defs.core_mem_swap_back0 T x y st st0 + let (st2, _) ← opaque_defs.core_mem_swap_back1 T x y st st1 + Result.ret (st2, x0) + +/- [external::custom_swap] -/ +def custom_swap_back + (T : Type) (x : T) (y : T) (st : State) (ret0 : T) (st0 : State) : + Result (State × (T × T)) + := + do + let (st1, _) ← opaque_defs.core_mem_swap_fwd T x y st + let (st2, _) ← opaque_defs.core_mem_swap_back0 T x y st st1 + let (_, y0) ← opaque_defs.core_mem_swap_back1 T x y st st2 + Result.ret (st0, (ret0, y0)) + +/- [external::test_custom_swap] -/ +def test_custom_swap_fwd + (x : U32) (y : U32) (st : State) : Result (State × Unit) := + do + let (st0, _) ← custom_swap_fwd U32 x y st + Result.ret (st0, ()) + +/- [external::test_custom_swap] -/ +def test_custom_swap_back + (x : U32) (y : U32) (st : State) (st0 : State) : + Result (State × (U32 × U32)) + := + custom_swap_back U32 x y st (U32.ofInt 1 (by intlit)) st0 + +/- [external::test_swap_non_zero] -/ +def test_swap_non_zero_fwd (x : U32) (st : State) : Result (State × U32) := + do + let (st0, _) ← swap_fwd U32 x (U32.ofInt 0 (by intlit)) st + let (st1, (x0, _)) ← swap_back U32 x (U32.ofInt 0 (by intlit)) st st0 + if x0 = (U32.ofInt 0 (by intlit)) + then Result.fail Error.panic + else Result.ret (st1, x0) + diff --git a/tests/lean/External/Opaque.lean b/tests/lean/External/Opaque.lean new file mode 100644 index 00000000..5483c3a9 --- /dev/null +++ b/tests/lean/External/Opaque.lean @@ -0,0 +1,28 @@ +-- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS +-- [external]: opaque function definitions +import Base +import External.Types +open Primitives + +structure OpaqueDefs where + + /- [core::mem::swap] -/ + core_mem_swap_fwd (T : Type) : T -> T -> State -> Result (State × Unit) + + /- [core::mem::swap] -/ + core_mem_swap_back0 + (T : Type) : T -> T -> State -> State -> Result (State × T) + + /- [core::mem::swap] -/ + core_mem_swap_back1 + (T : Type) : T -> T -> State -> State -> Result (State × T) + + /- [core::num::nonzero::NonZeroU32::{14}::new] -/ + core_num_nonzero_non_zero_u32_new_fwd + : + U32 -> State -> Result (State × (Option core_num_nonzero_non_zero_u32_t)) + + /- [core::option::Option::{0}::unwrap] -/ + core_option_option_unwrap_fwd + (T : Type) : Option T -> State -> Result (State × T) + diff --git a/tests/lean/External/Types.lean b/tests/lean/External/Types.lean new file mode 100644 index 00000000..25907da2 --- /dev/null +++ b/tests/lean/External/Types.lean @@ -0,0 +1,11 @@ +-- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS +-- [external]: type definitions +import Base +open Primitives + +/- [core::num::nonzero::NonZeroU32] -/ +axiom core_num_nonzero_non_zero_u32_t : Type + +/- The state type used in the state-error monad -/ +axiom State : Type + diff --git a/tests/lean/Hashmap.lean b/tests/lean/Hashmap.lean new file mode 100644 index 00000000..41630205 --- /dev/null +++ b/tests/lean/Hashmap.lean @@ -0,0 +1 @@ +import Hashmap.Funs diff --git a/tests/lean/Hashmap/Funs.lean b/tests/lean/Hashmap/Funs.lean new file mode 100644 index 00000000..26742d5d --- /dev/null +++ b/tests/lean/Hashmap/Funs.lean @@ -0,0 +1,474 @@ +-- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS +-- [hashmap]: function definitions +import Base +import Hashmap.Types +open Primitives + +/- [hashmap::hash_key] -/ +def hash_key_fwd (k : Usize) : Result Usize := + Result.ret k + +/- [hashmap::HashMap::{0}::allocate_slots] -/ +divergent def hash_map_allocate_slots_loop_fwd + (T : Type) (slots : Vec (list_t T)) (n : Usize) : Result (Vec (list_t T)) := + if n > (Usize.ofInt 0 (by intlit)) + then + do + let slots0 ← vec_push_back (list_t T) slots list_t.Nil + let n0 ← n - (Usize.ofInt 1 (by intlit)) + hash_map_allocate_slots_loop_fwd T slots0 n0 + else Result.ret slots + +/- [hashmap::HashMap::{0}::allocate_slots] -/ +def hash_map_allocate_slots_fwd + (T : Type) (slots : Vec (list_t T)) (n : Usize) : Result (Vec (list_t T)) := + hash_map_allocate_slots_loop_fwd T slots n + +/- [hashmap::HashMap::{0}::new_with_capacity] -/ +def hash_map_new_with_capacity_fwd + (T : Type) (capacity : Usize) (max_load_dividend : Usize) + (max_load_divisor : Usize) : + Result (hash_map_t T) + := + do + let v := vec_new (list_t T) + let slots ← hash_map_allocate_slots_fwd T v capacity + let i ← capacity * max_load_dividend + let i0 ← i / max_load_divisor + Result.ret + { + hash_map_num_entries := (Usize.ofInt 0 (by intlit)), + hash_map_max_load_factor := (max_load_dividend, max_load_divisor), + hash_map_max_load := i0, + hash_map_slots := slots + } + +/- [hashmap::HashMap::{0}::new] -/ +def hash_map_new_fwd (T : Type) : Result (hash_map_t T) := + hash_map_new_with_capacity_fwd T (Usize.ofInt 32 (by intlit)) + (Usize.ofInt 4 (by intlit)) (Usize.ofInt 5 (by intlit)) + +/- [hashmap::HashMap::{0}::clear] -/ +divergent def hash_map_clear_loop_fwd_back + (T : Type) (slots : Vec (list_t T)) (i : Usize) : Result (Vec (list_t T)) := + let i0 := vec_len (list_t T) slots + if i < i0 + then + do + let i1 ← i + (Usize.ofInt 1 (by intlit)) + let slots0 ← vec_index_mut_back (list_t T) slots i list_t.Nil + hash_map_clear_loop_fwd_back T slots0 i1 + else Result.ret slots + +/- [hashmap::HashMap::{0}::clear] -/ +def hash_map_clear_fwd_back + (T : Type) (self : hash_map_t T) : Result (hash_map_t T) := + do + let v ← + hash_map_clear_loop_fwd_back T self.hash_map_slots + (Usize.ofInt 0 (by intlit)) + Result.ret + { + self + with + hash_map_num_entries := (Usize.ofInt 0 (by intlit)), + hash_map_slots := v + } + +/- [hashmap::HashMap::{0}::len] -/ +def hash_map_len_fwd (T : Type) (self : hash_map_t T) : Result Usize := + Result.ret self.hash_map_num_entries + +/- [hashmap::HashMap::{0}::insert_in_list] -/ +divergent def hash_map_insert_in_list_loop_fwd + (T : Type) (key : Usize) (value : T) (ls : list_t T) : Result Bool := + match h: ls with + | list_t.Cons ckey cvalue tl => + if ckey = key + then Result.ret false + else hash_map_insert_in_list_loop_fwd T key value tl + | list_t.Nil => Result.ret true + +/- [hashmap::HashMap::{0}::insert_in_list] -/ +def hash_map_insert_in_list_fwd + (T : Type) (key : Usize) (value : T) (ls : list_t T) : Result Bool := + hash_map_insert_in_list_loop_fwd T key value ls + +/- [hashmap::HashMap::{0}::insert_in_list] -/ +divergent def hash_map_insert_in_list_loop_back + (T : Type) (key : Usize) (value : T) (ls : list_t T) : Result (list_t T) := + match h: ls with + | list_t.Cons ckey cvalue tl => + if ckey = key + then Result.ret (list_t.Cons ckey value tl) + else + do + let tl0 ← hash_map_insert_in_list_loop_back T key value tl + Result.ret (list_t.Cons ckey cvalue tl0) + | list_t.Nil => let l := list_t.Nil + Result.ret (list_t.Cons key value l) + +/- [hashmap::HashMap::{0}::insert_in_list] -/ +def hash_map_insert_in_list_back + (T : Type) (key : Usize) (value : T) (ls : list_t T) : Result (list_t T) := + hash_map_insert_in_list_loop_back T key value ls + +/- [hashmap::HashMap::{0}::insert_no_resize] -/ +def hash_map_insert_no_resize_fwd_back + (T : Type) (self : hash_map_t T) (key : Usize) (value : T) : + Result (hash_map_t T) + := + do + let hash ← hash_key_fwd key + let i := vec_len (list_t T) self.hash_map_slots + let hash_mod ← hash % i + let l ← vec_index_mut_fwd (list_t T) self.hash_map_slots hash_mod + let inserted ← hash_map_insert_in_list_fwd T key value l + if inserted + then + do + let i0 ← self.hash_map_num_entries + (Usize.ofInt 1 (by intlit)) + let l0 ← hash_map_insert_in_list_back T key value l + let v ← vec_index_mut_back (list_t T) self.hash_map_slots hash_mod l0 + Result.ret + { self with hash_map_num_entries := i0, hash_map_slots := v } + else + do + let l0 ← hash_map_insert_in_list_back T key value l + let v ← vec_index_mut_back (list_t T) self.hash_map_slots hash_mod l0 + Result.ret { self with hash_map_slots := v } + +/- [core::num::u32::{9}::MAX] -/ +def core_num_u32_max_body : Result U32 := + Result.ret (U32.ofInt 4294967295 (by intlit)) +def core_num_u32_max_c : U32 := eval_global core_num_u32_max_body (by simp) + +/- [hashmap::HashMap::{0}::move_elements_from_list] -/ +divergent def hash_map_move_elements_from_list_loop_fwd_back + (T : Type) (ntable : hash_map_t T) (ls : list_t T) : Result (hash_map_t T) := + match h: ls with + | list_t.Cons k v tl => + do + let ntable0 ← hash_map_insert_no_resize_fwd_back T ntable k v + hash_map_move_elements_from_list_loop_fwd_back T ntable0 tl + | list_t.Nil => Result.ret ntable + +/- [hashmap::HashMap::{0}::move_elements_from_list] -/ +def hash_map_move_elements_from_list_fwd_back + (T : Type) (ntable : hash_map_t T) (ls : list_t T) : Result (hash_map_t T) := + hash_map_move_elements_from_list_loop_fwd_back T ntable ls + +/- [hashmap::HashMap::{0}::move_elements] -/ +divergent def hash_map_move_elements_loop_fwd_back + (T : Type) (ntable : hash_map_t T) (slots : Vec (list_t T)) (i : Usize) : + Result ((hash_map_t T) × (Vec (list_t T))) + := + let i0 := vec_len (list_t T) slots + if i < i0 + then + do + let l ← vec_index_mut_fwd (list_t T) slots i + let ls := mem_replace_fwd (list_t T) l list_t.Nil + let ntable0 ← hash_map_move_elements_from_list_fwd_back T ntable ls + let i1 ← i + (Usize.ofInt 1 (by intlit)) + let l0 := mem_replace_back (list_t T) l list_t.Nil + let slots0 ← vec_index_mut_back (list_t T) slots i l0 + hash_map_move_elements_loop_fwd_back T ntable0 slots0 i1 + else Result.ret (ntable, slots) + +/- [hashmap::HashMap::{0}::move_elements] -/ +def hash_map_move_elements_fwd_back + (T : Type) (ntable : hash_map_t T) (slots : Vec (list_t T)) (i : Usize) : + Result ((hash_map_t T) × (Vec (list_t T))) + := + hash_map_move_elements_loop_fwd_back T ntable slots i + +/- [hashmap::HashMap::{0}::try_resize] -/ +def hash_map_try_resize_fwd_back + (T : Type) (self : hash_map_t T) : Result (hash_map_t T) := + do + let max_usize ← Scalar.cast .Usize core_num_u32_max_c + let capacity := vec_len (list_t T) self.hash_map_slots + let n1 ← max_usize / (Usize.ofInt 2 (by intlit)) + let (i, i0) := self.hash_map_max_load_factor + let i1 ← n1 / i + if capacity <= i1 + then + do + let i2 ← capacity * (Usize.ofInt 2 (by intlit)) + let ntable ← hash_map_new_with_capacity_fwd T i2 i i0 + let (ntable0, _) ← + hash_map_move_elements_fwd_back T ntable self.hash_map_slots + (Usize.ofInt 0 (by intlit)) + Result.ret + { + ntable0 + with + hash_map_num_entries := self.hash_map_num_entries, + hash_map_max_load_factor := (i, i0) + } + else Result.ret { self with hash_map_max_load_factor := (i, i0) } + +/- [hashmap::HashMap::{0}::insert] -/ +def hash_map_insert_fwd_back + (T : Type) (self : hash_map_t T) (key : Usize) (value : T) : + Result (hash_map_t T) + := + do + let self0 ← hash_map_insert_no_resize_fwd_back T self key value + let i ← hash_map_len_fwd T self0 + if i > self0.hash_map_max_load + then hash_map_try_resize_fwd_back T self0 + else Result.ret self0 + +/- [hashmap::HashMap::{0}::contains_key_in_list] -/ +divergent def hash_map_contains_key_in_list_loop_fwd + (T : Type) (key : Usize) (ls : list_t T) : Result Bool := + match h: ls with + | list_t.Cons ckey t tl => + if ckey = key + then Result.ret true + else hash_map_contains_key_in_list_loop_fwd T key tl + | list_t.Nil => Result.ret false + +/- [hashmap::HashMap::{0}::contains_key_in_list] -/ +def hash_map_contains_key_in_list_fwd + (T : Type) (key : Usize) (ls : list_t T) : Result Bool := + hash_map_contains_key_in_list_loop_fwd T key ls + +/- [hashmap::HashMap::{0}::contains_key] -/ +def hash_map_contains_key_fwd + (T : Type) (self : hash_map_t T) (key : Usize) : Result Bool := + do + let hash ← hash_key_fwd key + let i := vec_len (list_t T) self.hash_map_slots + let hash_mod ← hash % i + let l ← vec_index_fwd (list_t T) self.hash_map_slots hash_mod + hash_map_contains_key_in_list_fwd T key l + +/- [hashmap::HashMap::{0}::get_in_list] -/ +divergent def hash_map_get_in_list_loop_fwd + (T : Type) (key : Usize) (ls : list_t T) : Result T := + match h: ls with + | list_t.Cons ckey cvalue tl => + if ckey = key + then Result.ret cvalue + else hash_map_get_in_list_loop_fwd T key tl + | list_t.Nil => Result.fail Error.panic + +/- [hashmap::HashMap::{0}::get_in_list] -/ +def hash_map_get_in_list_fwd + (T : Type) (key : Usize) (ls : list_t T) : Result T := + hash_map_get_in_list_loop_fwd T key ls + +/- [hashmap::HashMap::{0}::get] -/ +def hash_map_get_fwd + (T : Type) (self : hash_map_t T) (key : Usize) : Result T := + do + let hash ← hash_key_fwd key + let i := vec_len (list_t T) self.hash_map_slots + let hash_mod ← hash % i + let l ← vec_index_fwd (list_t T) self.hash_map_slots hash_mod + hash_map_get_in_list_fwd T key l + +/- [hashmap::HashMap::{0}::get_mut_in_list] -/ +divergent def hash_map_get_mut_in_list_loop_fwd + (T : Type) (ls : list_t T) (key : Usize) : Result T := + match h: ls with + | list_t.Cons ckey cvalue tl => + if ckey = key + then Result.ret cvalue + else hash_map_get_mut_in_list_loop_fwd T tl key + | list_t.Nil => Result.fail Error.panic + +/- [hashmap::HashMap::{0}::get_mut_in_list] -/ +def hash_map_get_mut_in_list_fwd + (T : Type) (ls : list_t T) (key : Usize) : Result T := + hash_map_get_mut_in_list_loop_fwd T ls key + +/- [hashmap::HashMap::{0}::get_mut_in_list] -/ +divergent def hash_map_get_mut_in_list_loop_back + (T : Type) (ls : list_t T) (key : Usize) (ret0 : T) : Result (list_t T) := + match h: ls with + | list_t.Cons ckey cvalue tl => + if ckey = key + then Result.ret (list_t.Cons ckey ret0 tl) + else + do + let tl0 ← hash_map_get_mut_in_list_loop_back T tl key ret0 + Result.ret (list_t.Cons ckey cvalue tl0) + | list_t.Nil => Result.fail Error.panic + +/- [hashmap::HashMap::{0}::get_mut_in_list] -/ +def hash_map_get_mut_in_list_back + (T : Type) (ls : list_t T) (key : Usize) (ret0 : T) : Result (list_t T) := + hash_map_get_mut_in_list_loop_back T ls key ret0 + +/- [hashmap::HashMap::{0}::get_mut] -/ +def hash_map_get_mut_fwd + (T : Type) (self : hash_map_t T) (key : Usize) : Result T := + do + let hash ← hash_key_fwd key + let i := vec_len (list_t T) self.hash_map_slots + let hash_mod ← hash % i + let l ← vec_index_mut_fwd (list_t T) self.hash_map_slots hash_mod + hash_map_get_mut_in_list_fwd T l key + +/- [hashmap::HashMap::{0}::get_mut] -/ +def hash_map_get_mut_back + (T : Type) (self : hash_map_t T) (key : Usize) (ret0 : T) : + Result (hash_map_t T) + := + do + let hash ← hash_key_fwd key + let i := vec_len (list_t T) self.hash_map_slots + let hash_mod ← hash % i + let l ← vec_index_mut_fwd (list_t T) self.hash_map_slots hash_mod + let l0 ← hash_map_get_mut_in_list_back T l key ret0 + let v ← vec_index_mut_back (list_t T) self.hash_map_slots hash_mod l0 + Result.ret { self with hash_map_slots := v } + +/- [hashmap::HashMap::{0}::remove_from_list] -/ +divergent def hash_map_remove_from_list_loop_fwd + (T : Type) (key : Usize) (ls : list_t T) : Result (Option T) := + match h: ls with + | list_t.Cons ckey t tl => + if ckey = key + then + let mv_ls := + mem_replace_fwd (list_t T) (list_t.Cons ckey t tl) list_t.Nil + match h: mv_ls with + | list_t.Cons i cvalue tl0 => Result.ret (Option.some cvalue) + | list_t.Nil => Result.fail Error.panic + else hash_map_remove_from_list_loop_fwd T key tl + | list_t.Nil => Result.ret Option.none + +/- [hashmap::HashMap::{0}::remove_from_list] -/ +def hash_map_remove_from_list_fwd + (T : Type) (key : Usize) (ls : list_t T) : Result (Option T) := + hash_map_remove_from_list_loop_fwd T key ls + +/- [hashmap::HashMap::{0}::remove_from_list] -/ +divergent def hash_map_remove_from_list_loop_back + (T : Type) (key : Usize) (ls : list_t T) : Result (list_t T) := + match h: ls with + | list_t.Cons ckey t tl => + if ckey = key + then + let mv_ls := + mem_replace_fwd (list_t T) (list_t.Cons ckey t tl) list_t.Nil + match h: mv_ls with + | list_t.Cons i cvalue tl0 => Result.ret tl0 + | list_t.Nil => Result.fail Error.panic + else + do + let tl0 ← hash_map_remove_from_list_loop_back T key tl + Result.ret (list_t.Cons ckey t tl0) + | list_t.Nil => Result.ret list_t.Nil + +/- [hashmap::HashMap::{0}::remove_from_list] -/ +def hash_map_remove_from_list_back + (T : Type) (key : Usize) (ls : list_t T) : Result (list_t T) := + hash_map_remove_from_list_loop_back T key ls + +/- [hashmap::HashMap::{0}::remove] -/ +def hash_map_remove_fwd + (T : Type) (self : hash_map_t T) (key : Usize) : Result (Option T) := + do + let hash ← hash_key_fwd key + let i := vec_len (list_t T) self.hash_map_slots + let hash_mod ← hash % i + let l ← vec_index_mut_fwd (list_t T) self.hash_map_slots hash_mod + let x ← hash_map_remove_from_list_fwd T key l + match h: x with + | Option.none => Result.ret Option.none + | Option.some x0 => + do + let _ ← self.hash_map_num_entries - (Usize.ofInt 1 (by intlit)) + Result.ret (Option.some x0) + +/- [hashmap::HashMap::{0}::remove] -/ +def hash_map_remove_back + (T : Type) (self : hash_map_t T) (key : Usize) : Result (hash_map_t T) := + do + let hash ← hash_key_fwd key + let i := vec_len (list_t T) self.hash_map_slots + let hash_mod ← hash % i + let l ← vec_index_mut_fwd (list_t T) self.hash_map_slots hash_mod + let x ← hash_map_remove_from_list_fwd T key l + match h: x with + | Option.none => + do + let l0 ← hash_map_remove_from_list_back T key l + let v ← vec_index_mut_back (list_t T) self.hash_map_slots hash_mod l0 + Result.ret { self with hash_map_slots := v } + | Option.some x0 => + do + let i0 ← self.hash_map_num_entries - (Usize.ofInt 1 (by intlit)) + let l0 ← hash_map_remove_from_list_back T key l + let v ← vec_index_mut_back (list_t T) self.hash_map_slots hash_mod l0 + Result.ret + { self with hash_map_num_entries := i0, hash_map_slots := v } + +/- [hashmap::test1] -/ +def test1_fwd : Result Unit := + do + let hm ← hash_map_new_fwd U64 + let hm0 ← + hash_map_insert_fwd_back U64 hm (Usize.ofInt 0 (by intlit)) + (U64.ofInt 42 (by intlit)) + let hm1 ← + hash_map_insert_fwd_back U64 hm0 (Usize.ofInt 128 (by intlit)) + (U64.ofInt 18 (by intlit)) + let hm2 ← + hash_map_insert_fwd_back U64 hm1 (Usize.ofInt 1024 (by intlit)) + (U64.ofInt 138 (by intlit)) + let hm3 ← + hash_map_insert_fwd_back U64 hm2 (Usize.ofInt 1056 (by intlit)) + (U64.ofInt 256 (by intlit)) + let i ← hash_map_get_fwd U64 hm3 (Usize.ofInt 128 (by intlit)) + if not (i = (U64.ofInt 18 (by intlit))) + then Result.fail Error.panic + else + do + let hm4 ← + hash_map_get_mut_back U64 hm3 (Usize.ofInt 1024 (by intlit)) + (U64.ofInt 56 (by intlit)) + let i0 ← hash_map_get_fwd U64 hm4 (Usize.ofInt 1024 (by intlit)) + if not (i0 = (U64.ofInt 56 (by intlit))) + then Result.fail Error.panic + else + do + let x ← + hash_map_remove_fwd U64 hm4 (Usize.ofInt 1024 (by intlit)) + match h: x with + | Option.none => Result.fail Error.panic + | Option.some x0 => + if not (x0 = (U64.ofInt 56 (by intlit))) + then Result.fail Error.panic + else + do + let hm5 ← + hash_map_remove_back U64 hm4 (Usize.ofInt 1024 (by intlit)) + let i1 ← + hash_map_get_fwd U64 hm5 (Usize.ofInt 0 (by intlit)) + if not (i1 = (U64.ofInt 42 (by intlit))) + then Result.fail Error.panic + else + do + let i2 ← + hash_map_get_fwd U64 hm5 (Usize.ofInt 128 (by intlit)) + if not (i2 = (U64.ofInt 18 (by intlit))) + then Result.fail Error.panic + else + do + let i3 ← + hash_map_get_fwd U64 hm5 + (Usize.ofInt 1056 (by intlit)) + if not (i3 = (U64.ofInt 256 (by intlit))) + then Result.fail Error.panic + else Result.ret () + +/- Unit test for [hashmap::test1] -/ +#assert (test1_fwd == .ret ()) + diff --git a/tests/lean/Hashmap/Types.lean b/tests/lean/Hashmap/Types.lean new file mode 100644 index 00000000..af26f363 --- /dev/null +++ b/tests/lean/Hashmap/Types.lean @@ -0,0 +1,17 @@ +-- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS +-- [hashmap]: type definitions +import Base +open Primitives + +/- [hashmap::List] -/ +inductive list_t (T : Type) := +| Cons : Usize -> T -> list_t T -> list_t T +| Nil : list_t T + +/- [hashmap::HashMap] -/ +structure hash_map_t (T : Type) where + hash_map_num_entries : Usize + hash_map_max_load_factor : (Usize × Usize) + hash_map_max_load : Usize + hash_map_slots : Vec (list_t T) + diff --git a/tests/lean/HashmapMain.lean b/tests/lean/HashmapMain.lean new file mode 100644 index 00000000..1a4e7f82 --- /dev/null +++ b/tests/lean/HashmapMain.lean @@ -0,0 +1 @@ +import HashmapMain.Funs diff --git a/tests/lean/HashmapMain/Funs.lean b/tests/lean/HashmapMain/Funs.lean new file mode 100644 index 00000000..a59a9f26 --- /dev/null +++ b/tests/lean/HashmapMain/Funs.lean @@ -0,0 +1,557 @@ +-- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS +-- [hashmap_main]: function definitions +import Base +import HashmapMain.Types +import HashmapMain.ExternalFuns +open Primitives + +/- [hashmap_main::hashmap::hash_key] -/ +def hashmap_hash_key_fwd (k : Usize) : Result Usize := + Result.ret k + +/- [hashmap_main::hashmap::HashMap::{0}::allocate_slots] -/ +divergent def hashmap_hash_map_allocate_slots_loop_fwd + (T : Type) (slots : Vec (hashmap_list_t T)) (n : Usize) : + Result (Vec (hashmap_list_t T)) + := + if n > (Usize.ofInt 0 (by intlit)) + then + do + let slots0 ← vec_push_back (hashmap_list_t T) slots hashmap_list_t.Nil + let n0 ← n - (Usize.ofInt 1 (by intlit)) + hashmap_hash_map_allocate_slots_loop_fwd T slots0 n0 + else Result.ret slots + +/- [hashmap_main::hashmap::HashMap::{0}::allocate_slots] -/ +def hashmap_hash_map_allocate_slots_fwd + (T : Type) (slots : Vec (hashmap_list_t T)) (n : Usize) : + Result (Vec (hashmap_list_t T)) + := + hashmap_hash_map_allocate_slots_loop_fwd T slots n + +/- [hashmap_main::hashmap::HashMap::{0}::new_with_capacity] -/ +def hashmap_hash_map_new_with_capacity_fwd + (T : Type) (capacity : Usize) (max_load_dividend : Usize) + (max_load_divisor : Usize) : + Result (hashmap_hash_map_t T) + := + do + let v := vec_new (hashmap_list_t T) + let slots ← hashmap_hash_map_allocate_slots_fwd T v capacity + let i ← capacity * max_load_dividend + let i0 ← i / max_load_divisor + Result.ret + { + hashmap_hash_map_num_entries := (Usize.ofInt 0 (by intlit)), + hashmap_hash_map_max_load_factor := + (max_load_dividend, max_load_divisor), + hashmap_hash_map_max_load := i0, + hashmap_hash_map_slots := slots + } + +/- [hashmap_main::hashmap::HashMap::{0}::new] -/ +def hashmap_hash_map_new_fwd (T : Type) : Result (hashmap_hash_map_t T) := + hashmap_hash_map_new_with_capacity_fwd T (Usize.ofInt 32 (by intlit)) + (Usize.ofInt 4 (by intlit)) (Usize.ofInt 5 (by intlit)) + +/- [hashmap_main::hashmap::HashMap::{0}::clear] -/ +divergent def hashmap_hash_map_clear_loop_fwd_back + (T : Type) (slots : Vec (hashmap_list_t T)) (i : Usize) : + Result (Vec (hashmap_list_t T)) + := + let i0 := vec_len (hashmap_list_t T) slots + if i < i0 + then + do + let i1 ← i + (Usize.ofInt 1 (by intlit)) + let slots0 ← + vec_index_mut_back (hashmap_list_t T) slots i hashmap_list_t.Nil + hashmap_hash_map_clear_loop_fwd_back T slots0 i1 + else Result.ret slots + +/- [hashmap_main::hashmap::HashMap::{0}::clear] -/ +def hashmap_hash_map_clear_fwd_back + (T : Type) (self : hashmap_hash_map_t T) : Result (hashmap_hash_map_t T) := + do + let v ← + hashmap_hash_map_clear_loop_fwd_back T self.hashmap_hash_map_slots + (Usize.ofInt 0 (by intlit)) + Result.ret + { + self + with + hashmap_hash_map_num_entries := (Usize.ofInt 0 (by intlit)), + hashmap_hash_map_slots := v + } + +/- [hashmap_main::hashmap::HashMap::{0}::len] -/ +def hashmap_hash_map_len_fwd + (T : Type) (self : hashmap_hash_map_t T) : Result Usize := + Result.ret self.hashmap_hash_map_num_entries + +/- [hashmap_main::hashmap::HashMap::{0}::insert_in_list] -/ +divergent def hashmap_hash_map_insert_in_list_loop_fwd + (T : Type) (key : Usize) (value : T) (ls : hashmap_list_t T) : Result Bool := + match h: ls with + | hashmap_list_t.Cons ckey cvalue tl => + if ckey = key + then Result.ret false + else hashmap_hash_map_insert_in_list_loop_fwd T key value tl + | hashmap_list_t.Nil => Result.ret true + +/- [hashmap_main::hashmap::HashMap::{0}::insert_in_list] -/ +def hashmap_hash_map_insert_in_list_fwd + (T : Type) (key : Usize) (value : T) (ls : hashmap_list_t T) : Result Bool := + hashmap_hash_map_insert_in_list_loop_fwd T key value ls + +/- [hashmap_main::hashmap::HashMap::{0}::insert_in_list] -/ +divergent def hashmap_hash_map_insert_in_list_loop_back + (T : Type) (key : Usize) (value : T) (ls : hashmap_list_t T) : + Result (hashmap_list_t T) + := + match h: ls with + | hashmap_list_t.Cons ckey cvalue tl => + if ckey = key + then Result.ret (hashmap_list_t.Cons ckey value tl) + else + do + let tl0 ← hashmap_hash_map_insert_in_list_loop_back T key value tl + Result.ret (hashmap_list_t.Cons ckey cvalue tl0) + | hashmap_list_t.Nil => + let l := hashmap_list_t.Nil + Result.ret (hashmap_list_t.Cons key value l) + +/- [hashmap_main::hashmap::HashMap::{0}::insert_in_list] -/ +def hashmap_hash_map_insert_in_list_back + (T : Type) (key : Usize) (value : T) (ls : hashmap_list_t T) : + Result (hashmap_list_t T) + := + hashmap_hash_map_insert_in_list_loop_back T key value ls + +/- [hashmap_main::hashmap::HashMap::{0}::insert_no_resize] -/ +def hashmap_hash_map_insert_no_resize_fwd_back + (T : Type) (self : hashmap_hash_map_t T) (key : Usize) (value : T) : + Result (hashmap_hash_map_t T) + := + do + let hash ← hashmap_hash_key_fwd key + let i := vec_len (hashmap_list_t T) self.hashmap_hash_map_slots + let hash_mod ← hash % i + let l ← + vec_index_mut_fwd (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod + let inserted ← hashmap_hash_map_insert_in_list_fwd T key value l + if inserted + then + do + let i0 ← self.hashmap_hash_map_num_entries + + (Usize.ofInt 1 (by intlit)) + let l0 ← hashmap_hash_map_insert_in_list_back T key value l + let v ← + vec_index_mut_back (hashmap_list_t T) self.hashmap_hash_map_slots + hash_mod l0 + Result.ret + { + self + with + hashmap_hash_map_num_entries := i0, hashmap_hash_map_slots := v + } + else + do + let l0 ← hashmap_hash_map_insert_in_list_back T key value l + let v ← + vec_index_mut_back (hashmap_list_t T) self.hashmap_hash_map_slots + hash_mod l0 + Result.ret { self with hashmap_hash_map_slots := v } + +/- [core::num::u32::{9}::MAX] -/ +def core_num_u32_max_body : Result U32 := + Result.ret (U32.ofInt 4294967295 (by intlit)) +def core_num_u32_max_c : U32 := eval_global core_num_u32_max_body (by simp) + +/- [hashmap_main::hashmap::HashMap::{0}::move_elements_from_list] -/ +divergent def hashmap_hash_map_move_elements_from_list_loop_fwd_back + (T : Type) (ntable : hashmap_hash_map_t T) (ls : hashmap_list_t T) : + Result (hashmap_hash_map_t T) + := + match h: ls with + | hashmap_list_t.Cons k v tl => + do + let ntable0 ← hashmap_hash_map_insert_no_resize_fwd_back T ntable k v + hashmap_hash_map_move_elements_from_list_loop_fwd_back T ntable0 tl + | hashmap_list_t.Nil => Result.ret ntable + +/- [hashmap_main::hashmap::HashMap::{0}::move_elements_from_list] -/ +def hashmap_hash_map_move_elements_from_list_fwd_back + (T : Type) (ntable : hashmap_hash_map_t T) (ls : hashmap_list_t T) : + Result (hashmap_hash_map_t T) + := + hashmap_hash_map_move_elements_from_list_loop_fwd_back T ntable ls + +/- [hashmap_main::hashmap::HashMap::{0}::move_elements] -/ +divergent def hashmap_hash_map_move_elements_loop_fwd_back + (T : Type) (ntable : hashmap_hash_map_t T) (slots : Vec (hashmap_list_t T)) + (i : Usize) : + Result ((hashmap_hash_map_t T) × (Vec (hashmap_list_t T))) + := + let i0 := vec_len (hashmap_list_t T) slots + if i < i0 + then + do + let l ← vec_index_mut_fwd (hashmap_list_t T) slots i + let ls := mem_replace_fwd (hashmap_list_t T) l hashmap_list_t.Nil + let ntable0 ← + hashmap_hash_map_move_elements_from_list_fwd_back T ntable ls + let i1 ← i + (Usize.ofInt 1 (by intlit)) + let l0 := mem_replace_back (hashmap_list_t T) l hashmap_list_t.Nil + let slots0 ← vec_index_mut_back (hashmap_list_t T) slots i l0 + hashmap_hash_map_move_elements_loop_fwd_back T ntable0 slots0 i1 + else Result.ret (ntable, slots) + +/- [hashmap_main::hashmap::HashMap::{0}::move_elements] -/ +def hashmap_hash_map_move_elements_fwd_back + (T : Type) (ntable : hashmap_hash_map_t T) (slots : Vec (hashmap_list_t T)) + (i : Usize) : + Result ((hashmap_hash_map_t T) × (Vec (hashmap_list_t T))) + := + hashmap_hash_map_move_elements_loop_fwd_back T ntable slots i + +/- [hashmap_main::hashmap::HashMap::{0}::try_resize] -/ +def hashmap_hash_map_try_resize_fwd_back + (T : Type) (self : hashmap_hash_map_t T) : Result (hashmap_hash_map_t T) := + do + let max_usize ← Scalar.cast .Usize core_num_u32_max_c + let capacity := vec_len (hashmap_list_t T) self.hashmap_hash_map_slots + let n1 ← max_usize / (Usize.ofInt 2 (by intlit)) + let (i, i0) := self.hashmap_hash_map_max_load_factor + let i1 ← n1 / i + if capacity <= i1 + then + do + let i2 ← capacity * (Usize.ofInt 2 (by intlit)) + let ntable ← hashmap_hash_map_new_with_capacity_fwd T i2 i i0 + let (ntable0, _) ← + hashmap_hash_map_move_elements_fwd_back T ntable + self.hashmap_hash_map_slots (Usize.ofInt 0 (by intlit)) + Result.ret + { + ntable0 + with + hashmap_hash_map_num_entries := self.hashmap_hash_map_num_entries, + hashmap_hash_map_max_load_factor := (i, i0) + } + else Result.ret { self with hashmap_hash_map_max_load_factor := (i, i0) } + +/- [hashmap_main::hashmap::HashMap::{0}::insert] -/ +def hashmap_hash_map_insert_fwd_back + (T : Type) (self : hashmap_hash_map_t T) (key : Usize) (value : T) : + Result (hashmap_hash_map_t T) + := + do + let self0 ← hashmap_hash_map_insert_no_resize_fwd_back T self key value + let i ← hashmap_hash_map_len_fwd T self0 + if i > self0.hashmap_hash_map_max_load + then hashmap_hash_map_try_resize_fwd_back T self0 + else Result.ret self0 + +/- [hashmap_main::hashmap::HashMap::{0}::contains_key_in_list] -/ +divergent def hashmap_hash_map_contains_key_in_list_loop_fwd + (T : Type) (key : Usize) (ls : hashmap_list_t T) : Result Bool := + match h: ls with + | hashmap_list_t.Cons ckey t tl => + if ckey = key + then Result.ret true + else hashmap_hash_map_contains_key_in_list_loop_fwd T key tl + | hashmap_list_t.Nil => Result.ret false + +/- [hashmap_main::hashmap::HashMap::{0}::contains_key_in_list] -/ +def hashmap_hash_map_contains_key_in_list_fwd + (T : Type) (key : Usize) (ls : hashmap_list_t T) : Result Bool := + hashmap_hash_map_contains_key_in_list_loop_fwd T key ls + +/- [hashmap_main::hashmap::HashMap::{0}::contains_key] -/ +def hashmap_hash_map_contains_key_fwd + (T : Type) (self : hashmap_hash_map_t T) (key : Usize) : Result Bool := + do + let hash ← hashmap_hash_key_fwd key + let i := vec_len (hashmap_list_t T) self.hashmap_hash_map_slots + let hash_mod ← hash % i + let l ← + vec_index_fwd (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod + hashmap_hash_map_contains_key_in_list_fwd T key l + +/- [hashmap_main::hashmap::HashMap::{0}::get_in_list] -/ +divergent def hashmap_hash_map_get_in_list_loop_fwd + (T : Type) (key : Usize) (ls : hashmap_list_t T) : Result T := + match h: ls with + | hashmap_list_t.Cons ckey cvalue tl => + if ckey = key + then Result.ret cvalue + else hashmap_hash_map_get_in_list_loop_fwd T key tl + | hashmap_list_t.Nil => Result.fail Error.panic + +/- [hashmap_main::hashmap::HashMap::{0}::get_in_list] -/ +def hashmap_hash_map_get_in_list_fwd + (T : Type) (key : Usize) (ls : hashmap_list_t T) : Result T := + hashmap_hash_map_get_in_list_loop_fwd T key ls + +/- [hashmap_main::hashmap::HashMap::{0}::get] -/ +def hashmap_hash_map_get_fwd + (T : Type) (self : hashmap_hash_map_t T) (key : Usize) : Result T := + do + let hash ← hashmap_hash_key_fwd key + let i := vec_len (hashmap_list_t T) self.hashmap_hash_map_slots + let hash_mod ← hash % i + let l ← + vec_index_fwd (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod + hashmap_hash_map_get_in_list_fwd T key l + +/- [hashmap_main::hashmap::HashMap::{0}::get_mut_in_list] -/ +divergent def hashmap_hash_map_get_mut_in_list_loop_fwd + (T : Type) (ls : hashmap_list_t T) (key : Usize) : Result T := + match h: ls with + | hashmap_list_t.Cons ckey cvalue tl => + if ckey = key + then Result.ret cvalue + else hashmap_hash_map_get_mut_in_list_loop_fwd T tl key + | hashmap_list_t.Nil => Result.fail Error.panic + +/- [hashmap_main::hashmap::HashMap::{0}::get_mut_in_list] -/ +def hashmap_hash_map_get_mut_in_list_fwd + (T : Type) (ls : hashmap_list_t T) (key : Usize) : Result T := + hashmap_hash_map_get_mut_in_list_loop_fwd T ls key + +/- [hashmap_main::hashmap::HashMap::{0}::get_mut_in_list] -/ +divergent def hashmap_hash_map_get_mut_in_list_loop_back + (T : Type) (ls : hashmap_list_t T) (key : Usize) (ret0 : T) : + Result (hashmap_list_t T) + := + match h: ls with + | hashmap_list_t.Cons ckey cvalue tl => + if ckey = key + then Result.ret (hashmap_list_t.Cons ckey ret0 tl) + else + do + let tl0 ← hashmap_hash_map_get_mut_in_list_loop_back T tl key ret0 + Result.ret (hashmap_list_t.Cons ckey cvalue tl0) + | hashmap_list_t.Nil => Result.fail Error.panic + +/- [hashmap_main::hashmap::HashMap::{0}::get_mut_in_list] -/ +def hashmap_hash_map_get_mut_in_list_back + (T : Type) (ls : hashmap_list_t T) (key : Usize) (ret0 : T) : + Result (hashmap_list_t T) + := + hashmap_hash_map_get_mut_in_list_loop_back T ls key ret0 + +/- [hashmap_main::hashmap::HashMap::{0}::get_mut] -/ +def hashmap_hash_map_get_mut_fwd + (T : Type) (self : hashmap_hash_map_t T) (key : Usize) : Result T := + do + let hash ← hashmap_hash_key_fwd key + let i := vec_len (hashmap_list_t T) self.hashmap_hash_map_slots + let hash_mod ← hash % i + let l ← + vec_index_mut_fwd (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod + hashmap_hash_map_get_mut_in_list_fwd T l key + +/- [hashmap_main::hashmap::HashMap::{0}::get_mut] -/ +def hashmap_hash_map_get_mut_back + (T : Type) (self : hashmap_hash_map_t T) (key : Usize) (ret0 : T) : + Result (hashmap_hash_map_t T) + := + do + let hash ← hashmap_hash_key_fwd key + let i := vec_len (hashmap_list_t T) self.hashmap_hash_map_slots + let hash_mod ← hash % i + let l ← + vec_index_mut_fwd (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod + let l0 ← hashmap_hash_map_get_mut_in_list_back T l key ret0 + let v ← + vec_index_mut_back (hashmap_list_t T) self.hashmap_hash_map_slots + hash_mod l0 + Result.ret { self with hashmap_hash_map_slots := v } + +/- [hashmap_main::hashmap::HashMap::{0}::remove_from_list] -/ +divergent def hashmap_hash_map_remove_from_list_loop_fwd + (T : Type) (key : Usize) (ls : hashmap_list_t T) : Result (Option T) := + match h: ls with + | hashmap_list_t.Cons ckey t tl => + if ckey = key + then + let mv_ls := + mem_replace_fwd (hashmap_list_t T) (hashmap_list_t.Cons ckey t tl) + hashmap_list_t.Nil + match h: mv_ls with + | hashmap_list_t.Cons i cvalue tl0 => Result.ret (Option.some cvalue) + | hashmap_list_t.Nil => Result.fail Error.panic + else hashmap_hash_map_remove_from_list_loop_fwd T key tl + | hashmap_list_t.Nil => Result.ret Option.none + +/- [hashmap_main::hashmap::HashMap::{0}::remove_from_list] -/ +def hashmap_hash_map_remove_from_list_fwd + (T : Type) (key : Usize) (ls : hashmap_list_t T) : Result (Option T) := + hashmap_hash_map_remove_from_list_loop_fwd T key ls + +/- [hashmap_main::hashmap::HashMap::{0}::remove_from_list] -/ +divergent def hashmap_hash_map_remove_from_list_loop_back + (T : Type) (key : Usize) (ls : hashmap_list_t T) : + Result (hashmap_list_t T) + := + match h: ls with + | hashmap_list_t.Cons ckey t tl => + if ckey = key + then + let mv_ls := + mem_replace_fwd (hashmap_list_t T) (hashmap_list_t.Cons ckey t tl) + hashmap_list_t.Nil + match h: mv_ls with + | hashmap_list_t.Cons i cvalue tl0 => Result.ret tl0 + | hashmap_list_t.Nil => Result.fail Error.panic + else + do + let tl0 ← hashmap_hash_map_remove_from_list_loop_back T key tl + Result.ret (hashmap_list_t.Cons ckey t tl0) + | hashmap_list_t.Nil => Result.ret hashmap_list_t.Nil + +/- [hashmap_main::hashmap::HashMap::{0}::remove_from_list] -/ +def hashmap_hash_map_remove_from_list_back + (T : Type) (key : Usize) (ls : hashmap_list_t T) : + Result (hashmap_list_t T) + := + hashmap_hash_map_remove_from_list_loop_back T key ls + +/- [hashmap_main::hashmap::HashMap::{0}::remove] -/ +def hashmap_hash_map_remove_fwd + (T : Type) (self : hashmap_hash_map_t T) (key : Usize) : Result (Option T) := + do + let hash ← hashmap_hash_key_fwd key + let i := vec_len (hashmap_list_t T) self.hashmap_hash_map_slots + let hash_mod ← hash % i + let l ← + vec_index_mut_fwd (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod + let x ← hashmap_hash_map_remove_from_list_fwd T key l + match h: x with + | Option.none => Result.ret Option.none + | Option.some x0 => + do + let _ ← self.hashmap_hash_map_num_entries - + (Usize.ofInt 1 (by intlit)) + Result.ret (Option.some x0) + +/- [hashmap_main::hashmap::HashMap::{0}::remove] -/ +def hashmap_hash_map_remove_back + (T : Type) (self : hashmap_hash_map_t T) (key : Usize) : + Result (hashmap_hash_map_t T) + := + do + let hash ← hashmap_hash_key_fwd key + let i := vec_len (hashmap_list_t T) self.hashmap_hash_map_slots + let hash_mod ← hash % i + let l ← + vec_index_mut_fwd (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod + let x ← hashmap_hash_map_remove_from_list_fwd T key l + match h: x with + | Option.none => + do + let l0 ← hashmap_hash_map_remove_from_list_back T key l + let v ← + vec_index_mut_back (hashmap_list_t T) self.hashmap_hash_map_slots + hash_mod l0 + Result.ret { self with hashmap_hash_map_slots := v } + | Option.some x0 => + do + let i0 ← self.hashmap_hash_map_num_entries - + (Usize.ofInt 1 (by intlit)) + let l0 ← hashmap_hash_map_remove_from_list_back T key l + let v ← + vec_index_mut_back (hashmap_list_t T) self.hashmap_hash_map_slots + hash_mod l0 + Result.ret + { + self + with + hashmap_hash_map_num_entries := i0, hashmap_hash_map_slots := v + } + +/- [hashmap_main::hashmap::test1] -/ +def hashmap_test1_fwd : Result Unit := + do + let hm ← hashmap_hash_map_new_fwd U64 + let hm0 ← + hashmap_hash_map_insert_fwd_back U64 hm (Usize.ofInt 0 (by intlit)) + (U64.ofInt 42 (by intlit)) + let hm1 ← + hashmap_hash_map_insert_fwd_back U64 hm0 (Usize.ofInt 128 (by intlit)) + (U64.ofInt 18 (by intlit)) + let hm2 ← + hashmap_hash_map_insert_fwd_back U64 hm1 (Usize.ofInt 1024 (by intlit)) + (U64.ofInt 138 (by intlit)) + let hm3 ← + hashmap_hash_map_insert_fwd_back U64 hm2 (Usize.ofInt 1056 (by intlit)) + (U64.ofInt 256 (by intlit)) + let i ← hashmap_hash_map_get_fwd U64 hm3 (Usize.ofInt 128 (by intlit)) + if not (i = (U64.ofInt 18 (by intlit))) + then Result.fail Error.panic + else + do + let hm4 ← + hashmap_hash_map_get_mut_back U64 hm3 (Usize.ofInt 1024 (by intlit)) + (U64.ofInt 56 (by intlit)) + let i0 ← + hashmap_hash_map_get_fwd U64 hm4 (Usize.ofInt 1024 (by intlit)) + if not (i0 = (U64.ofInt 56 (by intlit))) + then Result.fail Error.panic + else + do + let x ← + hashmap_hash_map_remove_fwd U64 hm4 + (Usize.ofInt 1024 (by intlit)) + match h: x with + | Option.none => Result.fail Error.panic + | Option.some x0 => + if not (x0 = (U64.ofInt 56 (by intlit))) + then Result.fail Error.panic + else + do + let hm5 ← + hashmap_hash_map_remove_back U64 hm4 + (Usize.ofInt 1024 (by intlit)) + let i1 ← + hashmap_hash_map_get_fwd U64 hm5 + (Usize.ofInt 0 (by intlit)) + if not (i1 = (U64.ofInt 42 (by intlit))) + then Result.fail Error.panic + else + do + let i2 ← + hashmap_hash_map_get_fwd U64 hm5 + (Usize.ofInt 128 (by intlit)) + if not (i2 = (U64.ofInt 18 (by intlit))) + then Result.fail Error.panic + else + do + let i3 ← + hashmap_hash_map_get_fwd U64 hm5 + (Usize.ofInt 1056 (by intlit)) + if not (i3 = (U64.ofInt 256 (by intlit))) + then Result.fail Error.panic + else Result.ret () + +/- Unit test for [hashmap_main::hashmap::test1] -/ +#assert (hashmap_test1_fwd == .ret ()) + +/- [hashmap_main::insert_on_disk] -/ +def insert_on_disk_fwd + (key : Usize) (value : U64) (st : State) : Result (State × Unit) := + do + let (st0, hm) ← opaque_defs.hashmap_utils_deserialize_fwd st + let hm0 ← hashmap_hash_map_insert_fwd_back U64 hm key value + let (st1, _) ← opaque_defs.hashmap_utils_serialize_fwd hm0 st0 + Result.ret (st1, ()) + +/- [hashmap_main::main] -/ +def main_fwd : Result Unit := + Result.ret () + +/- Unit test for [hashmap_main::main] -/ +#assert (main_fwd == .ret ()) + diff --git a/tests/lean/HashmapMain/Opaque.lean b/tests/lean/HashmapMain/Opaque.lean new file mode 100644 index 00000000..bef4f3fb --- /dev/null +++ b/tests/lean/HashmapMain/Opaque.lean @@ -0,0 +1,16 @@ +-- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS +-- [hashmap_main]: opaque function definitions +import Base +import HashmapMain.Types +open Primitives + +structure OpaqueDefs where + + /- [hashmap_main::hashmap_utils::deserialize] -/ + hashmap_utils_deserialize_fwd + : State -> Result (State × (hashmap_hash_map_t U64)) + + /- [hashmap_main::hashmap_utils::serialize] -/ + hashmap_utils_serialize_fwd + : hashmap_hash_map_t U64 -> State -> Result (State × Unit) + diff --git a/tests/lean/HashmapMain/Types.lean b/tests/lean/HashmapMain/Types.lean new file mode 100644 index 00000000..858e1c51 --- /dev/null +++ b/tests/lean/HashmapMain/Types.lean @@ -0,0 +1,20 @@ +-- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS +-- [hashmap_main]: type definitions +import Base +open Primitives + +/- [hashmap_main::hashmap::List] -/ +inductive hashmap_list_t (T : Type) := +| Cons : Usize -> T -> hashmap_list_t T -> hashmap_list_t T +| Nil : hashmap_list_t T + +/- [hashmap_main::hashmap::HashMap] -/ +structure hashmap_hash_map_t (T : Type) where + hashmap_hash_map_num_entries : Usize + hashmap_hash_map_max_load_factor : (Usize × Usize) + hashmap_hash_map_max_load : Usize + hashmap_hash_map_slots : Vec (hashmap_list_t T) + +/- The state type used in the state-error monad -/ +axiom State : Type + diff --git a/tests/lean/Loops.lean b/tests/lean/Loops.lean new file mode 100644 index 00000000..60c73776 --- /dev/null +++ b/tests/lean/Loops.lean @@ -0,0 +1 @@ +import Loops.Funs diff --git a/tests/lean/Loops/Funs.lean b/tests/lean/Loops/Funs.lean new file mode 100644 index 00000000..7d5f7595 --- /dev/null +++ b/tests/lean/Loops/Funs.lean @@ -0,0 +1,624 @@ +-- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS +-- [loops]: function definitions +import Base +import Loops.Types +open Primitives + +/- [loops::sum] -/ +divergent def sum_loop_fwd (max : U32) (i : U32) (s : U32) : Result U32 := + if i < max + then + do + let s0 ← s + i + let i0 ← i + (U32.ofInt 1 (by intlit)) + sum_loop_fwd max i0 s0 + else s * (U32.ofInt 2 (by intlit)) + +/- [loops::sum] -/ +def sum_fwd (max : U32) : Result U32 := + sum_loop_fwd max (U32.ofInt 0 (by intlit)) (U32.ofInt 0 (by intlit)) + +/- [loops::sum_with_mut_borrows] -/ +divergent def sum_with_mut_borrows_loop_fwd + (max : U32) (mi : U32) (ms : U32) : Result U32 := + if mi < max + then + do + let ms0 ← ms + mi + let mi0 ← mi + (U32.ofInt 1 (by intlit)) + sum_with_mut_borrows_loop_fwd max mi0 ms0 + else ms * (U32.ofInt 2 (by intlit)) + +/- [loops::sum_with_mut_borrows] -/ +def sum_with_mut_borrows_fwd (max : U32) : Result U32 := + sum_with_mut_borrows_loop_fwd max (U32.ofInt 0 (by intlit)) + (U32.ofInt 0 (by intlit)) + +/- [loops::sum_with_shared_borrows] -/ +divergent def sum_with_shared_borrows_loop_fwd + (max : U32) (i : U32) (s : U32) : Result U32 := + if i < max + then + do + let i0 ← i + (U32.ofInt 1 (by intlit)) + let s0 ← s + i0 + sum_with_shared_borrows_loop_fwd max i0 s0 + else s * (U32.ofInt 2 (by intlit)) + +/- [loops::sum_with_shared_borrows] -/ +def sum_with_shared_borrows_fwd (max : U32) : Result U32 := + sum_with_shared_borrows_loop_fwd max (U32.ofInt 0 (by intlit)) + (U32.ofInt 0 (by intlit)) + +/- [loops::clear] -/ +divergent def clear_loop_fwd_back + (v : Vec U32) (i : Usize) : Result (Vec U32) := + let i0 := vec_len U32 v + if i < i0 + then + do + let i1 ← i + (Usize.ofInt 1 (by intlit)) + let v0 ← vec_index_mut_back U32 v i (U32.ofInt 0 (by intlit)) + clear_loop_fwd_back v0 i1 + else Result.ret v + +/- [loops::clear] -/ +def clear_fwd_back (v : Vec U32) : Result (Vec U32) := + clear_loop_fwd_back v (Usize.ofInt 0 (by intlit)) + +/- [loops::list_mem] -/ +divergent def list_mem_loop_fwd (x : U32) (ls : list_t U32) : Result Bool := + match h: ls with + | list_t.Cons y tl => + if y = x + then Result.ret true + else list_mem_loop_fwd x tl + | list_t.Nil => Result.ret false + +/- [loops::list_mem] -/ +def list_mem_fwd (x : U32) (ls : list_t U32) : Result Bool := + list_mem_loop_fwd x ls + +/- [loops::list_nth_mut_loop] -/ +divergent def list_nth_mut_loop_loop_fwd + (T : Type) (ls : list_t T) (i : U32) : Result T := + match h: ls with + | list_t.Cons x tl => + if i = (U32.ofInt 0 (by intlit)) + then Result.ret x + else + do + let i0 ← i - (U32.ofInt 1 (by intlit)) + list_nth_mut_loop_loop_fwd T tl i0 + | list_t.Nil => Result.fail Error.panic + +/- [loops::list_nth_mut_loop] -/ +def list_nth_mut_loop_fwd (T : Type) (ls : list_t T) (i : U32) : Result T := + list_nth_mut_loop_loop_fwd T ls i + +/- [loops::list_nth_mut_loop] -/ +divergent def list_nth_mut_loop_loop_back + (T : Type) (ls : list_t T) (i : U32) (ret0 : T) : Result (list_t T) := + match h: ls with + | list_t.Cons x tl => + if i = (U32.ofInt 0 (by intlit)) + then Result.ret (list_t.Cons ret0 tl) + else + do + let i0 ← i - (U32.ofInt 1 (by intlit)) + let tl0 ← list_nth_mut_loop_loop_back T tl i0 ret0 + Result.ret (list_t.Cons x tl0) + | list_t.Nil => Result.fail Error.panic + +/- [loops::list_nth_mut_loop] -/ +def list_nth_mut_loop_back + (T : Type) (ls : list_t T) (i : U32) (ret0 : T) : Result (list_t T) := + list_nth_mut_loop_loop_back T ls i ret0 + +/- [loops::list_nth_shared_loop] -/ +divergent def list_nth_shared_loop_loop_fwd + (T : Type) (ls : list_t T) (i : U32) : Result T := + match h: ls with + | list_t.Cons x tl => + if i = (U32.ofInt 0 (by intlit)) + then Result.ret x + else + do + let i0 ← i - (U32.ofInt 1 (by intlit)) + list_nth_shared_loop_loop_fwd T tl i0 + | list_t.Nil => Result.fail Error.panic + +/- [loops::list_nth_shared_loop] -/ +def list_nth_shared_loop_fwd (T : Type) (ls : list_t T) (i : U32) : Result T := + list_nth_shared_loop_loop_fwd T ls i + +/- [loops::get_elem_mut] -/ +divergent def get_elem_mut_loop_fwd + (x : Usize) (ls : list_t Usize) : Result Usize := + match h: ls with + | list_t.Cons y tl => + if y = x + then Result.ret y + else get_elem_mut_loop_fwd x tl + | list_t.Nil => Result.fail Error.panic + +/- [loops::get_elem_mut] -/ +def get_elem_mut_fwd (slots : Vec (list_t Usize)) (x : Usize) : Result Usize := + do + let l ← + vec_index_mut_fwd (list_t Usize) slots (Usize.ofInt 0 (by intlit)) + get_elem_mut_loop_fwd x l + +/- [loops::get_elem_mut] -/ +divergent def get_elem_mut_loop_back + (x : Usize) (ls : list_t Usize) (ret0 : Usize) : Result (list_t Usize) := + match h: ls with + | list_t.Cons y tl => + if y = x + then Result.ret (list_t.Cons ret0 tl) + else + do + let tl0 ← get_elem_mut_loop_back x tl ret0 + Result.ret (list_t.Cons y tl0) + | list_t.Nil => Result.fail Error.panic + +/- [loops::get_elem_mut] -/ +def get_elem_mut_back + (slots : Vec (list_t Usize)) (x : Usize) (ret0 : Usize) : + Result (Vec (list_t Usize)) + := + do + let l ← + vec_index_mut_fwd (list_t Usize) slots (Usize.ofInt 0 (by intlit)) + let l0 ← get_elem_mut_loop_back x l ret0 + vec_index_mut_back (list_t Usize) slots (Usize.ofInt 0 (by intlit)) l0 + +/- [loops::get_elem_shared] -/ +divergent def get_elem_shared_loop_fwd + (x : Usize) (ls : list_t Usize) : Result Usize := + match h: ls with + | list_t.Cons y tl => + if y = x + then Result.ret y + else get_elem_shared_loop_fwd x tl + | list_t.Nil => Result.fail Error.panic + +/- [loops::get_elem_shared] -/ +def get_elem_shared_fwd + (slots : Vec (list_t Usize)) (x : Usize) : Result Usize := + do + let l ← vec_index_fwd (list_t Usize) slots (Usize.ofInt 0 (by intlit)) + get_elem_shared_loop_fwd x l + +/- [loops::id_mut] -/ +def id_mut_fwd (T : Type) (ls : list_t T) : Result (list_t T) := + Result.ret ls + +/- [loops::id_mut] -/ +def id_mut_back + (T : Type) (ls : list_t T) (ret0 : list_t T) : Result (list_t T) := + Result.ret ret0 + +/- [loops::id_shared] -/ +def id_shared_fwd (T : Type) (ls : list_t T) : Result (list_t T) := + Result.ret ls + +/- [loops::list_nth_mut_loop_with_id] -/ +divergent def list_nth_mut_loop_with_id_loop_fwd + (T : Type) (i : U32) (ls : list_t T) : Result T := + match h: ls with + | list_t.Cons x tl => + if i = (U32.ofInt 0 (by intlit)) + then Result.ret x + else + do + let i0 ← i - (U32.ofInt 1 (by intlit)) + list_nth_mut_loop_with_id_loop_fwd T i0 tl + | list_t.Nil => Result.fail Error.panic + +/- [loops::list_nth_mut_loop_with_id] -/ +def list_nth_mut_loop_with_id_fwd + (T : Type) (ls : list_t T) (i : U32) : Result T := + do + let ls0 ← id_mut_fwd T ls + list_nth_mut_loop_with_id_loop_fwd T i ls0 + +/- [loops::list_nth_mut_loop_with_id] -/ +divergent def list_nth_mut_loop_with_id_loop_back + (T : Type) (i : U32) (ls : list_t T) (ret0 : T) : Result (list_t T) := + match h: ls with + | list_t.Cons x tl => + if i = (U32.ofInt 0 (by intlit)) + then Result.ret (list_t.Cons ret0 tl) + else + do + let i0 ← i - (U32.ofInt 1 (by intlit)) + let tl0 ← list_nth_mut_loop_with_id_loop_back T i0 tl ret0 + Result.ret (list_t.Cons x tl0) + | list_t.Nil => Result.fail Error.panic + +/- [loops::list_nth_mut_loop_with_id] -/ +def list_nth_mut_loop_with_id_back + (T : Type) (ls : list_t T) (i : U32) (ret0 : T) : Result (list_t T) := + do + let ls0 ← id_mut_fwd T ls + let l ← list_nth_mut_loop_with_id_loop_back T i ls0 ret0 + id_mut_back T ls l + +/- [loops::list_nth_shared_loop_with_id] -/ +divergent def list_nth_shared_loop_with_id_loop_fwd + (T : Type) (i : U32) (ls : list_t T) : Result T := + match h: ls with + | list_t.Cons x tl => + if i = (U32.ofInt 0 (by intlit)) + then Result.ret x + else + do + let i0 ← i - (U32.ofInt 1 (by intlit)) + list_nth_shared_loop_with_id_loop_fwd T i0 tl + | list_t.Nil => Result.fail Error.panic + +/- [loops::list_nth_shared_loop_with_id] -/ +def list_nth_shared_loop_with_id_fwd + (T : Type) (ls : list_t T) (i : U32) : Result T := + do + let ls0 ← id_shared_fwd T ls + list_nth_shared_loop_with_id_loop_fwd T i ls0 + +/- [loops::list_nth_mut_loop_pair] -/ +divergent def list_nth_mut_loop_pair_loop_fwd + (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := + match h: ls0 with + | list_t.Cons x0 tl0 => + match h: ls1 with + | list_t.Cons x1 tl1 => + if i = (U32.ofInt 0 (by intlit)) + then Result.ret (x0, x1) + else + do + let i0 ← i - (U32.ofInt 1 (by intlit)) + list_nth_mut_loop_pair_loop_fwd T tl0 tl1 i0 + | list_t.Nil => Result.fail Error.panic + | list_t.Nil => Result.fail Error.panic + +/- [loops::list_nth_mut_loop_pair] -/ +def list_nth_mut_loop_pair_fwd + (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := + list_nth_mut_loop_pair_loop_fwd T ls0 ls1 i + +/- [loops::list_nth_mut_loop_pair] -/ +divergent def list_nth_mut_loop_pair_loop_back'a + (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : + Result (list_t T) + := + match h: ls0 with + | list_t.Cons x0 tl0 => + match h: ls1 with + | list_t.Cons x1 tl1 => + if i = (U32.ofInt 0 (by intlit)) + then Result.ret (list_t.Cons ret0 tl0) + else + do + let i0 ← i - (U32.ofInt 1 (by intlit)) + let tl00 ← list_nth_mut_loop_pair_loop_back'a T tl0 tl1 i0 ret0 + Result.ret (list_t.Cons x0 tl00) + | list_t.Nil => Result.fail Error.panic + | list_t.Nil => Result.fail Error.panic + +/- [loops::list_nth_mut_loop_pair] -/ +def list_nth_mut_loop_pair_back'a + (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : + Result (list_t T) + := + list_nth_mut_loop_pair_loop_back'a T ls0 ls1 i ret0 + +/- [loops::list_nth_mut_loop_pair] -/ +divergent def list_nth_mut_loop_pair_loop_back'b + (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : + Result (list_t T) + := + match h: ls0 with + | list_t.Cons x0 tl0 => + match h: ls1 with + | list_t.Cons x1 tl1 => + if i = (U32.ofInt 0 (by intlit)) + then Result.ret (list_t.Cons ret0 tl1) + else + do + let i0 ← i - (U32.ofInt 1 (by intlit)) + let tl10 ← list_nth_mut_loop_pair_loop_back'b T tl0 tl1 i0 ret0 + Result.ret (list_t.Cons x1 tl10) + | list_t.Nil => Result.fail Error.panic + | list_t.Nil => Result.fail Error.panic + +/- [loops::list_nth_mut_loop_pair] -/ +def list_nth_mut_loop_pair_back'b + (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : + Result (list_t T) + := + list_nth_mut_loop_pair_loop_back'b T ls0 ls1 i ret0 + +/- [loops::list_nth_shared_loop_pair] -/ +divergent def list_nth_shared_loop_pair_loop_fwd + (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := + match h: ls0 with + | list_t.Cons x0 tl0 => + match h: ls1 with + | list_t.Cons x1 tl1 => + if i = (U32.ofInt 0 (by intlit)) + then Result.ret (x0, x1) + else + do + let i0 ← i - (U32.ofInt 1 (by intlit)) + list_nth_shared_loop_pair_loop_fwd T tl0 tl1 i0 + | list_t.Nil => Result.fail Error.panic + | list_t.Nil => Result.fail Error.panic + +/- [loops::list_nth_shared_loop_pair] -/ +def list_nth_shared_loop_pair_fwd + (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := + list_nth_shared_loop_pair_loop_fwd T ls0 ls1 i + +/- [loops::list_nth_mut_loop_pair_merge] -/ +divergent def list_nth_mut_loop_pair_merge_loop_fwd + (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := + match h: ls0 with + | list_t.Cons x0 tl0 => + match h: ls1 with + | list_t.Cons x1 tl1 => + if i = (U32.ofInt 0 (by intlit)) + then Result.ret (x0, x1) + else + do + let i0 ← i - (U32.ofInt 1 (by intlit)) + list_nth_mut_loop_pair_merge_loop_fwd T tl0 tl1 i0 + | list_t.Nil => Result.fail Error.panic + | list_t.Nil => Result.fail Error.panic + +/- [loops::list_nth_mut_loop_pair_merge] -/ +def list_nth_mut_loop_pair_merge_fwd + (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := + list_nth_mut_loop_pair_merge_loop_fwd T ls0 ls1 i + +/- [loops::list_nth_mut_loop_pair_merge] -/ +divergent def list_nth_mut_loop_pair_merge_loop_back + (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : (T × T)) : + Result ((list_t T) × (list_t T)) + := + match h: ls0 with + | list_t.Cons x0 tl0 => + match h: ls1 with + | list_t.Cons x1 tl1 => + if i = (U32.ofInt 0 (by intlit)) + then + let (t, t0) := ret0 + Result.ret (list_t.Cons t tl0, list_t.Cons t0 tl1) + else + do + let i0 ← i - (U32.ofInt 1 (by intlit)) + let (tl00, tl10) ← + list_nth_mut_loop_pair_merge_loop_back T tl0 tl1 i0 ret0 + Result.ret (list_t.Cons x0 tl00, list_t.Cons x1 tl10) + | list_t.Nil => Result.fail Error.panic + | list_t.Nil => Result.fail Error.panic + +/- [loops::list_nth_mut_loop_pair_merge] -/ +def list_nth_mut_loop_pair_merge_back + (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : (T × T)) : + Result ((list_t T) × (list_t T)) + := + list_nth_mut_loop_pair_merge_loop_back T ls0 ls1 i ret0 + +/- [loops::list_nth_shared_loop_pair_merge] -/ +divergent def list_nth_shared_loop_pair_merge_loop_fwd + (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := + match h: ls0 with + | list_t.Cons x0 tl0 => + match h: ls1 with + | list_t.Cons x1 tl1 => + if i = (U32.ofInt 0 (by intlit)) + then Result.ret (x0, x1) + else + do + let i0 ← i - (U32.ofInt 1 (by intlit)) + list_nth_shared_loop_pair_merge_loop_fwd T tl0 tl1 i0 + | list_t.Nil => Result.fail Error.panic + | list_t.Nil => Result.fail Error.panic + +/- [loops::list_nth_shared_loop_pair_merge] -/ +def list_nth_shared_loop_pair_merge_fwd + (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := + list_nth_shared_loop_pair_merge_loop_fwd T ls0 ls1 i + +/- [loops::list_nth_mut_shared_loop_pair] -/ +divergent def list_nth_mut_shared_loop_pair_loop_fwd + (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := + match h: ls0 with + | list_t.Cons x0 tl0 => + match h: ls1 with + | list_t.Cons x1 tl1 => + if i = (U32.ofInt 0 (by intlit)) + then Result.ret (x0, x1) + else + do + let i0 ← i - (U32.ofInt 1 (by intlit)) + list_nth_mut_shared_loop_pair_loop_fwd T tl0 tl1 i0 + | list_t.Nil => Result.fail Error.panic + | list_t.Nil => Result.fail Error.panic + +/- [loops::list_nth_mut_shared_loop_pair] -/ +def list_nth_mut_shared_loop_pair_fwd + (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := + list_nth_mut_shared_loop_pair_loop_fwd T ls0 ls1 i + +/- [loops::list_nth_mut_shared_loop_pair] -/ +divergent def list_nth_mut_shared_loop_pair_loop_back + (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : + Result (list_t T) + := + match h: ls0 with + | list_t.Cons x0 tl0 => + match h: ls1 with + | list_t.Cons x1 tl1 => + if i = (U32.ofInt 0 (by intlit)) + then Result.ret (list_t.Cons ret0 tl0) + else + do + let i0 ← i - (U32.ofInt 1 (by intlit)) + let tl00 ← + list_nth_mut_shared_loop_pair_loop_back T tl0 tl1 i0 ret0 + Result.ret (list_t.Cons x0 tl00) + | list_t.Nil => Result.fail Error.panic + | list_t.Nil => Result.fail Error.panic + +/- [loops::list_nth_mut_shared_loop_pair] -/ +def list_nth_mut_shared_loop_pair_back + (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : + Result (list_t T) + := + list_nth_mut_shared_loop_pair_loop_back T ls0 ls1 i ret0 + +/- [loops::list_nth_mut_shared_loop_pair_merge] -/ +divergent def list_nth_mut_shared_loop_pair_merge_loop_fwd + (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := + match h: ls0 with + | list_t.Cons x0 tl0 => + match h: ls1 with + | list_t.Cons x1 tl1 => + if i = (U32.ofInt 0 (by intlit)) + then Result.ret (x0, x1) + else + do + let i0 ← i - (U32.ofInt 1 (by intlit)) + list_nth_mut_shared_loop_pair_merge_loop_fwd T tl0 tl1 i0 + | list_t.Nil => Result.fail Error.panic + | list_t.Nil => Result.fail Error.panic + +/- [loops::list_nth_mut_shared_loop_pair_merge] -/ +def list_nth_mut_shared_loop_pair_merge_fwd + (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := + list_nth_mut_shared_loop_pair_merge_loop_fwd T ls0 ls1 i + +/- [loops::list_nth_mut_shared_loop_pair_merge] -/ +divergent def list_nth_mut_shared_loop_pair_merge_loop_back + (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : + Result (list_t T) + := + match h: ls0 with + | list_t.Cons x0 tl0 => + match h: ls1 with + | list_t.Cons x1 tl1 => + if i = (U32.ofInt 0 (by intlit)) + then Result.ret (list_t.Cons ret0 tl0) + else + do + let i0 ← i - (U32.ofInt 1 (by intlit)) + let tl00 ← + list_nth_mut_shared_loop_pair_merge_loop_back T tl0 tl1 i0 ret0 + Result.ret (list_t.Cons x0 tl00) + | list_t.Nil => Result.fail Error.panic + | list_t.Nil => Result.fail Error.panic + +/- [loops::list_nth_mut_shared_loop_pair_merge] -/ +def list_nth_mut_shared_loop_pair_merge_back + (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : + Result (list_t T) + := + list_nth_mut_shared_loop_pair_merge_loop_back T ls0 ls1 i ret0 + +/- [loops::list_nth_shared_mut_loop_pair] -/ +divergent def list_nth_shared_mut_loop_pair_loop_fwd + (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := + match h: ls0 with + | list_t.Cons x0 tl0 => + match h: ls1 with + | list_t.Cons x1 tl1 => + if i = (U32.ofInt 0 (by intlit)) + then Result.ret (x0, x1) + else + do + let i0 ← i - (U32.ofInt 1 (by intlit)) + list_nth_shared_mut_loop_pair_loop_fwd T tl0 tl1 i0 + | list_t.Nil => Result.fail Error.panic + | list_t.Nil => Result.fail Error.panic + +/- [loops::list_nth_shared_mut_loop_pair] -/ +def list_nth_shared_mut_loop_pair_fwd + (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := + list_nth_shared_mut_loop_pair_loop_fwd T ls0 ls1 i + +/- [loops::list_nth_shared_mut_loop_pair] -/ +divergent def list_nth_shared_mut_loop_pair_loop_back + (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : + Result (list_t T) + := + match h: ls0 with + | list_t.Cons x0 tl0 => + match h: ls1 with + | list_t.Cons x1 tl1 => + if i = (U32.ofInt 0 (by intlit)) + then Result.ret (list_t.Cons ret0 tl1) + else + do + let i0 ← i - (U32.ofInt 1 (by intlit)) + let tl10 ← + list_nth_shared_mut_loop_pair_loop_back T tl0 tl1 i0 ret0 + Result.ret (list_t.Cons x1 tl10) + | list_t.Nil => Result.fail Error.panic + | list_t.Nil => Result.fail Error.panic + +/- [loops::list_nth_shared_mut_loop_pair] -/ +def list_nth_shared_mut_loop_pair_back + (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : + Result (list_t T) + := + list_nth_shared_mut_loop_pair_loop_back T ls0 ls1 i ret0 + +/- [loops::list_nth_shared_mut_loop_pair_merge] -/ +divergent def list_nth_shared_mut_loop_pair_merge_loop_fwd + (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := + match h: ls0 with + | list_t.Cons x0 tl0 => + match h: ls1 with + | list_t.Cons x1 tl1 => + if i = (U32.ofInt 0 (by intlit)) + then Result.ret (x0, x1) + else + do + let i0 ← i - (U32.ofInt 1 (by intlit)) + list_nth_shared_mut_loop_pair_merge_loop_fwd T tl0 tl1 i0 + | list_t.Nil => Result.fail Error.panic + | list_t.Nil => Result.fail Error.panic + +/- [loops::list_nth_shared_mut_loop_pair_merge] -/ +def list_nth_shared_mut_loop_pair_merge_fwd + (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := + list_nth_shared_mut_loop_pair_merge_loop_fwd T ls0 ls1 i + +/- [loops::list_nth_shared_mut_loop_pair_merge] -/ +divergent def list_nth_shared_mut_loop_pair_merge_loop_back + (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : + Result (list_t T) + := + match h: ls0 with + | list_t.Cons x0 tl0 => + match h: ls1 with + | list_t.Cons x1 tl1 => + if i = (U32.ofInt 0 (by intlit)) + then Result.ret (list_t.Cons ret0 tl1) + else + do + let i0 ← i - (U32.ofInt 1 (by intlit)) + let tl10 ← + list_nth_shared_mut_loop_pair_merge_loop_back T tl0 tl1 i0 ret0 + Result.ret (list_t.Cons x1 tl10) + | list_t.Nil => Result.fail Error.panic + | list_t.Nil => Result.fail Error.panic + +/- [loops::list_nth_shared_mut_loop_pair_merge] -/ +def list_nth_shared_mut_loop_pair_merge_back + (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : + Result (list_t T) + := + list_nth_shared_mut_loop_pair_merge_loop_back T ls0 ls1 i ret0 + diff --git a/tests/lean/Loops/Types.lean b/tests/lean/Loops/Types.lean new file mode 100644 index 00000000..e14f9766 --- /dev/null +++ b/tests/lean/Loops/Types.lean @@ -0,0 +1,10 @@ +-- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS +-- [loops]: type definitions +import Base +open Primitives + +/- [loops::List] -/ +inductive list_t (T : Type) := +| Cons : T -> list_t T -> list_t T +| Nil : list_t T + diff --git a/tests/lean/Makefile b/tests/lean/Makefile index ed3b3e3b..3ccfbec2 100644 --- a/tests/lean/Makefile +++ b/tests/lean/Makefile @@ -1,40 +1,35 @@ ALL_DIRS ?= $(filter-out %~ lean-toolchain% Makefile%, $(wildcard *)) +# TODO: remove UPDATE_DIRS = $(addprefix update-,$(ALL_DIRS)) +# TODO: remove VERIFY_DIRS = $(addprefix verif-,$(ALL_DIRS)) +# TODO: remove CLEAN_DIRS = $(addprefix clean-,$(ALL_DIRS)) +# TODO: remove COPY_LEAN_TOOLCHAIN = $(addprefix copy-lean-toolchain-,$(ALL_DIRS)) .PHONY: all all: prepare-projects verify .PHONY: prepare-projects -prepare-projects: $(COPY_LEAN_TOOLCHAIN) +prepare-projects: copy-lean-toolchain .PHONY: prepare-projects -copy-lean-toolchain-%: - cp lean-toolchain $* +copy-lean-toolchain: + cp ../../backends/lean/lean-toolchain . .PHONY: update -update: $(UPDATE_DIRS) - -.PHONY: update-% -update-%: - cd $* && lake update +update: + lake update .PHONY: verify -verify: $(VERIFY_DIRS) - -.PHONY: verif-% -verif-%: - cd $* && lake build +verify: + lake build .PHONY: clean -clean: $(CLEAN_DIRS) - -.PHONY: clean-% -clean-%: - cd $* && lake clean +clean: + lake clean diff --git a/tests/lean/NoNestedBorrows.lean b/tests/lean/NoNestedBorrows.lean new file mode 100644 index 00000000..67ef4b20 --- /dev/null +++ b/tests/lean/NoNestedBorrows.lean @@ -0,0 +1,541 @@ +-- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS +-- [no_nested_borrows] +import Base +open Primitives + +/- [no_nested_borrows::Pair] -/ +structure pair_t (T1 T2 : Type) where + pair_x : T1 + pair_y : T2 + +/- [no_nested_borrows::List] -/ +inductive list_t (T : Type) := +| Cons : T -> list_t T -> list_t T +| Nil : list_t T + +/- [no_nested_borrows::One] -/ +inductive one_t (T1 : Type) := +| One : T1 -> one_t T1 + +/- [no_nested_borrows::EmptyEnum] -/ +inductive empty_enum_t := +| Empty : empty_enum_t + +/- [no_nested_borrows::Enum] -/ +inductive enum_t := +| Variant1 : enum_t +| Variant2 : enum_t + +/- [no_nested_borrows::EmptyStruct] -/ +structure empty_struct_t where + +/- [no_nested_borrows::Sum] -/ +inductive sum_t (T1 T2 : Type) := +| Left : T1 -> sum_t T1 T2 +| Right : T2 -> sum_t T1 T2 + +/- [no_nested_borrows::neg_test] -/ +def neg_test_fwd (x : I32) : Result I32 := + - x + +/- [no_nested_borrows::add_test] -/ +def add_test_fwd (x : U32) (y : U32) : Result U32 := + x + y + +/- [no_nested_borrows::subs_test] -/ +def subs_test_fwd (x : U32) (y : U32) : Result U32 := + x - y + +/- [no_nested_borrows::div_test] -/ +def div_test_fwd (x : U32) (y : U32) : Result U32 := + x / y + +/- [no_nested_borrows::div_test1] -/ +def div_test1_fwd (x : U32) : Result U32 := + x / (U32.ofInt 2 (by intlit)) + +/- [no_nested_borrows::rem_test] -/ +def rem_test_fwd (x : U32) (y : U32) : Result U32 := + x % y + +/- [no_nested_borrows::cast_test] -/ +def cast_test_fwd (x : U32) : Result I32 := + Scalar.cast .I32 x + +/- [no_nested_borrows::test2] -/ +def test2_fwd : Result Unit := + do + let _ ← (U32.ofInt 23 (by intlit)) + (U32.ofInt 44 (by intlit)) + Result.ret () + +/- Unit test for [no_nested_borrows::test2] -/ +#assert (test2_fwd == .ret ()) + +/- [no_nested_borrows::get_max] -/ +def get_max_fwd (x : U32) (y : U32) : Result U32 := + if x >= y + then Result.ret x + else Result.ret y + +/- [no_nested_borrows::test3] -/ +def test3_fwd : Result Unit := + do + let x ← get_max_fwd (U32.ofInt 4 (by intlit)) (U32.ofInt 3 (by intlit)) + let y ← get_max_fwd (U32.ofInt 10 (by intlit)) (U32.ofInt 11 (by intlit)) + let z ← x + y + if not (z = (U32.ofInt 15 (by intlit))) + then Result.fail Error.panic + else Result.ret () + +/- Unit test for [no_nested_borrows::test3] -/ +#assert (test3_fwd == .ret ()) + +/- [no_nested_borrows::test_neg1] -/ +def test_neg1_fwd : Result Unit := + do + let y ← - (I32.ofInt 3 (by intlit)) + if not (y = (I32.ofInt (-(3:Int)) (by intlit))) + then Result.fail Error.panic + else Result.ret () + +/- Unit test for [no_nested_borrows::test_neg1] -/ +#assert (test_neg1_fwd == .ret ()) + +/- [no_nested_borrows::refs_test1] -/ +def refs_test1_fwd : Result Unit := + if not ((I32.ofInt 1 (by intlit)) = (I32.ofInt 1 (by intlit))) + then Result.fail Error.panic + else Result.ret () + +/- Unit test for [no_nested_borrows::refs_test1] -/ +#assert (refs_test1_fwd == .ret ()) + +/- [no_nested_borrows::refs_test2] -/ +def refs_test2_fwd : Result Unit := + if not ((I32.ofInt 2 (by intlit)) = (I32.ofInt 2 (by intlit))) + then Result.fail Error.panic + else + if not ((I32.ofInt 0 (by intlit)) = (I32.ofInt 0 (by intlit))) + then Result.fail Error.panic + else + if not ((I32.ofInt 2 (by intlit)) = (I32.ofInt 2 (by intlit))) + then Result.fail Error.panic + else + if not ((I32.ofInt 2 (by intlit)) = (I32.ofInt 2 (by intlit))) + then Result.fail Error.panic + else Result.ret () + +/- Unit test for [no_nested_borrows::refs_test2] -/ +#assert (refs_test2_fwd == .ret ()) + +/- [no_nested_borrows::test_list1] -/ +def test_list1_fwd : Result Unit := + Result.ret () + +/- Unit test for [no_nested_borrows::test_list1] -/ +#assert (test_list1_fwd == .ret ()) + +/- [no_nested_borrows::test_box1] -/ +def test_box1_fwd : Result Unit := + let b := (I32.ofInt 1 (by intlit)) + let x := b + if not (x = (I32.ofInt 1 (by intlit))) + then Result.fail Error.panic + else Result.ret () + +/- Unit test for [no_nested_borrows::test_box1] -/ +#assert (test_box1_fwd == .ret ()) + +/- [no_nested_borrows::copy_int] -/ +def copy_int_fwd (x : I32) : Result I32 := + Result.ret x + +/- [no_nested_borrows::test_unreachable] -/ +def test_unreachable_fwd (b : Bool) : Result Unit := + if b + then Result.fail Error.panic + else Result.ret () + +/- [no_nested_borrows::test_panic] -/ +def test_panic_fwd (b : Bool) : Result Unit := + if b + then Result.fail Error.panic + else Result.ret () + +/- [no_nested_borrows::test_copy_int] -/ +def test_copy_int_fwd : Result Unit := + do + let y ← copy_int_fwd (I32.ofInt 0 (by intlit)) + if not ((I32.ofInt 0 (by intlit)) = y) + then Result.fail Error.panic + else Result.ret () + +/- Unit test for [no_nested_borrows::test_copy_int] -/ +#assert (test_copy_int_fwd == .ret ()) + +/- [no_nested_borrows::is_cons] -/ +def is_cons_fwd (T : Type) (l : list_t T) : Result Bool := + match h: l with + | list_t.Cons t l0 => Result.ret true + | list_t.Nil => Result.ret false + +/- [no_nested_borrows::test_is_cons] -/ +def test_is_cons_fwd : Result Unit := + do + let l := list_t.Nil + let b ← is_cons_fwd I32 (list_t.Cons (I32.ofInt 0 (by intlit)) l) + if not b + then Result.fail Error.panic + else Result.ret () + +/- Unit test for [no_nested_borrows::test_is_cons] -/ +#assert (test_is_cons_fwd == .ret ()) + +/- [no_nested_borrows::split_list] -/ +def split_list_fwd (T : Type) (l : list_t T) : Result (T × (list_t T)) := + match h: l with + | list_t.Cons hd tl => Result.ret (hd, tl) + | list_t.Nil => Result.fail Error.panic + +/- [no_nested_borrows::test_split_list] -/ +def test_split_list_fwd : Result Unit := + do + let l := list_t.Nil + let p ← split_list_fwd I32 (list_t.Cons (I32.ofInt 0 (by intlit)) l) + let (hd, _) := p + if not (hd = (I32.ofInt 0 (by intlit))) + then Result.fail Error.panic + else Result.ret () + +/- Unit test for [no_nested_borrows::test_split_list] -/ +#assert (test_split_list_fwd == .ret ()) + +/- [no_nested_borrows::choose] -/ +def choose_fwd (T : Type) (b : Bool) (x : T) (y : T) : Result T := + if b + then Result.ret x + else Result.ret y + +/- [no_nested_borrows::choose] -/ +def choose_back + (T : Type) (b : Bool) (x : T) (y : T) (ret0 : T) : Result (T × T) := + if b + then Result.ret (ret0, y) + else Result.ret (x, ret0) + +/- [no_nested_borrows::choose_test] -/ +def choose_test_fwd : Result Unit := + do + let z ← + choose_fwd I32 true (I32.ofInt 0 (by intlit)) (I32.ofInt 0 (by intlit)) + let z0 ← z + (I32.ofInt 1 (by intlit)) + if not (z0 = (I32.ofInt 1 (by intlit))) + then Result.fail Error.panic + else + do + let (x, y) ← + choose_back I32 true (I32.ofInt 0 (by intlit)) + (I32.ofInt 0 (by intlit)) z0 + if not (x = (I32.ofInt 1 (by intlit))) + then Result.fail Error.panic + else + if not (y = (I32.ofInt 0 (by intlit))) + then Result.fail Error.panic + else Result.ret () + +/- Unit test for [no_nested_borrows::choose_test] -/ +#assert (choose_test_fwd == .ret ()) + +/- [no_nested_borrows::test_char] -/ +def test_char_fwd : Result Char := + Result.ret 'a' + +mutual + +/- [no_nested_borrows::NodeElem] -/ +inductive node_elem_t (T : Type) := +| Cons : tree_t T -> node_elem_t T -> node_elem_t T +| Nil : node_elem_t T + +/- [no_nested_borrows::Tree] -/ +inductive tree_t (T : Type) := +| Leaf : T -> tree_t T +| Node : T -> node_elem_t T -> tree_t T -> tree_t T + +end + +/- [no_nested_borrows::list_length] -/ +divergent def list_length_fwd (T : Type) (l : list_t T) : Result U32 := + match h: l with + | list_t.Cons t l1 => + do + let i ← list_length_fwd T l1 + (U32.ofInt 1 (by intlit)) + i + | list_t.Nil => Result.ret (U32.ofInt 0 (by intlit)) + +/- [no_nested_borrows::list_nth_shared] -/ +divergent def list_nth_shared_fwd + (T : Type) (l : list_t T) (i : U32) : Result T := + match h: l with + | list_t.Cons x tl => + if i = (U32.ofInt 0 (by intlit)) + then Result.ret x + else + do + let i0 ← i - (U32.ofInt 1 (by intlit)) + list_nth_shared_fwd T tl i0 + | list_t.Nil => Result.fail Error.panic + +/- [no_nested_borrows::list_nth_mut] -/ +divergent def list_nth_mut_fwd + (T : Type) (l : list_t T) (i : U32) : Result T := + match h: l with + | list_t.Cons x tl => + if i = (U32.ofInt 0 (by intlit)) + then Result.ret x + else do + let i0 ← i - (U32.ofInt 1 (by intlit)) + list_nth_mut_fwd T tl i0 + | list_t.Nil => Result.fail Error.panic + +/- [no_nested_borrows::list_nth_mut] -/ +divergent def list_nth_mut_back + (T : Type) (l : list_t T) (i : U32) (ret0 : T) : Result (list_t T) := + match h: l with + | list_t.Cons x tl => + if i = (U32.ofInt 0 (by intlit)) + then Result.ret (list_t.Cons ret0 tl) + else + do + let i0 ← i - (U32.ofInt 1 (by intlit)) + let tl0 ← list_nth_mut_back T tl i0 ret0 + Result.ret (list_t.Cons x tl0) + | list_t.Nil => Result.fail Error.panic + +/- [no_nested_borrows::list_rev_aux] -/ +divergent def list_rev_aux_fwd + (T : Type) (li : list_t T) (lo : list_t T) : Result (list_t T) := + match h: li with + | list_t.Cons hd tl => list_rev_aux_fwd T tl (list_t.Cons hd lo) + | list_t.Nil => Result.ret lo + +/- [no_nested_borrows::list_rev] -/ +def list_rev_fwd_back (T : Type) (l : list_t T) : Result (list_t T) := + let li := mem_replace_fwd (list_t T) l list_t.Nil + list_rev_aux_fwd T li list_t.Nil + +/- [no_nested_borrows::test_list_functions] -/ +def test_list_functions_fwd : Result Unit := + do + let l := list_t.Nil + let l0 := list_t.Cons (I32.ofInt 2 (by intlit)) l + let l1 := list_t.Cons (I32.ofInt 1 (by intlit)) l0 + let i ← list_length_fwd I32 (list_t.Cons (I32.ofInt 0 (by intlit)) l1) + if not (i = (U32.ofInt 3 (by intlit))) + then Result.fail Error.panic + else + do + let i0 ← + list_nth_shared_fwd I32 (list_t.Cons (I32.ofInt 0 (by intlit)) l1) + (U32.ofInt 0 (by intlit)) + if not (i0 = (I32.ofInt 0 (by intlit))) + then Result.fail Error.panic + else + do + let i1 ← + list_nth_shared_fwd I32 (list_t.Cons (I32.ofInt 0 (by intlit)) + l1) (U32.ofInt 1 (by intlit)) + if not (i1 = (I32.ofInt 1 (by intlit))) + then Result.fail Error.panic + else + do + let i2 ← + list_nth_shared_fwd I32 (list_t.Cons + (I32.ofInt 0 (by intlit)) l1) (U32.ofInt 2 (by intlit)) + if not (i2 = (I32.ofInt 2 (by intlit))) + then Result.fail Error.panic + else + do + let ls ← + list_nth_mut_back I32 (list_t.Cons + (I32.ofInt 0 (by intlit)) l1) (U32.ofInt 1 (by intlit)) + (I32.ofInt 3 (by intlit)) + let i3 ← + list_nth_shared_fwd I32 ls (U32.ofInt 0 (by intlit)) + if not (i3 = (I32.ofInt 0 (by intlit))) + then Result.fail Error.panic + else + do + let i4 ← + list_nth_shared_fwd I32 ls (U32.ofInt 1 (by intlit)) + if not (i4 = (I32.ofInt 3 (by intlit))) + then Result.fail Error.panic + else + do + let i5 ← + list_nth_shared_fwd I32 ls + (U32.ofInt 2 (by intlit)) + if not (i5 = (I32.ofInt 2 (by intlit))) + then Result.fail Error.panic + else Result.ret () + +/- Unit test for [no_nested_borrows::test_list_functions] -/ +#assert (test_list_functions_fwd == .ret ()) + +/- [no_nested_borrows::id_mut_pair1] -/ +def id_mut_pair1_fwd (T1 T2 : Type) (x : T1) (y : T2) : Result (T1 × T2) := + Result.ret (x, y) + +/- [no_nested_borrows::id_mut_pair1] -/ +def id_mut_pair1_back + (T1 T2 : Type) (x : T1) (y : T2) (ret0 : (T1 × T2)) : Result (T1 × T2) := + let (t, t0) := ret0 + Result.ret (t, t0) + +/- [no_nested_borrows::id_mut_pair2] -/ +def id_mut_pair2_fwd (T1 T2 : Type) (p : (T1 × T2)) : Result (T1 × T2) := + let (t, t0) := p + Result.ret (t, t0) + +/- [no_nested_borrows::id_mut_pair2] -/ +def id_mut_pair2_back + (T1 T2 : Type) (p : (T1 × T2)) (ret0 : (T1 × T2)) : Result (T1 × T2) := + let (t, t0) := ret0 + Result.ret (t, t0) + +/- [no_nested_borrows::id_mut_pair3] -/ +def id_mut_pair3_fwd (T1 T2 : Type) (x : T1) (y : T2) : Result (T1 × T2) := + Result.ret (x, y) + +/- [no_nested_borrows::id_mut_pair3] -/ +def id_mut_pair3_back'a + (T1 T2 : Type) (x : T1) (y : T2) (ret0 : T1) : Result T1 := + Result.ret ret0 + +/- [no_nested_borrows::id_mut_pair3] -/ +def id_mut_pair3_back'b + (T1 T2 : Type) (x : T1) (y : T2) (ret0 : T2) : Result T2 := + Result.ret ret0 + +/- [no_nested_borrows::id_mut_pair4] -/ +def id_mut_pair4_fwd (T1 T2 : Type) (p : (T1 × T2)) : Result (T1 × T2) := + let (t, t0) := p + Result.ret (t, t0) + +/- [no_nested_borrows::id_mut_pair4] -/ +def id_mut_pair4_back'a + (T1 T2 : Type) (p : (T1 × T2)) (ret0 : T1) : Result T1 := + Result.ret ret0 + +/- [no_nested_borrows::id_mut_pair4] -/ +def id_mut_pair4_back'b + (T1 T2 : Type) (p : (T1 × T2)) (ret0 : T2) : Result T2 := + Result.ret ret0 + +/- [no_nested_borrows::StructWithTuple] -/ +structure struct_with_tuple_t (T1 T2 : Type) where + struct_with_tuple_p : (T1 × T2) + +/- [no_nested_borrows::new_tuple1] -/ +def new_tuple1_fwd : Result (struct_with_tuple_t U32 U32) := + Result.ret + { + struct_with_tuple_p := + ((U32.ofInt 1 (by intlit)), (U32.ofInt 2 (by intlit))) + } + +/- [no_nested_borrows::new_tuple2] -/ +def new_tuple2_fwd : Result (struct_with_tuple_t I16 I16) := + Result.ret + { + struct_with_tuple_p := + ((I16.ofInt 1 (by intlit)), (I16.ofInt 2 (by intlit))) + } + +/- [no_nested_borrows::new_tuple3] -/ +def new_tuple3_fwd : Result (struct_with_tuple_t U64 I64) := + Result.ret + { + struct_with_tuple_p := + ((U64.ofInt 1 (by intlit)), (I64.ofInt 2 (by intlit))) + } + +/- [no_nested_borrows::StructWithPair] -/ +structure struct_with_pair_t (T1 T2 : Type) where + struct_with_pair_p : pair_t T1 T2 + +/- [no_nested_borrows::new_pair1] -/ +def new_pair1_fwd : Result (struct_with_pair_t U32 U32) := + Result.ret + { + struct_with_pair_p := + { + pair_x := (U32.ofInt 1 (by intlit)), + pair_y := (U32.ofInt 2 (by intlit)) + } + } + +/- [no_nested_borrows::test_constants] -/ +def test_constants_fwd : Result Unit := + do + let swt ← new_tuple1_fwd + let (i, _) := swt.struct_with_tuple_p + if not (i = (U32.ofInt 1 (by intlit))) + then Result.fail Error.panic + else + do + let swt0 ← new_tuple2_fwd + let (i0, _) := swt0.struct_with_tuple_p + if not (i0 = (I16.ofInt 1 (by intlit))) + then Result.fail Error.panic + else + do + let swt1 ← new_tuple3_fwd + let (i1, _) := swt1.struct_with_tuple_p + if not (i1 = (U64.ofInt 1 (by intlit))) + then Result.fail Error.panic + else + do + let swp ← new_pair1_fwd + if not (swp.struct_with_pair_p.pair_x = + (U32.ofInt 1 (by intlit))) + then Result.fail Error.panic + else Result.ret () + +/- Unit test for [no_nested_borrows::test_constants] -/ +#assert (test_constants_fwd == .ret ()) + +/- [no_nested_borrows::test_weird_borrows1] -/ +def test_weird_borrows1_fwd : Result Unit := + Result.ret () + +/- Unit test for [no_nested_borrows::test_weird_borrows1] -/ +#assert (test_weird_borrows1_fwd == .ret ()) + +/- [no_nested_borrows::test_mem_replace] -/ +def test_mem_replace_fwd_back (px : U32) : Result U32 := + let y := mem_replace_fwd U32 px (U32.ofInt 1 (by intlit)) + if not (y = (U32.ofInt 0 (by intlit))) + then Result.fail Error.panic + else Result.ret (U32.ofInt 2 (by intlit)) + +/- [no_nested_borrows::test_shared_borrow_bool1] -/ +def test_shared_borrow_bool1_fwd (b : Bool) : Result U32 := + if b + then Result.ret (U32.ofInt 0 (by intlit)) + else Result.ret (U32.ofInt 1 (by intlit)) + +/- [no_nested_borrows::test_shared_borrow_bool2] -/ +def test_shared_borrow_bool2_fwd : Result U32 := + Result.ret (U32.ofInt 0 (by intlit)) + +/- [no_nested_borrows::test_shared_borrow_enum1] -/ +def test_shared_borrow_enum1_fwd (l : list_t U32) : Result U32 := + match h: l with + | list_t.Cons i l0 => Result.ret (U32.ofInt 1 (by intlit)) + | list_t.Nil => Result.ret (U32.ofInt 0 (by intlit)) + +/- [no_nested_borrows::test_shared_borrow_enum2] -/ +def test_shared_borrow_enum2_fwd : Result U32 := + Result.ret (U32.ofInt 0 (by intlit)) + diff --git a/tests/lean/Paper.lean b/tests/lean/Paper.lean new file mode 100644 index 00000000..9019b694 --- /dev/null +++ b/tests/lean/Paper.lean @@ -0,0 +1,125 @@ +-- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS +-- [paper] +import Base +open Primitives + +/- [paper::ref_incr] -/ +def ref_incr_fwd_back (x : I32) : Result I32 := + x + (I32.ofInt 1 (by intlit)) + +/- [paper::test_incr] -/ +def test_incr_fwd : Result Unit := + do + let x ← ref_incr_fwd_back (I32.ofInt 0 (by intlit)) + if not (x = (I32.ofInt 1 (by intlit))) + then Result.fail Error.panic + else Result.ret () + +/- Unit test for [paper::test_incr] -/ +#assert (test_incr_fwd == .ret ()) + +/- [paper::choose] -/ +def choose_fwd (T : Type) (b : Bool) (x : T) (y : T) : Result T := + if b + then Result.ret x + else Result.ret y + +/- [paper::choose] -/ +def choose_back + (T : Type) (b : Bool) (x : T) (y : T) (ret0 : T) : Result (T × T) := + if b + then Result.ret (ret0, y) + else Result.ret (x, ret0) + +/- [paper::test_choose] -/ +def test_choose_fwd : Result Unit := + do + let z ← + choose_fwd I32 true (I32.ofInt 0 (by intlit)) (I32.ofInt 0 (by intlit)) + let z0 ← z + (I32.ofInt 1 (by intlit)) + if not (z0 = (I32.ofInt 1 (by intlit))) + then Result.fail Error.panic + else + do + let (x, y) ← + choose_back I32 true (I32.ofInt 0 (by intlit)) + (I32.ofInt 0 (by intlit)) z0 + if not (x = (I32.ofInt 1 (by intlit))) + then Result.fail Error.panic + else + if not (y = (I32.ofInt 0 (by intlit))) + then Result.fail Error.panic + else Result.ret () + +/- Unit test for [paper::test_choose] -/ +#assert (test_choose_fwd == .ret ()) + +/- [paper::List] -/ +inductive list_t (T : Type) := +| Cons : T -> list_t T -> list_t T +| Nil : list_t T + +/- [paper::list_nth_mut] -/ +divergent def list_nth_mut_fwd + (T : Type) (l : list_t T) (i : U32) : Result T := + match h: l with + | list_t.Cons x tl => + if i = (U32.ofInt 0 (by intlit)) + then Result.ret x + else do + let i0 ← i - (U32.ofInt 1 (by intlit)) + list_nth_mut_fwd T tl i0 + | list_t.Nil => Result.fail Error.panic + +/- [paper::list_nth_mut] -/ +divergent def list_nth_mut_back + (T : Type) (l : list_t T) (i : U32) (ret0 : T) : Result (list_t T) := + match h: l with + | list_t.Cons x tl => + if i = (U32.ofInt 0 (by intlit)) + then Result.ret (list_t.Cons ret0 tl) + else + do + let i0 ← i - (U32.ofInt 1 (by intlit)) + let tl0 ← list_nth_mut_back T tl i0 ret0 + Result.ret (list_t.Cons x tl0) + | list_t.Nil => Result.fail Error.panic + +/- [paper::sum] -/ +divergent def sum_fwd (l : list_t I32) : Result I32 := + match h: l with + | list_t.Cons x tl => do + let i ← sum_fwd tl + x + i + | list_t.Nil => Result.ret (I32.ofInt 0 (by intlit)) + +/- [paper::test_nth] -/ +def test_nth_fwd : Result Unit := + do + let l := list_t.Nil + let l0 := list_t.Cons (I32.ofInt 3 (by intlit)) l + let l1 := list_t.Cons (I32.ofInt 2 (by intlit)) l0 + let x ← + list_nth_mut_fwd I32 (list_t.Cons (I32.ofInt 1 (by intlit)) l1) + (U32.ofInt 2 (by intlit)) + let x0 ← x + (I32.ofInt 1 (by intlit)) + let l2 ← + list_nth_mut_back I32 (list_t.Cons (I32.ofInt 1 (by intlit)) l1) + (U32.ofInt 2 (by intlit)) x0 + let i ← sum_fwd l2 + if not (i = (I32.ofInt 7 (by intlit))) + then Result.fail Error.panic + else Result.ret () + +/- Unit test for [paper::test_nth] -/ +#assert (test_nth_fwd == .ret ()) + +/- [paper::call_choose] -/ +def call_choose_fwd (p : (U32 × U32)) : Result U32 := + do + let (px, py) := p + let pz ← choose_fwd U32 true px py + let pz0 ← pz + (U32.ofInt 1 (by intlit)) + let (px0, _) ← choose_back U32 true px py pz0 + Result.ret px0 + diff --git a/tests/lean/PoloniusList.lean b/tests/lean/PoloniusList.lean new file mode 100644 index 00000000..671f54ea --- /dev/null +++ b/tests/lean/PoloniusList.lean @@ -0,0 +1,33 @@ +-- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS +-- [polonius_list] +import Base +open Primitives + +/- [polonius_list::List] -/ +inductive list_t (T : Type) := +| Cons : T -> list_t T -> list_t T +| Nil : list_t T + +/- [polonius_list::get_list_at_x] -/ +divergent def get_list_at_x_fwd + (ls : list_t U32) (x : U32) : Result (list_t U32) := + match h: ls with + | list_t.Cons hd tl => + if hd = x + then Result.ret (list_t.Cons hd tl) + else get_list_at_x_fwd tl x + | list_t.Nil => Result.ret list_t.Nil + +/- [polonius_list::get_list_at_x] -/ +divergent def get_list_at_x_back + (ls : list_t U32) (x : U32) (ret0 : list_t U32) : Result (list_t U32) := + match h: ls with + | list_t.Cons hd tl => + if hd = x + then Result.ret ret0 + else + do + let tl0 ← get_list_at_x_back tl x ret0 + Result.ret (list_t.Cons hd tl0) + | list_t.Nil => Result.ret ret0 + diff --git a/tests/lean/Tests.lean b/tests/lean/Tests.lean new file mode 100644 index 00000000..9b12270e --- /dev/null +++ b/tests/lean/Tests.lean @@ -0,0 +1,9 @@ +import BetreeMain +import Constants +import External +import Hashmap +import HashmapMain +import Loops +import NoNestedBorrows +import Paper +import PoloniusList diff --git a/tests/lean/hashmap/Base/Primitives.lean b/tests/lean/hashmap/Base/Primitives.lean deleted file mode 100644 index 4a66a453..00000000 --- a/tests/lean/hashmap/Base/Primitives.lean +++ /dev/null @@ -1,583 +0,0 @@ -import Lean -import Lean.Meta.Tactic.Simp -import Init.Data.List.Basic -import Mathlib.Tactic.RunCmd - --------------------- --- ASSERT COMMAND -- --------------------- - -open Lean Elab Command Term Meta - -syntax (name := assert) "#assert" term: command - -@[command_elab assert] -unsafe -def assertImpl : CommandElab := fun (_stx: Syntax) => do - runTermElabM (fun _ => do - let r ← evalTerm Bool (mkConst ``Bool) _stx[1] - if not r then - logInfo "Assertion failed for: " - logInfo _stx[1] - logError "Expression reduced to false" - pure ()) - -#eval 2 == 2 -#assert (2 == 2) - -------------- --- PRELUDE -- -------------- - --- Results & monadic combinators - -inductive Error where - | assertionFailure: Error - | integerOverflow: Error - | divisionByZero: Error - | arrayOutOfBounds: Error - | maximumSizeExceeded: Error - | panic: Error -deriving Repr, BEq - -open Error - -inductive Result (α : Type u) where - | ret (v: α): Result α - | fail (e: Error): Result α -deriving Repr, BEq - -open Result - -instance Result_Inhabited (α : Type u) : Inhabited (Result α) := - Inhabited.mk (fail panic) - -/- HELPERS -/ - -def ret? {α: Type} (r: Result α): Bool := - match r with - | Result.ret _ => true - | Result.fail _ => false - -def massert (b:Bool) : Result Unit := - if b then .ret () else fail assertionFailure - -def eval_global {α: Type} (x: Result α) (_: ret? x): α := - match x with - | Result.fail _ => by contradiction - | Result.ret x => x - -/- DO-DSL SUPPORT -/ - -def bind (x: Result α) (f: α -> Result β) : Result β := - match x with - | ret v => f v - | fail v => fail v - --- Allows using Result in do-blocks -instance : Bind Result where - bind := bind - --- Allows using return x in do-blocks -instance : Pure Result where - pure := fun x => ret x - -/- CUSTOM-DSL SUPPORT -/ - --- Let-binding the Result of a monadic operation is oftentimes not sufficient, --- because we may need a hypothesis for equational reasoning in the scope. We --- rely on subtype, and a custom let-binding operator, in effect recreating our --- own variant of the do-dsl - -def Result.attach {α: Type} (o : Result α): Result { x : α // o = ret x } := - match o with - | .ret x => .ret ⟨x, rfl⟩ - | .fail e => .fail e - -macro "let" e:term " ⟵ " f:term : doElem => - `(doElem| let ⟨$e, h⟩ ← Result.attach $f) - --- TODO: any way to factorize both definitions? -macro "let" e:term " <-- " f:term : doElem => - `(doElem| let ⟨$e, h⟩ ← Result.attach $f) - --- We call the hypothesis `h`, in effect making it unavailable to the user --- (because too much shadowing). But in practice, once can use the French single --- quote notation (input with f< and f>), where `‹ h ›` finds a suitable --- hypothesis in the context, this is equivalent to `have x: h := by assumption in x` -#eval do - let y <-- .ret (0: Nat) - let _: y = 0 := by cases ‹ ret 0 = ret y › ; decide - let r: { x: Nat // x = 0 } := ⟨ y, by assumption ⟩ - .ret r - ----------------------- --- MACHINE INTEGERS -- ----------------------- - --- We redefine our machine integers types. - --- For Isize/Usize, we reuse `getNumBits` from `USize`. You cannot reduce `getNumBits` --- using the simplifier, meaning that proofs do not depend on the compile-time value of --- USize.size. (Lean assumes 32 or 64-bit platforms, and Rust doesn't really support, at --- least officially, 16-bit microcontrollers, so this seems like a fine design decision --- for now.) - --- Note from Chris Bailey: "If there's more than one salient property of your --- definition then the subtyping strategy might get messy, and the property part --- of a subtype is less discoverable by the simplifier or tactics like --- library_search." So, we will not add refinements on the return values of the --- operations defined on Primitives, but will rather rely on custom lemmas to --- invert on possible return values of the primitive operations. - --- Machine integer constants, done via `ofNatCore`, which requires a proof that --- the `Nat` fits within the desired integer type. We provide a custom tactic. - -open System.Platform.getNumBits - --- TODO: is there a way of only importing System.Platform.getNumBits? --- -@[simp] def size_num_bits : Nat := (System.Platform.getNumBits ()).val - --- Remark: Lean seems to use < for the comparisons with the upper bounds by convention. --- We keep the F* convention for now. -@[simp] def Isize.min : Int := - (HPow.hPow 2 (size_num_bits - 1)) -@[simp] def Isize.max : Int := (HPow.hPow 2 (size_num_bits - 1)) - 1 -@[simp] def I8.min : Int := - (HPow.hPow 2 7) -@[simp] def I8.max : Int := HPow.hPow 2 7 - 1 -@[simp] def I16.min : Int := - (HPow.hPow 2 15) -@[simp] def I16.max : Int := HPow.hPow 2 15 - 1 -@[simp] def I32.min : Int := -(HPow.hPow 2 31) -@[simp] def I32.max : Int := HPow.hPow 2 31 - 1 -@[simp] def I64.min : Int := -(HPow.hPow 2 63) -@[simp] def I64.max : Int := HPow.hPow 2 63 - 1 -@[simp] def I128.min : Int := -(HPow.hPow 2 127) -@[simp] def I128.max : Int := HPow.hPow 2 127 - 1 -@[simp] def Usize.min : Int := 0 -@[simp] def Usize.max : Int := HPow.hPow 2 size_num_bits - 1 -@[simp] def U8.min : Int := 0 -@[simp] def U8.max : Int := HPow.hPow 2 8 - 1 -@[simp] def U16.min : Int := 0 -@[simp] def U16.max : Int := HPow.hPow 2 16 - 1 -@[simp] def U32.min : Int := 0 -@[simp] def U32.max : Int := HPow.hPow 2 32 - 1 -@[simp] def U64.min : Int := 0 -@[simp] def U64.max : Int := HPow.hPow 2 64 - 1 -@[simp] def U128.min : Int := 0 -@[simp] def U128.max : Int := HPow.hPow 2 128 - 1 - -#assert (I8.min == -128) -#assert (I8.max == 127) -#assert (I16.min == -32768) -#assert (I16.max == 32767) -#assert (I32.min == -2147483648) -#assert (I32.max == 2147483647) -#assert (I64.min == -9223372036854775808) -#assert (I64.max == 9223372036854775807) -#assert (I128.min == -170141183460469231731687303715884105728) -#assert (I128.max == 170141183460469231731687303715884105727) -#assert (U8.min == 0) -#assert (U8.max == 255) -#assert (U16.min == 0) -#assert (U16.max == 65535) -#assert (U32.min == 0) -#assert (U32.max == 4294967295) -#assert (U64.min == 0) -#assert (U64.max == 18446744073709551615) -#assert (U128.min == 0) -#assert (U128.max == 340282366920938463463374607431768211455) - -inductive ScalarTy := -| Isize -| I8 -| I16 -| I32 -| I64 -| I128 -| Usize -| U8 -| U16 -| U32 -| U64 -| U128 - -def Scalar.min (ty : ScalarTy) : Int := - match ty with - | .Isize => Isize.min - | .I8 => I8.min - | .I16 => I16.min - | .I32 => I32.min - | .I64 => I64.min - | .I128 => I128.min - | .Usize => Usize.min - | .U8 => U8.min - | .U16 => U16.min - | .U32 => U32.min - | .U64 => U64.min - | .U128 => U128.min - -def Scalar.max (ty : ScalarTy) : Int := - match ty with - | .Isize => Isize.max - | .I8 => I8.max - | .I16 => I16.max - | .I32 => I32.max - | .I64 => I64.max - | .I128 => I128.max - | .Usize => Usize.max - | .U8 => U8.max - | .U16 => U16.max - | .U32 => U32.max - | .U64 => U64.max - | .U128 => U128.max - --- "Conservative" bounds --- We use those because we can't compare to the isize bounds (which can't --- reduce at compile-time). Whenever we perform an arithmetic operation like --- addition we need to check that the result is in bounds: we first compare --- to the conservative bounds, which reduce, then compare to the real bounds. --- This is useful for the various #asserts that we want to reduce at --- type-checking time. -def Scalar.cMin (ty : ScalarTy) : Int := - match ty with - | .Isize => I32.min - | _ => Scalar.min ty - -def Scalar.cMax (ty : ScalarTy) : Int := - match ty with - | .Isize => I32.max - | .Usize => U32.max - | _ => Scalar.max ty - -theorem Scalar.cMin_bound ty : Scalar.min ty <= Scalar.cMin ty := by sorry -theorem Scalar.cMax_bound ty : Scalar.min ty <= Scalar.cMin ty := by sorry - -structure Scalar (ty : ScalarTy) where - val : Int - hmin : Scalar.min ty <= val - hmax : val <= Scalar.max ty - -theorem Scalar.bound_suffices (ty : ScalarTy) (x : Int) : - Scalar.cMin ty <= x && x <= Scalar.cMax ty -> - (decide (Scalar.min ty ≤ x) && decide (x ≤ Scalar.max ty)) = true - := by sorry - -def Scalar.ofIntCore {ty : ScalarTy} (x : Int) - (hmin : Scalar.min ty <= x) (hmax : x <= Scalar.max ty) : Scalar ty := - { val := x, hmin := hmin, hmax := hmax } - -def Scalar.ofInt {ty : ScalarTy} (x : Int) - (h : Scalar.min ty <= x && x <= Scalar.max ty) : Scalar ty := - let hmin: Scalar.min ty <= x := by sorry - let hmax: x <= Scalar.max ty := by sorry - Scalar.ofIntCore x hmin hmax - --- Further thoughts: look at what has been done here: --- https://github.com/leanprover-community/mathlib4/blob/master/Mathlib/Data/Fin/Basic.lean --- and --- https://github.com/leanprover-community/mathlib4/blob/master/Mathlib/Data/UInt.lean --- which both contain a fair amount of reasoning already! -def Scalar.tryMk (ty : ScalarTy) (x : Int) : Result (Scalar ty) := - -- TODO: write this with only one if then else - if hmin_cons: Scalar.cMin ty <= x || Scalar.min ty <= x then - if hmax_cons: x <= Scalar.cMax ty || x <= Scalar.max ty then - let hmin: Scalar.min ty <= x := by sorry - let hmax: x <= Scalar.max ty := by sorry - return Scalar.ofIntCore x hmin hmax - else fail integerOverflow - else fail integerOverflow - -def Scalar.neg {ty : ScalarTy} (x : Scalar ty) : Result (Scalar ty) := Scalar.tryMk ty (- x.val) - -def Scalar.div {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - if y.val != 0 then Scalar.tryMk ty (x.val / y.val) else fail divisionByZero - --- Checking that the % operation in Lean computes the same as the remainder operation in Rust -#assert 1 % 2 = (1:Int) -#assert (-1) % 2 = -1 -#assert 1 % (-2) = 1 -#assert (-1) % (-2) = -1 - -def Scalar.rem {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - if y.val != 0 then Scalar.tryMk ty (x.val % y.val) else fail divisionByZero - -def Scalar.add {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - Scalar.tryMk ty (x.val + y.val) - -def Scalar.sub {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - Scalar.tryMk ty (x.val - y.val) - -def Scalar.mul {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - Scalar.tryMk ty (x.val * y.val) - --- TODO: instances of +, -, * etc. for scalars - --- Cast an integer from a [src_ty] to a [tgt_ty] --- TODO: check the semantics of casts in Rust -def Scalar.cast {src_ty : ScalarTy} (tgt_ty : ScalarTy) (x : Scalar src_ty) : Result (Scalar tgt_ty) := - Scalar.tryMk tgt_ty x.val - --- The scalar types --- We declare the definitions as reducible so that Lean can unfold them (useful --- for type class resolution for instance). -@[reducible] def Isize := Scalar .Isize -@[reducible] def I8 := Scalar .I8 -@[reducible] def I16 := Scalar .I16 -@[reducible] def I32 := Scalar .I32 -@[reducible] def I64 := Scalar .I64 -@[reducible] def I128 := Scalar .I128 -@[reducible] def Usize := Scalar .Usize -@[reducible] def U8 := Scalar .U8 -@[reducible] def U16 := Scalar .U16 -@[reducible] def U32 := Scalar .U32 -@[reducible] def U64 := Scalar .U64 -@[reducible] def U128 := Scalar .U128 - --- TODO: below: not sure this is the best way. --- Should we rather overload operations like +, -, etc.? --- Also, it is possible to automate the generation of those definitions --- with macros (but would it be a good idea? It would be less easy to --- read the file, which is not supposed to change a lot) - --- Negation - -/-- -Remark: there is no heterogeneous negation in the Lean prelude: we thus introduce -one here. - -The notation typeclass for heterogeneous addition. -This enables the notation `- a : β` where `a : α`. --/ -class HNeg (α : Type u) (β : outParam (Type v)) where - /-- `- a` computes the negation of `a`. - The meaning of this notation is type-dependent. -/ - hNeg : α → β - -prefix:75 "-" => HNeg.hNeg - -instance : HNeg Isize (Result Isize) where hNeg x := Scalar.neg x -instance : HNeg I8 (Result I8) where hNeg x := Scalar.neg x -instance : HNeg I16 (Result I16) where hNeg x := Scalar.neg x -instance : HNeg I32 (Result I32) where hNeg x := Scalar.neg x -instance : HNeg I64 (Result I64) where hNeg x := Scalar.neg x -instance : HNeg I128 (Result I128) where hNeg x := Scalar.neg x - --- Addition -instance {ty} : HAdd (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hAdd x y := Scalar.add x y - --- Substraction -instance {ty} : HSub (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hSub x y := Scalar.sub x y - --- Multiplication -instance {ty} : HMul (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hMul x y := Scalar.mul x y - --- Division -instance {ty} : HDiv (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hDiv x y := Scalar.div x y - --- Remainder -instance {ty} : HMod (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hMod x y := Scalar.rem x y - --- ofIntCore --- TODO: typeclass? -def Isize.ofIntCore := @Scalar.ofIntCore .Isize -def I8.ofIntCore := @Scalar.ofIntCore .I8 -def I16.ofIntCore := @Scalar.ofIntCore .I16 -def I32.ofIntCore := @Scalar.ofIntCore .I32 -def I64.ofIntCore := @Scalar.ofIntCore .I64 -def I128.ofIntCore := @Scalar.ofIntCore .I128 -def Usize.ofIntCore := @Scalar.ofIntCore .Usize -def U8.ofIntCore := @Scalar.ofIntCore .U8 -def U16.ofIntCore := @Scalar.ofIntCore .U16 -def U32.ofIntCore := @Scalar.ofIntCore .U32 -def U64.ofIntCore := @Scalar.ofIntCore .U64 -def U128.ofIntCore := @Scalar.ofIntCore .U128 - --- ofInt --- TODO: typeclass? -def Isize.ofInt := @Scalar.ofInt .Isize -def I8.ofInt := @Scalar.ofInt .I8 -def I16.ofInt := @Scalar.ofInt .I16 -def I32.ofInt := @Scalar.ofInt .I32 -def I64.ofInt := @Scalar.ofInt .I64 -def I128.ofInt := @Scalar.ofInt .I128 -def Usize.ofInt := @Scalar.ofInt .Usize -def U8.ofInt := @Scalar.ofInt .U8 -def U16.ofInt := @Scalar.ofInt .U16 -def U32.ofInt := @Scalar.ofInt .U32 -def U64.ofInt := @Scalar.ofInt .U64 -def U128.ofInt := @Scalar.ofInt .U128 - --- Comparisons -instance {ty} : LT (Scalar ty) where - lt a b := LT.lt a.val b.val - -instance {ty} : LE (Scalar ty) where le a b := LE.le a.val b.val - -instance Scalar.decLt {ty} (a b : Scalar ty) : Decidable (LT.lt a b) := Int.decLt .. -instance Scalar.decLe {ty} (a b : Scalar ty) : Decidable (LE.le a b) := Int.decLe .. - -theorem Scalar.eq_of_val_eq {ty} : ∀ {i j : Scalar ty}, Eq i.val j.val → Eq i j - | ⟨_, _, _⟩, ⟨_, _, _⟩, rfl => rfl - -theorem Scalar.val_eq_of_eq {ty} {i j : Scalar ty} (h : Eq i j) : Eq i.val j.val := - h ▸ rfl - -theorem Scalar.ne_of_val_ne {ty} {i j : Scalar ty} (h : Not (Eq i.val j.val)) : Not (Eq i j) := - fun h' => absurd (val_eq_of_eq h') h - -instance (ty : ScalarTy) : DecidableEq (Scalar ty) := - fun i j => - match decEq i.val j.val with - | isTrue h => isTrue (Scalar.eq_of_val_eq h) - | isFalse h => isFalse (Scalar.ne_of_val_ne h) - -def Scalar.toInt {ty} (n : Scalar ty) : Int := n.val - --- Tactic to prove that integers are in bounds -syntax "intlit" : tactic - -macro_rules - | `(tactic| intlit) => `(tactic| apply Scalar.bound_suffices ; decide) - --- -- We now define a type class that subsumes the various machine integer types, so --- -- as to write a concise definition for scalar_cast, rather than exhaustively --- -- enumerating all of the possible pairs. We remark that Rust has sane semantics --- -- and fails if a cast operation would involve a truncation or modulo. - --- class MachineInteger (t: Type) where --- size: Nat --- val: t -> Fin size --- ofNatCore: (n:Nat) -> LT.lt n size -> t - --- set_option hygiene false in --- run_cmd --- for typeName in [`UInt8, `UInt16, `UInt32, `UInt64, `USize].map Lean.mkIdent do --- Lean.Elab.Command.elabCommand (← `( --- namespace $typeName --- instance: MachineInteger $typeName where --- size := size --- val := val --- ofNatCore := ofNatCore --- end $typeName --- )) - --- -- Aeneas only instantiates the destination type (`src` is implicit). We rely on --- -- Lean to infer `src`. - --- def scalar_cast { src: Type } (dst: Type) [ MachineInteger src ] [ MachineInteger dst ] (x: src): Result dst := --- if h: MachineInteger.val x < MachineInteger.size dst then --- .ret (MachineInteger.ofNatCore (MachineInteger.val x).val h) --- else --- .fail integerOverflow - -------------- --- VECTORS -- -------------- - -def Vec (α : Type u) := { l : List α // List.length l <= Usize.max } - -def vec_new (α : Type u): Vec α := ⟨ [], by sorry ⟩ - -def vec_len (α : Type u) (v : Vec α) : Usize := - let ⟨ v, l ⟩ := v - Usize.ofIntCore (List.length v) (by sorry) l - -def vec_push_fwd (α : Type u) (_ : Vec α) (_ : α) : Unit := () - -def vec_push_back (α : Type u) (v : Vec α) (x : α) : Result (Vec α) - := - if h : List.length v.val <= U32.max || List.length v.val <= Usize.max then - return ⟨ List.concat v.val x, by sorry ⟩ - else - fail maximumSizeExceeded - -def vec_insert_fwd (α : Type u) (v: Vec α) (i: Usize) (_: α): Result Unit := - if i.val < List.length v.val then - .ret () - else - .fail arrayOutOfBounds - -def vec_insert_back (α : Type u) (v: Vec α) (i: Usize) (x: α): Result (Vec α) := - if i.val < List.length v.val then - -- TODO: maybe we should redefine a list library which uses integers - -- (instead of natural numbers) - let i : Nat := - match i.val with - | .ofNat n => n - | .negSucc n => by sorry -- TODO: we can't get here - let isLt: i < USize.size := by sorry - let i : Fin USize.size := { val := i, isLt := isLt } - .ret ⟨ List.set v.val i.val x, by - have h: List.length v.val <= Usize.max := v.property - rewrite [ List.length_set v.val i.val x ] - assumption - ⟩ - else - .fail arrayOutOfBounds - -def vec_index_fwd (α : Type u) (v: Vec α) (i: Usize): Result α := - if i.val < List.length v.val then - let i : Nat := - match i.val with - | .ofNat n => n - | .negSucc n => by sorry -- TODO: we can't get here - let isLt: i < USize.size := by sorry - let i : Fin USize.size := { val := i, isLt := isLt } - let h: i < List.length v.val := by sorry - .ret (List.get v.val ⟨i.val, h⟩) - else - .fail arrayOutOfBounds - -def vec_index_back (α : Type u) (v: Vec α) (i: Usize) (_: α): Result Unit := - if i.val < List.length v.val then - .ret () - else - .fail arrayOutOfBounds - -def vec_index_mut_fwd (α : Type u) (v: Vec α) (i: Usize): Result α := - if i.val < List.length v.val then - let i : Nat := - match i.val with - | .ofNat n => n - | .negSucc n => by sorry -- TODO: we can't get here - let isLt: i < USize.size := by sorry - let i : Fin USize.size := { val := i, isLt := isLt } - let h: i < List.length v.val := by sorry - .ret (List.get v.val ⟨i.val, h⟩) - else - .fail arrayOutOfBounds - -def vec_index_mut_back (α : Type u) (v: Vec α) (i: Usize) (x: α): Result (Vec α) := - if i.val < List.length v.val then - let i : Nat := - match i.val with - | .ofNat n => n - | .negSucc n => by sorry -- TODO: we can't get here - let isLt: i < USize.size := by sorry - let i : Fin USize.size := { val := i, isLt := isLt } - .ret ⟨ List.set v.val i.val x, by - have h: List.length v.val <= Usize.max := v.property - rewrite [ List.length_set v.val i.val x ] - assumption - ⟩ - else - .fail arrayOutOfBounds - ----------- --- MISC -- ----------- - -def mem_replace_fwd (a : Type) (x : a) (_ : a) : a := - x - -def mem_replace_back (a : Type) (_ : a) (y : a) : a := - y - -/-- Aeneas-translated function -- useful to reduce non-recursive definitions. - Use with `simp [ aeneas ]` -/ -register_simp_attr aeneas diff --git a/tests/lean/hashmap/Hashmap.lean b/tests/lean/hashmap/Hashmap.lean deleted file mode 100644 index 41630205..00000000 --- a/tests/lean/hashmap/Hashmap.lean +++ /dev/null @@ -1 +0,0 @@ -import Hashmap.Funs diff --git a/tests/lean/hashmap/Hashmap/Clauses/Clauses.lean b/tests/lean/hashmap/Hashmap/Clauses/Clauses.lean deleted file mode 100644 index 197b0a6a..00000000 --- a/tests/lean/hashmap/Hashmap/Clauses/Clauses.lean +++ /dev/null @@ -1,107 +0,0 @@ --- [hashmap]: templates for the decreases clauses -import Base.Primitives -import Hashmap.Types - -/- [hashmap::HashMap::{0}::allocate_slots]: termination measure -/ -@[simp] -def hash_map_allocate_slots_loop_terminates (T : Type) (slots : Vec (list_t T)) - (n : Usize) := - (slots, n) - -/- [hashmap::HashMap::{0}::allocate_slots]: decreases_by tactic -/ -syntax "hash_map_allocate_slots_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hash_map_allocate_slots_loop_decreases $slots $n) =>`(tactic| sorry) - -/- [hashmap::HashMap::{0}::clear]: termination measure -/ -@[simp] -def hash_map_clear_loop_terminates (T : Type) (slots : Vec (list_t T)) - (i : Usize) := - (slots, i) - -/- [hashmap::HashMap::{0}::clear]: decreases_by tactic -/ -syntax "hash_map_clear_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hash_map_clear_loop_decreases $slots $i) =>`(tactic| sorry) - -/- [hashmap::HashMap::{0}::insert_in_list]: termination measure -/ -@[simp] -def hash_map_insert_in_list_loop_terminates (T : Type) (key : Usize) - (value : T) (ls : list_t T) := - (key, value, ls) - -/- [hashmap::HashMap::{0}::insert_in_list]: decreases_by tactic -/ -syntax "hash_map_insert_in_list_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hash_map_insert_in_list_loop_decreases $key $value $ls) => - `(tactic| sorry) - -/- [hashmap::HashMap::{0}::move_elements_from_list]: termination measure -/ -@[simp] -def hash_map_move_elements_from_list_loop_terminates (T : Type) - (ntable : hash_map_t T) (ls : list_t T) := - (ntable, ls) - -/- [hashmap::HashMap::{0}::move_elements_from_list]: decreases_by tactic -/ -syntax "hash_map_move_elements_from_list_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hash_map_move_elements_from_list_loop_decreases $ntable $ls) => - `(tactic| sorry) - -/- [hashmap::HashMap::{0}::move_elements]: termination measure -/ -@[simp] -def hash_map_move_elements_loop_terminates (T : Type) (ntable : hash_map_t T) - (slots : Vec (list_t T)) (i : Usize) := - (ntable, slots, i) - -/- [hashmap::HashMap::{0}::move_elements]: decreases_by tactic -/ -syntax "hash_map_move_elements_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hash_map_move_elements_loop_decreases $ntable $slots $i) => - `(tactic| sorry) - -/- [hashmap::HashMap::{0}::contains_key_in_list]: termination measure -/ -@[simp] -def hash_map_contains_key_in_list_loop_terminates (T : Type) (key : Usize) - (ls : list_t T) := - (key, ls) - -/- [hashmap::HashMap::{0}::contains_key_in_list]: decreases_by tactic -/ -syntax "hash_map_contains_key_in_list_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hash_map_contains_key_in_list_loop_decreases $key $ls) => - `(tactic| sorry) - -/- [hashmap::HashMap::{0}::get_in_list]: termination measure -/ -@[simp] -def hash_map_get_in_list_loop_terminates (T : Type) (key : Usize) - (ls : list_t T) := - (key, ls) - -/- [hashmap::HashMap::{0}::get_in_list]: decreases_by tactic -/ -syntax "hash_map_get_in_list_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hash_map_get_in_list_loop_decreases $key $ls) =>`(tactic| sorry) - -/- [hashmap::HashMap::{0}::get_mut_in_list]: termination measure -/ -@[simp] -def hash_map_get_mut_in_list_loop_terminates (T : Type) (ls : list_t T) - (key : Usize) := - (ls, key) - -/- [hashmap::HashMap::{0}::get_mut_in_list]: decreases_by tactic -/ -syntax "hash_map_get_mut_in_list_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hash_map_get_mut_in_list_loop_decreases $ls $key) =>`(tactic| sorry) - -/- [hashmap::HashMap::{0}::remove_from_list]: termination measure -/ -@[simp] -def hash_map_remove_from_list_loop_terminates (T : Type) (key : Usize) - (ls : list_t T) := - (key, ls) - -/- [hashmap::HashMap::{0}::remove_from_list]: decreases_by tactic -/ -syntax "hash_map_remove_from_list_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hash_map_remove_from_list_loop_decreases $key $ls) =>`(tactic| sorry) - diff --git a/tests/lean/hashmap/Hashmap/Clauses/Template.lean b/tests/lean/hashmap/Hashmap/Clauses/Template.lean deleted file mode 100644 index 560592c8..00000000 --- a/tests/lean/hashmap/Hashmap/Clauses/Template.lean +++ /dev/null @@ -1,108 +0,0 @@ --- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS --- [hashmap]: templates for the decreases clauses -import Base.Primitives -import Hashmap.Types - -/- [hashmap::HashMap::{0}::allocate_slots]: termination measure -/ -@[simp] -def hash_map_allocate_slots_loop_terminates (T : Type) (slots : Vec (list_t T)) - (n : Usize) := - (slots, n) - -/- [hashmap::HashMap::{0}::allocate_slots]: decreases_by tactic -/ -syntax "hash_map_allocate_slots_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hash_map_allocate_slots_loop_decreases $slots $n) =>`(tactic| sorry) - -/- [hashmap::HashMap::{0}::clear]: termination measure -/ -@[simp] -def hash_map_clear_loop_terminates (T : Type) (slots : Vec (list_t T)) - (i : Usize) := - (slots, i) - -/- [hashmap::HashMap::{0}::clear]: decreases_by tactic -/ -syntax "hash_map_clear_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hash_map_clear_loop_decreases $slots $i) =>`(tactic| sorry) - -/- [hashmap::HashMap::{0}::insert_in_list]: termination measure -/ -@[simp] -def hash_map_insert_in_list_loop_terminates (T : Type) (key : Usize) - (value : T) (ls : list_t T) := - (key, value, ls) - -/- [hashmap::HashMap::{0}::insert_in_list]: decreases_by tactic -/ -syntax "hash_map_insert_in_list_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hash_map_insert_in_list_loop_decreases $key $value $ls) => - `(tactic| sorry) - -/- [hashmap::HashMap::{0}::move_elements_from_list]: termination measure -/ -@[simp] -def hash_map_move_elements_from_list_loop_terminates (T : Type) - (ntable : hash_map_t T) (ls : list_t T) := - (ntable, ls) - -/- [hashmap::HashMap::{0}::move_elements_from_list]: decreases_by tactic -/ -syntax "hash_map_move_elements_from_list_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hash_map_move_elements_from_list_loop_decreases $ntable $ls) => - `(tactic| sorry) - -/- [hashmap::HashMap::{0}::move_elements]: termination measure -/ -@[simp] -def hash_map_move_elements_loop_terminates (T : Type) (ntable : hash_map_t T) - (slots : Vec (list_t T)) (i : Usize) := - (ntable, slots, i) - -/- [hashmap::HashMap::{0}::move_elements]: decreases_by tactic -/ -syntax "hash_map_move_elements_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hash_map_move_elements_loop_decreases $ntable $slots $i) => - `(tactic| sorry) - -/- [hashmap::HashMap::{0}::contains_key_in_list]: termination measure -/ -@[simp] -def hash_map_contains_key_in_list_loop_terminates (T : Type) (key : Usize) - (ls : list_t T) := - (key, ls) - -/- [hashmap::HashMap::{0}::contains_key_in_list]: decreases_by tactic -/ -syntax "hash_map_contains_key_in_list_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hash_map_contains_key_in_list_loop_decreases $key $ls) => - `(tactic| sorry) - -/- [hashmap::HashMap::{0}::get_in_list]: termination measure -/ -@[simp] -def hash_map_get_in_list_loop_terminates (T : Type) (key : Usize) - (ls : list_t T) := - (key, ls) - -/- [hashmap::HashMap::{0}::get_in_list]: decreases_by tactic -/ -syntax "hash_map_get_in_list_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hash_map_get_in_list_loop_decreases $key $ls) =>`(tactic| sorry) - -/- [hashmap::HashMap::{0}::get_mut_in_list]: termination measure -/ -@[simp] -def hash_map_get_mut_in_list_loop_terminates (T : Type) (ls : list_t T) - (key : Usize) := - (ls, key) - -/- [hashmap::HashMap::{0}::get_mut_in_list]: decreases_by tactic -/ -syntax "hash_map_get_mut_in_list_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hash_map_get_mut_in_list_loop_decreases $ls $key) =>`(tactic| sorry) - -/- [hashmap::HashMap::{0}::remove_from_list]: termination measure -/ -@[simp] -def hash_map_remove_from_list_loop_terminates (T : Type) (key : Usize) - (ls : list_t T) := - (key, ls) - -/- [hashmap::HashMap::{0}::remove_from_list]: decreases_by tactic -/ -syntax "hash_map_remove_from_list_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hash_map_remove_from_list_loop_decreases $key $ls) =>`(tactic| sorry) - diff --git a/tests/lean/hashmap/Hashmap/Funs.lean b/tests/lean/hashmap/Hashmap/Funs.lean deleted file mode 100644 index 77b1a157..00000000 --- a/tests/lean/hashmap/Hashmap/Funs.lean +++ /dev/null @@ -1,513 +0,0 @@ --- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS --- [hashmap]: function definitions -import Base.Primitives -import Hashmap.Types -import Hashmap.Clauses.Clauses - -/- [hashmap::hash_key] -/ -def hash_key_fwd (k : Usize) : Result Usize := - Result.ret k - -/- [hashmap::HashMap::{0}::allocate_slots] -/ -def hash_map_allocate_slots_loop_fwd - (T : Type) (slots : Vec (list_t T)) (n : Usize) : - (Result (Vec (list_t T))) - := - if h: n > (Usize.ofInt 0 (by intlit)) - then - do - let slots0 ← vec_push_back (list_t T) slots list_t.Nil - let n0 ← n - (Usize.ofInt 1 (by intlit)) - hash_map_allocate_slots_loop_fwd T slots0 n0 - else Result.ret slots -termination_by hash_map_allocate_slots_loop_fwd slots n => - hash_map_allocate_slots_loop_terminates T slots n -decreasing_by hash_map_allocate_slots_loop_decreases slots n - -/- [hashmap::HashMap::{0}::allocate_slots] -/ -def hash_map_allocate_slots_fwd - (T : Type) (slots : Vec (list_t T)) (n : Usize) : Result (Vec (list_t T)) := - hash_map_allocate_slots_loop_fwd T slots n - -/- [hashmap::HashMap::{0}::new_with_capacity] -/ -def hash_map_new_with_capacity_fwd - (T : Type) (capacity : Usize) (max_load_dividend : Usize) - (max_load_divisor : Usize) : - Result (hash_map_t T) - := - do - let v := vec_new (list_t T) - let slots ← hash_map_allocate_slots_fwd T v capacity - let i ← capacity * max_load_dividend - let i0 ← i / max_load_divisor - Result.ret - { - hash_map_num_entries := (Usize.ofInt 0 (by intlit)), - hash_map_max_load_factor := (max_load_dividend, max_load_divisor), - hash_map_max_load := i0, - hash_map_slots := slots - } - -/- [hashmap::HashMap::{0}::new] -/ -def hash_map_new_fwd (T : Type) : Result (hash_map_t T) := - hash_map_new_with_capacity_fwd T (Usize.ofInt 32 (by intlit)) - (Usize.ofInt 4 (by intlit)) (Usize.ofInt 5 (by intlit)) - -/- [hashmap::HashMap::{0}::clear] -/ -def hash_map_clear_loop_fwd_back - (T : Type) (slots : Vec (list_t T)) (i : Usize) : - (Result (Vec (list_t T))) - := - let i0 := vec_len (list_t T) slots - if h: i < i0 - then - do - let i1 ← i + (Usize.ofInt 1 (by intlit)) - let slots0 ← vec_index_mut_back (list_t T) slots i list_t.Nil - hash_map_clear_loop_fwd_back T slots0 i1 - else Result.ret slots -termination_by hash_map_clear_loop_fwd_back slots i => - hash_map_clear_loop_terminates T slots i -decreasing_by hash_map_clear_loop_decreases slots i - -/- [hashmap::HashMap::{0}::clear] -/ -def hash_map_clear_fwd_back - (T : Type) (self : hash_map_t T) : Result (hash_map_t T) := - do - let v ← - hash_map_clear_loop_fwd_back T self.hash_map_slots - (Usize.ofInt 0 (by intlit)) - Result.ret - { - self - with - hash_map_num_entries := (Usize.ofInt 0 (by intlit)), - hash_map_slots := v - } - -/- [hashmap::HashMap::{0}::len] -/ -def hash_map_len_fwd (T : Type) (self : hash_map_t T) : Result Usize := - Result.ret self.hash_map_num_entries - -/- [hashmap::HashMap::{0}::insert_in_list] -/ -def hash_map_insert_in_list_loop_fwd - (T : Type) (key : Usize) (value : T) (ls : list_t T) : (Result Bool) := - match h: ls with - | list_t.Cons ckey cvalue tl => - if h: ckey = key - then Result.ret false - else hash_map_insert_in_list_loop_fwd T key value tl - | list_t.Nil => Result.ret true -termination_by hash_map_insert_in_list_loop_fwd key value ls => - hash_map_insert_in_list_loop_terminates T key value ls -decreasing_by hash_map_insert_in_list_loop_decreases key value ls - -/- [hashmap::HashMap::{0}::insert_in_list] -/ -def hash_map_insert_in_list_fwd - (T : Type) (key : Usize) (value : T) (ls : list_t T) : Result Bool := - hash_map_insert_in_list_loop_fwd T key value ls - -/- [hashmap::HashMap::{0}::insert_in_list] -/ -def hash_map_insert_in_list_loop_back - (T : Type) (key : Usize) (value : T) (ls : list_t T) : (Result (list_t T)) := - match h: ls with - | list_t.Cons ckey cvalue tl => - if h: ckey = key - then Result.ret (list_t.Cons ckey value tl) - else - do - let tl0 ← hash_map_insert_in_list_loop_back T key value tl - Result.ret (list_t.Cons ckey cvalue tl0) - | list_t.Nil => let l := list_t.Nil - Result.ret (list_t.Cons key value l) -termination_by hash_map_insert_in_list_loop_back key value ls => - hash_map_insert_in_list_loop_terminates T key value ls -decreasing_by hash_map_insert_in_list_loop_decreases key value ls - -/- [hashmap::HashMap::{0}::insert_in_list] -/ -def hash_map_insert_in_list_back - (T : Type) (key : Usize) (value : T) (ls : list_t T) : Result (list_t T) := - hash_map_insert_in_list_loop_back T key value ls - -/- [hashmap::HashMap::{0}::insert_no_resize] -/ -def hash_map_insert_no_resize_fwd_back - (T : Type) (self : hash_map_t T) (key : Usize) (value : T) : - Result (hash_map_t T) - := - do - let hash ← hash_key_fwd key - let i := vec_len (list_t T) self.hash_map_slots - let hash_mod ← hash % i - let l ← vec_index_mut_fwd (list_t T) self.hash_map_slots hash_mod - let inserted ← hash_map_insert_in_list_fwd T key value l - if h: inserted - then - do - let i0 ← self.hash_map_num_entries + (Usize.ofInt 1 (by intlit)) - let l0 ← hash_map_insert_in_list_back T key value l - let v ← vec_index_mut_back (list_t T) self.hash_map_slots hash_mod l0 - Result.ret - { self with hash_map_num_entries := i0, hash_map_slots := v } - else - do - let l0 ← hash_map_insert_in_list_back T key value l - let v ← vec_index_mut_back (list_t T) self.hash_map_slots hash_mod l0 - Result.ret { self with hash_map_slots := v } - -/- [core::num::u32::{9}::MAX] -/ -def core_num_u32_max_body : Result U32 := - Result.ret (U32.ofInt 4294967295 (by intlit)) -def core_num_u32_max_c : U32 := eval_global core_num_u32_max_body (by simp) - -/- [hashmap::HashMap::{0}::move_elements_from_list] -/ -def hash_map_move_elements_from_list_loop_fwd_back - (T : Type) (ntable : hash_map_t T) (ls : list_t T) : - (Result (hash_map_t T)) - := - match h: ls with - | list_t.Cons k v tl => - do - let ntable0 ← hash_map_insert_no_resize_fwd_back T ntable k v - hash_map_move_elements_from_list_loop_fwd_back T ntable0 tl - | list_t.Nil => Result.ret ntable -termination_by hash_map_move_elements_from_list_loop_fwd_back ntable ls => - hash_map_move_elements_from_list_loop_terminates T ntable ls -decreasing_by hash_map_move_elements_from_list_loop_decreases ntable ls - -/- [hashmap::HashMap::{0}::move_elements_from_list] -/ -def hash_map_move_elements_from_list_fwd_back - (T : Type) (ntable : hash_map_t T) (ls : list_t T) : Result (hash_map_t T) := - hash_map_move_elements_from_list_loop_fwd_back T ntable ls - -/- [hashmap::HashMap::{0}::move_elements] -/ -def hash_map_move_elements_loop_fwd_back - (T : Type) (ntable : hash_map_t T) (slots : Vec (list_t T)) (i : Usize) : - (Result ((hash_map_t T) × (Vec (list_t T)))) - := - let i0 := vec_len (list_t T) slots - if h: i < i0 - then - do - let l ← vec_index_mut_fwd (list_t T) slots i - let ls := mem_replace_fwd (list_t T) l list_t.Nil - let ntable0 ← hash_map_move_elements_from_list_fwd_back T ntable ls - let i1 ← i + (Usize.ofInt 1 (by intlit)) - let l0 := mem_replace_back (list_t T) l list_t.Nil - let slots0 ← vec_index_mut_back (list_t T) slots i l0 - hash_map_move_elements_loop_fwd_back T ntable0 slots0 i1 - else Result.ret (ntable, slots) -termination_by hash_map_move_elements_loop_fwd_back ntable slots i => - hash_map_move_elements_loop_terminates T ntable slots i -decreasing_by hash_map_move_elements_loop_decreases ntable slots i - -/- [hashmap::HashMap::{0}::move_elements] -/ -def hash_map_move_elements_fwd_back - (T : Type) (ntable : hash_map_t T) (slots : Vec (list_t T)) (i : Usize) : - Result ((hash_map_t T) × (Vec (list_t T))) - := - hash_map_move_elements_loop_fwd_back T ntable slots i - -/- [hashmap::HashMap::{0}::try_resize] -/ -def hash_map_try_resize_fwd_back - (T : Type) (self : hash_map_t T) : Result (hash_map_t T) := - do - let max_usize ← Scalar.cast .Usize core_num_u32_max_c - let capacity := vec_len (list_t T) self.hash_map_slots - let n1 ← max_usize / (Usize.ofInt 2 (by intlit)) - let (i, i0) := self.hash_map_max_load_factor - let i1 ← n1 / i - if h: capacity <= i1 - then - do - let i2 ← capacity * (Usize.ofInt 2 (by intlit)) - let ntable ← hash_map_new_with_capacity_fwd T i2 i i0 - let (ntable0, _) ← - hash_map_move_elements_fwd_back T ntable self.hash_map_slots - (Usize.ofInt 0 (by intlit)) - Result.ret - { - ntable0 - with - hash_map_num_entries := self.hash_map_num_entries, - hash_map_max_load_factor := (i, i0) - } - else Result.ret { self with hash_map_max_load_factor := (i, i0) } - -/- [hashmap::HashMap::{0}::insert] -/ -def hash_map_insert_fwd_back - (T : Type) (self : hash_map_t T) (key : Usize) (value : T) : - Result (hash_map_t T) - := - do - let self0 ← hash_map_insert_no_resize_fwd_back T self key value - let i ← hash_map_len_fwd T self0 - if h: i > self0.hash_map_max_load - then hash_map_try_resize_fwd_back T self0 - else Result.ret self0 - -/- [hashmap::HashMap::{0}::contains_key_in_list] -/ -def hash_map_contains_key_in_list_loop_fwd - (T : Type) (key : Usize) (ls : list_t T) : (Result Bool) := - match h: ls with - | list_t.Cons ckey t tl => - if h: ckey = key - then Result.ret true - else hash_map_contains_key_in_list_loop_fwd T key tl - | list_t.Nil => Result.ret false -termination_by hash_map_contains_key_in_list_loop_fwd key ls => - hash_map_contains_key_in_list_loop_terminates T key ls -decreasing_by hash_map_contains_key_in_list_loop_decreases key ls - -/- [hashmap::HashMap::{0}::contains_key_in_list] -/ -def hash_map_contains_key_in_list_fwd - (T : Type) (key : Usize) (ls : list_t T) : Result Bool := - hash_map_contains_key_in_list_loop_fwd T key ls - -/- [hashmap::HashMap::{0}::contains_key] -/ -def hash_map_contains_key_fwd - (T : Type) (self : hash_map_t T) (key : Usize) : Result Bool := - do - let hash ← hash_key_fwd key - let i := vec_len (list_t T) self.hash_map_slots - let hash_mod ← hash % i - let l ← vec_index_fwd (list_t T) self.hash_map_slots hash_mod - hash_map_contains_key_in_list_fwd T key l - -/- [hashmap::HashMap::{0}::get_in_list] -/ -def hash_map_get_in_list_loop_fwd - (T : Type) (key : Usize) (ls : list_t T) : (Result T) := - match h: ls with - | list_t.Cons ckey cvalue tl => - if h: ckey = key - then Result.ret cvalue - else hash_map_get_in_list_loop_fwd T key tl - | list_t.Nil => Result.fail Error.panic -termination_by hash_map_get_in_list_loop_fwd key ls => - hash_map_get_in_list_loop_terminates T key ls -decreasing_by hash_map_get_in_list_loop_decreases key ls - -/- [hashmap::HashMap::{0}::get_in_list] -/ -def hash_map_get_in_list_fwd - (T : Type) (key : Usize) (ls : list_t T) : Result T := - hash_map_get_in_list_loop_fwd T key ls - -/- [hashmap::HashMap::{0}::get] -/ -def hash_map_get_fwd - (T : Type) (self : hash_map_t T) (key : Usize) : Result T := - do - let hash ← hash_key_fwd key - let i := vec_len (list_t T) self.hash_map_slots - let hash_mod ← hash % i - let l ← vec_index_fwd (list_t T) self.hash_map_slots hash_mod - hash_map_get_in_list_fwd T key l - -/- [hashmap::HashMap::{0}::get_mut_in_list] -/ -def hash_map_get_mut_in_list_loop_fwd - (T : Type) (ls : list_t T) (key : Usize) : (Result T) := - match h: ls with - | list_t.Cons ckey cvalue tl => - if h: ckey = key - then Result.ret cvalue - else hash_map_get_mut_in_list_loop_fwd T tl key - | list_t.Nil => Result.fail Error.panic -termination_by hash_map_get_mut_in_list_loop_fwd ls key => - hash_map_get_mut_in_list_loop_terminates T ls key -decreasing_by hash_map_get_mut_in_list_loop_decreases ls key - -/- [hashmap::HashMap::{0}::get_mut_in_list] -/ -def hash_map_get_mut_in_list_fwd - (T : Type) (ls : list_t T) (key : Usize) : Result T := - hash_map_get_mut_in_list_loop_fwd T ls key - -/- [hashmap::HashMap::{0}::get_mut_in_list] -/ -def hash_map_get_mut_in_list_loop_back - (T : Type) (ls : list_t T) (key : Usize) (ret0 : T) : (Result (list_t T)) := - match h: ls with - | list_t.Cons ckey cvalue tl => - if h: ckey = key - then Result.ret (list_t.Cons ckey ret0 tl) - else - do - let tl0 ← hash_map_get_mut_in_list_loop_back T tl key ret0 - Result.ret (list_t.Cons ckey cvalue tl0) - | list_t.Nil => Result.fail Error.panic -termination_by hash_map_get_mut_in_list_loop_back ls key ret0 => - hash_map_get_mut_in_list_loop_terminates T ls key -decreasing_by hash_map_get_mut_in_list_loop_decreases ls key - -/- [hashmap::HashMap::{0}::get_mut_in_list] -/ -def hash_map_get_mut_in_list_back - (T : Type) (ls : list_t T) (key : Usize) (ret0 : T) : Result (list_t T) := - hash_map_get_mut_in_list_loop_back T ls key ret0 - -/- [hashmap::HashMap::{0}::get_mut] -/ -def hash_map_get_mut_fwd - (T : Type) (self : hash_map_t T) (key : Usize) : Result T := - do - let hash ← hash_key_fwd key - let i := vec_len (list_t T) self.hash_map_slots - let hash_mod ← hash % i - let l ← vec_index_mut_fwd (list_t T) self.hash_map_slots hash_mod - hash_map_get_mut_in_list_fwd T l key - -/- [hashmap::HashMap::{0}::get_mut] -/ -def hash_map_get_mut_back - (T : Type) (self : hash_map_t T) (key : Usize) (ret0 : T) : - Result (hash_map_t T) - := - do - let hash ← hash_key_fwd key - let i := vec_len (list_t T) self.hash_map_slots - let hash_mod ← hash % i - let l ← vec_index_mut_fwd (list_t T) self.hash_map_slots hash_mod - let l0 ← hash_map_get_mut_in_list_back T l key ret0 - let v ← vec_index_mut_back (list_t T) self.hash_map_slots hash_mod l0 - Result.ret { self with hash_map_slots := v } - -/- [hashmap::HashMap::{0}::remove_from_list] -/ -def hash_map_remove_from_list_loop_fwd - (T : Type) (key : Usize) (ls : list_t T) : (Result (Option T)) := - match h: ls with - | list_t.Cons ckey t tl => - if h: ckey = key - then - let mv_ls := - mem_replace_fwd (list_t T) (list_t.Cons ckey t tl) list_t.Nil - match h: mv_ls with - | list_t.Cons i cvalue tl0 => Result.ret (Option.some cvalue) - | list_t.Nil => Result.fail Error.panic - else hash_map_remove_from_list_loop_fwd T key tl - | list_t.Nil => Result.ret Option.none -termination_by hash_map_remove_from_list_loop_fwd key ls => - hash_map_remove_from_list_loop_terminates T key ls -decreasing_by hash_map_remove_from_list_loop_decreases key ls - -/- [hashmap::HashMap::{0}::remove_from_list] -/ -def hash_map_remove_from_list_fwd - (T : Type) (key : Usize) (ls : list_t T) : Result (Option T) := - hash_map_remove_from_list_loop_fwd T key ls - -/- [hashmap::HashMap::{0}::remove_from_list] -/ -def hash_map_remove_from_list_loop_back - (T : Type) (key : Usize) (ls : list_t T) : (Result (list_t T)) := - match h: ls with - | list_t.Cons ckey t tl => - if h: ckey = key - then - let mv_ls := - mem_replace_fwd (list_t T) (list_t.Cons ckey t tl) list_t.Nil - match h: mv_ls with - | list_t.Cons i cvalue tl0 => Result.ret tl0 - | list_t.Nil => Result.fail Error.panic - else - do - let tl0 ← hash_map_remove_from_list_loop_back T key tl - Result.ret (list_t.Cons ckey t tl0) - | list_t.Nil => Result.ret list_t.Nil -termination_by hash_map_remove_from_list_loop_back key ls => - hash_map_remove_from_list_loop_terminates T key ls -decreasing_by hash_map_remove_from_list_loop_decreases key ls - -/- [hashmap::HashMap::{0}::remove_from_list] -/ -def hash_map_remove_from_list_back - (T : Type) (key : Usize) (ls : list_t T) : Result (list_t T) := - hash_map_remove_from_list_loop_back T key ls - -/- [hashmap::HashMap::{0}::remove] -/ -def hash_map_remove_fwd - (T : Type) (self : hash_map_t T) (key : Usize) : Result (Option T) := - do - let hash ← hash_key_fwd key - let i := vec_len (list_t T) self.hash_map_slots - let hash_mod ← hash % i - let l ← vec_index_mut_fwd (list_t T) self.hash_map_slots hash_mod - let x ← hash_map_remove_from_list_fwd T key l - match h: x with - | Option.none => Result.ret Option.none - | Option.some x0 => - do - let _ ← self.hash_map_num_entries - (Usize.ofInt 1 (by intlit)) - Result.ret (Option.some x0) - -/- [hashmap::HashMap::{0}::remove] -/ -def hash_map_remove_back - (T : Type) (self : hash_map_t T) (key : Usize) : Result (hash_map_t T) := - do - let hash ← hash_key_fwd key - let i := vec_len (list_t T) self.hash_map_slots - let hash_mod ← hash % i - let l ← vec_index_mut_fwd (list_t T) self.hash_map_slots hash_mod - let x ← hash_map_remove_from_list_fwd T key l - match h: x with - | Option.none => - do - let l0 ← hash_map_remove_from_list_back T key l - let v ← vec_index_mut_back (list_t T) self.hash_map_slots hash_mod l0 - Result.ret { self with hash_map_slots := v } - | Option.some x0 => - do - let i0 ← self.hash_map_num_entries - (Usize.ofInt 1 (by intlit)) - let l0 ← hash_map_remove_from_list_back T key l - let v ← vec_index_mut_back (list_t T) self.hash_map_slots hash_mod l0 - Result.ret - { self with hash_map_num_entries := i0, hash_map_slots := v } - -/- [hashmap::test1] -/ -def test1_fwd : Result Unit := - do - let hm ← hash_map_new_fwd U64 - let hm0 ← - hash_map_insert_fwd_back U64 hm (Usize.ofInt 0 (by intlit)) - (U64.ofInt 42 (by intlit)) - let hm1 ← - hash_map_insert_fwd_back U64 hm0 (Usize.ofInt 128 (by intlit)) - (U64.ofInt 18 (by intlit)) - let hm2 ← - hash_map_insert_fwd_back U64 hm1 (Usize.ofInt 1024 (by intlit)) - (U64.ofInt 138 (by intlit)) - let hm3 ← - hash_map_insert_fwd_back U64 hm2 (Usize.ofInt 1056 (by intlit)) - (U64.ofInt 256 (by intlit)) - let i ← hash_map_get_fwd U64 hm3 (Usize.ofInt 128 (by intlit)) - if h: not (i = (U64.ofInt 18 (by intlit))) - then Result.fail Error.panic - else - do - let hm4 ← - hash_map_get_mut_back U64 hm3 (Usize.ofInt 1024 (by intlit)) - (U64.ofInt 56 (by intlit)) - let i0 ← hash_map_get_fwd U64 hm4 (Usize.ofInt 1024 (by intlit)) - if h: not (i0 = (U64.ofInt 56 (by intlit))) - then Result.fail Error.panic - else - do - let x ← - hash_map_remove_fwd U64 hm4 (Usize.ofInt 1024 (by intlit)) - match h: x with - | Option.none => Result.fail Error.panic - | Option.some x0 => - if h: not (x0 = (U64.ofInt 56 (by intlit))) - then Result.fail Error.panic - else - do - let hm5 ← - hash_map_remove_back U64 hm4 (Usize.ofInt 1024 (by intlit)) - let i1 ← - hash_map_get_fwd U64 hm5 (Usize.ofInt 0 (by intlit)) - if h: not (i1 = (U64.ofInt 42 (by intlit))) - then Result.fail Error.panic - else - do - let i2 ← - hash_map_get_fwd U64 hm5 (Usize.ofInt 128 (by intlit)) - if h: not (i2 = (U64.ofInt 18 (by intlit))) - then Result.fail Error.panic - else - do - let i3 ← - hash_map_get_fwd U64 hm5 - (Usize.ofInt 1056 (by intlit)) - if h: not (i3 = (U64.ofInt 256 (by intlit))) - then Result.fail Error.panic - else Result.ret () - diff --git a/tests/lean/hashmap/Hashmap/Types.lean b/tests/lean/hashmap/Hashmap/Types.lean deleted file mode 100644 index 6eabf7da..00000000 --- a/tests/lean/hashmap/Hashmap/Types.lean +++ /dev/null @@ -1,16 +0,0 @@ --- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS --- [hashmap]: type definitions -import Base.Primitives - -/- [hashmap::List] -/ -inductive list_t (T : Type) := -| Cons : Usize -> T -> list_t T -> list_t T -| Nil : list_t T - -/- [hashmap::HashMap] -/ -structure hash_map_t (T : Type) where - hash_map_num_entries : Usize - hash_map_max_load_factor : (Usize × Usize) - hash_map_max_load : Usize - hash_map_slots : Vec (list_t T) - diff --git a/tests/lean/hashmap/lake-manifest.json b/tests/lean/hashmap/lake-manifest.json deleted file mode 100644 index 88e446e5..00000000 --- a/tests/lean/hashmap/lake-manifest.json +++ /dev/null @@ -1,27 +0,0 @@ -{"version": 4, - "packagesDir": "./lake-packages", - "packages": - [{"git": - {"url": "https://github.com/leanprover-community/mathlib4.git", - "subDir?": null, - "rev": "1c5ed7840906e29e1f8ca7dbf088cf155e5397e9", - "name": "mathlib", - "inputRev?": null}}, - {"git": - {"url": "https://github.com/gebner/quote4", - "subDir?": null, - "rev": "2412c4fdf4a8b689f4467618e5e7b371ae5014aa", - "name": "Qq", - "inputRev?": "master"}}, - {"git": - {"url": "https://github.com/JLimperg/aesop", - "subDir?": null, - "rev": "7fe9ecd9339b0e1796e89d243b776849c305c690", - "name": "aesop", - "inputRev?": "master"}}, - {"git": - {"url": "https://github.com/leanprover/std4", - "subDir?": null, - "rev": "24897887905b3a1254b244369f5dd2cf6174b0ee", - "name": "std", - "inputRev?": "main"}}]} diff --git a/tests/lean/hashmap/lakefile.lean b/tests/lean/hashmap/lakefile.lean deleted file mode 100644 index 713785f6..00000000 --- a/tests/lean/hashmap/lakefile.lean +++ /dev/null @@ -1,12 +0,0 @@ -import Lake -open Lake DSL - -require mathlib from git - "https://github.com/leanprover-community/mathlib4.git" - -package «hashmap» {} - -lean_lib «Base» {} - -@[default_target] -lean_lib «Hashmap» {} diff --git a/tests/lean/hashmap/lean-toolchain b/tests/lean/hashmap/lean-toolchain deleted file mode 100644 index bbf57f10..00000000 --- a/tests/lean/hashmap/lean-toolchain +++ /dev/null @@ -1 +0,0 @@ -leanprover/lean4:nightly-2023-01-21 diff --git a/tests/lean/hashmap_on_disk/.gitignore b/tests/lean/hashmap_on_disk/.gitignore deleted file mode 100644 index a1735e7c..00000000 --- a/tests/lean/hashmap_on_disk/.gitignore +++ /dev/null @@ -1,5 +0,0 @@ -/build -/lean_packages/* -!/lean_packages/manifest.json -/build -/lake-packages/* diff --git a/tests/lean/hashmap_on_disk/Base/Primitives.lean b/tests/lean/hashmap_on_disk/Base/Primitives.lean deleted file mode 100644 index 4a66a453..00000000 --- a/tests/lean/hashmap_on_disk/Base/Primitives.lean +++ /dev/null @@ -1,583 +0,0 @@ -import Lean -import Lean.Meta.Tactic.Simp -import Init.Data.List.Basic -import Mathlib.Tactic.RunCmd - --------------------- --- ASSERT COMMAND -- --------------------- - -open Lean Elab Command Term Meta - -syntax (name := assert) "#assert" term: command - -@[command_elab assert] -unsafe -def assertImpl : CommandElab := fun (_stx: Syntax) => do - runTermElabM (fun _ => do - let r ← evalTerm Bool (mkConst ``Bool) _stx[1] - if not r then - logInfo "Assertion failed for: " - logInfo _stx[1] - logError "Expression reduced to false" - pure ()) - -#eval 2 == 2 -#assert (2 == 2) - -------------- --- PRELUDE -- -------------- - --- Results & monadic combinators - -inductive Error where - | assertionFailure: Error - | integerOverflow: Error - | divisionByZero: Error - | arrayOutOfBounds: Error - | maximumSizeExceeded: Error - | panic: Error -deriving Repr, BEq - -open Error - -inductive Result (α : Type u) where - | ret (v: α): Result α - | fail (e: Error): Result α -deriving Repr, BEq - -open Result - -instance Result_Inhabited (α : Type u) : Inhabited (Result α) := - Inhabited.mk (fail panic) - -/- HELPERS -/ - -def ret? {α: Type} (r: Result α): Bool := - match r with - | Result.ret _ => true - | Result.fail _ => false - -def massert (b:Bool) : Result Unit := - if b then .ret () else fail assertionFailure - -def eval_global {α: Type} (x: Result α) (_: ret? x): α := - match x with - | Result.fail _ => by contradiction - | Result.ret x => x - -/- DO-DSL SUPPORT -/ - -def bind (x: Result α) (f: α -> Result β) : Result β := - match x with - | ret v => f v - | fail v => fail v - --- Allows using Result in do-blocks -instance : Bind Result where - bind := bind - --- Allows using return x in do-blocks -instance : Pure Result where - pure := fun x => ret x - -/- CUSTOM-DSL SUPPORT -/ - --- Let-binding the Result of a monadic operation is oftentimes not sufficient, --- because we may need a hypothesis for equational reasoning in the scope. We --- rely on subtype, and a custom let-binding operator, in effect recreating our --- own variant of the do-dsl - -def Result.attach {α: Type} (o : Result α): Result { x : α // o = ret x } := - match o with - | .ret x => .ret ⟨x, rfl⟩ - | .fail e => .fail e - -macro "let" e:term " ⟵ " f:term : doElem => - `(doElem| let ⟨$e, h⟩ ← Result.attach $f) - --- TODO: any way to factorize both definitions? -macro "let" e:term " <-- " f:term : doElem => - `(doElem| let ⟨$e, h⟩ ← Result.attach $f) - --- We call the hypothesis `h`, in effect making it unavailable to the user --- (because too much shadowing). But in practice, once can use the French single --- quote notation (input with f< and f>), where `‹ h ›` finds a suitable --- hypothesis in the context, this is equivalent to `have x: h := by assumption in x` -#eval do - let y <-- .ret (0: Nat) - let _: y = 0 := by cases ‹ ret 0 = ret y › ; decide - let r: { x: Nat // x = 0 } := ⟨ y, by assumption ⟩ - .ret r - ----------------------- --- MACHINE INTEGERS -- ----------------------- - --- We redefine our machine integers types. - --- For Isize/Usize, we reuse `getNumBits` from `USize`. You cannot reduce `getNumBits` --- using the simplifier, meaning that proofs do not depend on the compile-time value of --- USize.size. (Lean assumes 32 or 64-bit platforms, and Rust doesn't really support, at --- least officially, 16-bit microcontrollers, so this seems like a fine design decision --- for now.) - --- Note from Chris Bailey: "If there's more than one salient property of your --- definition then the subtyping strategy might get messy, and the property part --- of a subtype is less discoverable by the simplifier or tactics like --- library_search." So, we will not add refinements on the return values of the --- operations defined on Primitives, but will rather rely on custom lemmas to --- invert on possible return values of the primitive operations. - --- Machine integer constants, done via `ofNatCore`, which requires a proof that --- the `Nat` fits within the desired integer type. We provide a custom tactic. - -open System.Platform.getNumBits - --- TODO: is there a way of only importing System.Platform.getNumBits? --- -@[simp] def size_num_bits : Nat := (System.Platform.getNumBits ()).val - --- Remark: Lean seems to use < for the comparisons with the upper bounds by convention. --- We keep the F* convention for now. -@[simp] def Isize.min : Int := - (HPow.hPow 2 (size_num_bits - 1)) -@[simp] def Isize.max : Int := (HPow.hPow 2 (size_num_bits - 1)) - 1 -@[simp] def I8.min : Int := - (HPow.hPow 2 7) -@[simp] def I8.max : Int := HPow.hPow 2 7 - 1 -@[simp] def I16.min : Int := - (HPow.hPow 2 15) -@[simp] def I16.max : Int := HPow.hPow 2 15 - 1 -@[simp] def I32.min : Int := -(HPow.hPow 2 31) -@[simp] def I32.max : Int := HPow.hPow 2 31 - 1 -@[simp] def I64.min : Int := -(HPow.hPow 2 63) -@[simp] def I64.max : Int := HPow.hPow 2 63 - 1 -@[simp] def I128.min : Int := -(HPow.hPow 2 127) -@[simp] def I128.max : Int := HPow.hPow 2 127 - 1 -@[simp] def Usize.min : Int := 0 -@[simp] def Usize.max : Int := HPow.hPow 2 size_num_bits - 1 -@[simp] def U8.min : Int := 0 -@[simp] def U8.max : Int := HPow.hPow 2 8 - 1 -@[simp] def U16.min : Int := 0 -@[simp] def U16.max : Int := HPow.hPow 2 16 - 1 -@[simp] def U32.min : Int := 0 -@[simp] def U32.max : Int := HPow.hPow 2 32 - 1 -@[simp] def U64.min : Int := 0 -@[simp] def U64.max : Int := HPow.hPow 2 64 - 1 -@[simp] def U128.min : Int := 0 -@[simp] def U128.max : Int := HPow.hPow 2 128 - 1 - -#assert (I8.min == -128) -#assert (I8.max == 127) -#assert (I16.min == -32768) -#assert (I16.max == 32767) -#assert (I32.min == -2147483648) -#assert (I32.max == 2147483647) -#assert (I64.min == -9223372036854775808) -#assert (I64.max == 9223372036854775807) -#assert (I128.min == -170141183460469231731687303715884105728) -#assert (I128.max == 170141183460469231731687303715884105727) -#assert (U8.min == 0) -#assert (U8.max == 255) -#assert (U16.min == 0) -#assert (U16.max == 65535) -#assert (U32.min == 0) -#assert (U32.max == 4294967295) -#assert (U64.min == 0) -#assert (U64.max == 18446744073709551615) -#assert (U128.min == 0) -#assert (U128.max == 340282366920938463463374607431768211455) - -inductive ScalarTy := -| Isize -| I8 -| I16 -| I32 -| I64 -| I128 -| Usize -| U8 -| U16 -| U32 -| U64 -| U128 - -def Scalar.min (ty : ScalarTy) : Int := - match ty with - | .Isize => Isize.min - | .I8 => I8.min - | .I16 => I16.min - | .I32 => I32.min - | .I64 => I64.min - | .I128 => I128.min - | .Usize => Usize.min - | .U8 => U8.min - | .U16 => U16.min - | .U32 => U32.min - | .U64 => U64.min - | .U128 => U128.min - -def Scalar.max (ty : ScalarTy) : Int := - match ty with - | .Isize => Isize.max - | .I8 => I8.max - | .I16 => I16.max - | .I32 => I32.max - | .I64 => I64.max - | .I128 => I128.max - | .Usize => Usize.max - | .U8 => U8.max - | .U16 => U16.max - | .U32 => U32.max - | .U64 => U64.max - | .U128 => U128.max - --- "Conservative" bounds --- We use those because we can't compare to the isize bounds (which can't --- reduce at compile-time). Whenever we perform an arithmetic operation like --- addition we need to check that the result is in bounds: we first compare --- to the conservative bounds, which reduce, then compare to the real bounds. --- This is useful for the various #asserts that we want to reduce at --- type-checking time. -def Scalar.cMin (ty : ScalarTy) : Int := - match ty with - | .Isize => I32.min - | _ => Scalar.min ty - -def Scalar.cMax (ty : ScalarTy) : Int := - match ty with - | .Isize => I32.max - | .Usize => U32.max - | _ => Scalar.max ty - -theorem Scalar.cMin_bound ty : Scalar.min ty <= Scalar.cMin ty := by sorry -theorem Scalar.cMax_bound ty : Scalar.min ty <= Scalar.cMin ty := by sorry - -structure Scalar (ty : ScalarTy) where - val : Int - hmin : Scalar.min ty <= val - hmax : val <= Scalar.max ty - -theorem Scalar.bound_suffices (ty : ScalarTy) (x : Int) : - Scalar.cMin ty <= x && x <= Scalar.cMax ty -> - (decide (Scalar.min ty ≤ x) && decide (x ≤ Scalar.max ty)) = true - := by sorry - -def Scalar.ofIntCore {ty : ScalarTy} (x : Int) - (hmin : Scalar.min ty <= x) (hmax : x <= Scalar.max ty) : Scalar ty := - { val := x, hmin := hmin, hmax := hmax } - -def Scalar.ofInt {ty : ScalarTy} (x : Int) - (h : Scalar.min ty <= x && x <= Scalar.max ty) : Scalar ty := - let hmin: Scalar.min ty <= x := by sorry - let hmax: x <= Scalar.max ty := by sorry - Scalar.ofIntCore x hmin hmax - --- Further thoughts: look at what has been done here: --- https://github.com/leanprover-community/mathlib4/blob/master/Mathlib/Data/Fin/Basic.lean --- and --- https://github.com/leanprover-community/mathlib4/blob/master/Mathlib/Data/UInt.lean --- which both contain a fair amount of reasoning already! -def Scalar.tryMk (ty : ScalarTy) (x : Int) : Result (Scalar ty) := - -- TODO: write this with only one if then else - if hmin_cons: Scalar.cMin ty <= x || Scalar.min ty <= x then - if hmax_cons: x <= Scalar.cMax ty || x <= Scalar.max ty then - let hmin: Scalar.min ty <= x := by sorry - let hmax: x <= Scalar.max ty := by sorry - return Scalar.ofIntCore x hmin hmax - else fail integerOverflow - else fail integerOverflow - -def Scalar.neg {ty : ScalarTy} (x : Scalar ty) : Result (Scalar ty) := Scalar.tryMk ty (- x.val) - -def Scalar.div {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - if y.val != 0 then Scalar.tryMk ty (x.val / y.val) else fail divisionByZero - --- Checking that the % operation in Lean computes the same as the remainder operation in Rust -#assert 1 % 2 = (1:Int) -#assert (-1) % 2 = -1 -#assert 1 % (-2) = 1 -#assert (-1) % (-2) = -1 - -def Scalar.rem {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - if y.val != 0 then Scalar.tryMk ty (x.val % y.val) else fail divisionByZero - -def Scalar.add {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - Scalar.tryMk ty (x.val + y.val) - -def Scalar.sub {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - Scalar.tryMk ty (x.val - y.val) - -def Scalar.mul {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - Scalar.tryMk ty (x.val * y.val) - --- TODO: instances of +, -, * etc. for scalars - --- Cast an integer from a [src_ty] to a [tgt_ty] --- TODO: check the semantics of casts in Rust -def Scalar.cast {src_ty : ScalarTy} (tgt_ty : ScalarTy) (x : Scalar src_ty) : Result (Scalar tgt_ty) := - Scalar.tryMk tgt_ty x.val - --- The scalar types --- We declare the definitions as reducible so that Lean can unfold them (useful --- for type class resolution for instance). -@[reducible] def Isize := Scalar .Isize -@[reducible] def I8 := Scalar .I8 -@[reducible] def I16 := Scalar .I16 -@[reducible] def I32 := Scalar .I32 -@[reducible] def I64 := Scalar .I64 -@[reducible] def I128 := Scalar .I128 -@[reducible] def Usize := Scalar .Usize -@[reducible] def U8 := Scalar .U8 -@[reducible] def U16 := Scalar .U16 -@[reducible] def U32 := Scalar .U32 -@[reducible] def U64 := Scalar .U64 -@[reducible] def U128 := Scalar .U128 - --- TODO: below: not sure this is the best way. --- Should we rather overload operations like +, -, etc.? --- Also, it is possible to automate the generation of those definitions --- with macros (but would it be a good idea? It would be less easy to --- read the file, which is not supposed to change a lot) - --- Negation - -/-- -Remark: there is no heterogeneous negation in the Lean prelude: we thus introduce -one here. - -The notation typeclass for heterogeneous addition. -This enables the notation `- a : β` where `a : α`. --/ -class HNeg (α : Type u) (β : outParam (Type v)) where - /-- `- a` computes the negation of `a`. - The meaning of this notation is type-dependent. -/ - hNeg : α → β - -prefix:75 "-" => HNeg.hNeg - -instance : HNeg Isize (Result Isize) where hNeg x := Scalar.neg x -instance : HNeg I8 (Result I8) where hNeg x := Scalar.neg x -instance : HNeg I16 (Result I16) where hNeg x := Scalar.neg x -instance : HNeg I32 (Result I32) where hNeg x := Scalar.neg x -instance : HNeg I64 (Result I64) where hNeg x := Scalar.neg x -instance : HNeg I128 (Result I128) where hNeg x := Scalar.neg x - --- Addition -instance {ty} : HAdd (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hAdd x y := Scalar.add x y - --- Substraction -instance {ty} : HSub (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hSub x y := Scalar.sub x y - --- Multiplication -instance {ty} : HMul (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hMul x y := Scalar.mul x y - --- Division -instance {ty} : HDiv (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hDiv x y := Scalar.div x y - --- Remainder -instance {ty} : HMod (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hMod x y := Scalar.rem x y - --- ofIntCore --- TODO: typeclass? -def Isize.ofIntCore := @Scalar.ofIntCore .Isize -def I8.ofIntCore := @Scalar.ofIntCore .I8 -def I16.ofIntCore := @Scalar.ofIntCore .I16 -def I32.ofIntCore := @Scalar.ofIntCore .I32 -def I64.ofIntCore := @Scalar.ofIntCore .I64 -def I128.ofIntCore := @Scalar.ofIntCore .I128 -def Usize.ofIntCore := @Scalar.ofIntCore .Usize -def U8.ofIntCore := @Scalar.ofIntCore .U8 -def U16.ofIntCore := @Scalar.ofIntCore .U16 -def U32.ofIntCore := @Scalar.ofIntCore .U32 -def U64.ofIntCore := @Scalar.ofIntCore .U64 -def U128.ofIntCore := @Scalar.ofIntCore .U128 - --- ofInt --- TODO: typeclass? -def Isize.ofInt := @Scalar.ofInt .Isize -def I8.ofInt := @Scalar.ofInt .I8 -def I16.ofInt := @Scalar.ofInt .I16 -def I32.ofInt := @Scalar.ofInt .I32 -def I64.ofInt := @Scalar.ofInt .I64 -def I128.ofInt := @Scalar.ofInt .I128 -def Usize.ofInt := @Scalar.ofInt .Usize -def U8.ofInt := @Scalar.ofInt .U8 -def U16.ofInt := @Scalar.ofInt .U16 -def U32.ofInt := @Scalar.ofInt .U32 -def U64.ofInt := @Scalar.ofInt .U64 -def U128.ofInt := @Scalar.ofInt .U128 - --- Comparisons -instance {ty} : LT (Scalar ty) where - lt a b := LT.lt a.val b.val - -instance {ty} : LE (Scalar ty) where le a b := LE.le a.val b.val - -instance Scalar.decLt {ty} (a b : Scalar ty) : Decidable (LT.lt a b) := Int.decLt .. -instance Scalar.decLe {ty} (a b : Scalar ty) : Decidable (LE.le a b) := Int.decLe .. - -theorem Scalar.eq_of_val_eq {ty} : ∀ {i j : Scalar ty}, Eq i.val j.val → Eq i j - | ⟨_, _, _⟩, ⟨_, _, _⟩, rfl => rfl - -theorem Scalar.val_eq_of_eq {ty} {i j : Scalar ty} (h : Eq i j) : Eq i.val j.val := - h ▸ rfl - -theorem Scalar.ne_of_val_ne {ty} {i j : Scalar ty} (h : Not (Eq i.val j.val)) : Not (Eq i j) := - fun h' => absurd (val_eq_of_eq h') h - -instance (ty : ScalarTy) : DecidableEq (Scalar ty) := - fun i j => - match decEq i.val j.val with - | isTrue h => isTrue (Scalar.eq_of_val_eq h) - | isFalse h => isFalse (Scalar.ne_of_val_ne h) - -def Scalar.toInt {ty} (n : Scalar ty) : Int := n.val - --- Tactic to prove that integers are in bounds -syntax "intlit" : tactic - -macro_rules - | `(tactic| intlit) => `(tactic| apply Scalar.bound_suffices ; decide) - --- -- We now define a type class that subsumes the various machine integer types, so --- -- as to write a concise definition for scalar_cast, rather than exhaustively --- -- enumerating all of the possible pairs. We remark that Rust has sane semantics --- -- and fails if a cast operation would involve a truncation or modulo. - --- class MachineInteger (t: Type) where --- size: Nat --- val: t -> Fin size --- ofNatCore: (n:Nat) -> LT.lt n size -> t - --- set_option hygiene false in --- run_cmd --- for typeName in [`UInt8, `UInt16, `UInt32, `UInt64, `USize].map Lean.mkIdent do --- Lean.Elab.Command.elabCommand (← `( --- namespace $typeName --- instance: MachineInteger $typeName where --- size := size --- val := val --- ofNatCore := ofNatCore --- end $typeName --- )) - --- -- Aeneas only instantiates the destination type (`src` is implicit). We rely on --- -- Lean to infer `src`. - --- def scalar_cast { src: Type } (dst: Type) [ MachineInteger src ] [ MachineInteger dst ] (x: src): Result dst := --- if h: MachineInteger.val x < MachineInteger.size dst then --- .ret (MachineInteger.ofNatCore (MachineInteger.val x).val h) --- else --- .fail integerOverflow - -------------- --- VECTORS -- -------------- - -def Vec (α : Type u) := { l : List α // List.length l <= Usize.max } - -def vec_new (α : Type u): Vec α := ⟨ [], by sorry ⟩ - -def vec_len (α : Type u) (v : Vec α) : Usize := - let ⟨ v, l ⟩ := v - Usize.ofIntCore (List.length v) (by sorry) l - -def vec_push_fwd (α : Type u) (_ : Vec α) (_ : α) : Unit := () - -def vec_push_back (α : Type u) (v : Vec α) (x : α) : Result (Vec α) - := - if h : List.length v.val <= U32.max || List.length v.val <= Usize.max then - return ⟨ List.concat v.val x, by sorry ⟩ - else - fail maximumSizeExceeded - -def vec_insert_fwd (α : Type u) (v: Vec α) (i: Usize) (_: α): Result Unit := - if i.val < List.length v.val then - .ret () - else - .fail arrayOutOfBounds - -def vec_insert_back (α : Type u) (v: Vec α) (i: Usize) (x: α): Result (Vec α) := - if i.val < List.length v.val then - -- TODO: maybe we should redefine a list library which uses integers - -- (instead of natural numbers) - let i : Nat := - match i.val with - | .ofNat n => n - | .negSucc n => by sorry -- TODO: we can't get here - let isLt: i < USize.size := by sorry - let i : Fin USize.size := { val := i, isLt := isLt } - .ret ⟨ List.set v.val i.val x, by - have h: List.length v.val <= Usize.max := v.property - rewrite [ List.length_set v.val i.val x ] - assumption - ⟩ - else - .fail arrayOutOfBounds - -def vec_index_fwd (α : Type u) (v: Vec α) (i: Usize): Result α := - if i.val < List.length v.val then - let i : Nat := - match i.val with - | .ofNat n => n - | .negSucc n => by sorry -- TODO: we can't get here - let isLt: i < USize.size := by sorry - let i : Fin USize.size := { val := i, isLt := isLt } - let h: i < List.length v.val := by sorry - .ret (List.get v.val ⟨i.val, h⟩) - else - .fail arrayOutOfBounds - -def vec_index_back (α : Type u) (v: Vec α) (i: Usize) (_: α): Result Unit := - if i.val < List.length v.val then - .ret () - else - .fail arrayOutOfBounds - -def vec_index_mut_fwd (α : Type u) (v: Vec α) (i: Usize): Result α := - if i.val < List.length v.val then - let i : Nat := - match i.val with - | .ofNat n => n - | .negSucc n => by sorry -- TODO: we can't get here - let isLt: i < USize.size := by sorry - let i : Fin USize.size := { val := i, isLt := isLt } - let h: i < List.length v.val := by sorry - .ret (List.get v.val ⟨i.val, h⟩) - else - .fail arrayOutOfBounds - -def vec_index_mut_back (α : Type u) (v: Vec α) (i: Usize) (x: α): Result (Vec α) := - if i.val < List.length v.val then - let i : Nat := - match i.val with - | .ofNat n => n - | .negSucc n => by sorry -- TODO: we can't get here - let isLt: i < USize.size := by sorry - let i : Fin USize.size := { val := i, isLt := isLt } - .ret ⟨ List.set v.val i.val x, by - have h: List.length v.val <= Usize.max := v.property - rewrite [ List.length_set v.val i.val x ] - assumption - ⟩ - else - .fail arrayOutOfBounds - ----------- --- MISC -- ----------- - -def mem_replace_fwd (a : Type) (x : a) (_ : a) : a := - x - -def mem_replace_back (a : Type) (_ : a) (y : a) : a := - y - -/-- Aeneas-translated function -- useful to reduce non-recursive definitions. - Use with `simp [ aeneas ]` -/ -register_simp_attr aeneas diff --git a/tests/lean/hashmap_on_disk/HashmapMain.lean b/tests/lean/hashmap_on_disk/HashmapMain.lean deleted file mode 100644 index 1a4e7f82..00000000 --- a/tests/lean/hashmap_on_disk/HashmapMain.lean +++ /dev/null @@ -1 +0,0 @@ -import HashmapMain.Funs diff --git a/tests/lean/hashmap_on_disk/HashmapMain/Clauses/Clauses.lean b/tests/lean/hashmap_on_disk/HashmapMain/Clauses/Clauses.lean deleted file mode 100644 index a4dc996a..00000000 --- a/tests/lean/hashmap_on_disk/HashmapMain/Clauses/Clauses.lean +++ /dev/null @@ -1,110 +0,0 @@ -import Base.Primitives -import HashmapMain.Types - -/- [hashmap_main::hashmap::HashMap::{0}::allocate_slots]: termination measure -/ -@[simp] -def hashmap_hash_map_allocate_slots_loop_terminates (T : Type) - (slots : Vec (hashmap_list_t T)) (n : Usize) := - (slots, n) - -/- [hashmap_main::hashmap::HashMap::{0}::allocate_slots]: decreases_by tactic -/ -syntax "hashmap_hash_map_allocate_slots_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hashmap_hash_map_allocate_slots_loop_decreases $slots $n) => - `(tactic| sorry) - -/- [hashmap_main::hashmap::HashMap::{0}::clear]: termination measure -/ -@[simp] -def hashmap_hash_map_clear_loop_terminates (T : Type) - (slots : Vec (hashmap_list_t T)) (i : Usize) := - (slots, i) - -/- [hashmap_main::hashmap::HashMap::{0}::clear]: decreases_by tactic -/ -syntax "hashmap_hash_map_clear_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hashmap_hash_map_clear_loop_decreases $slots $i) =>`(tactic| sorry) - -/- [hashmap_main::hashmap::HashMap::{0}::insert_in_list]: termination measure -/ -@[simp] -def hashmap_hash_map_insert_in_list_loop_terminates (T : Type) (key : Usize) - (value : T) (ls : hashmap_list_t T) := - (key, value, ls) - -/- [hashmap_main::hashmap::HashMap::{0}::insert_in_list]: decreases_by tactic -/ -syntax "hashmap_hash_map_insert_in_list_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hashmap_hash_map_insert_in_list_loop_decreases $key $value $ls) => - `(tactic| sorry) - -/- [hashmap_main::hashmap::HashMap::{0}::move_elements_from_list]: termination measure -/ -@[simp] -def hashmap_hash_map_move_elements_from_list_loop_terminates (T : Type) - (ntable : hashmap_hash_map_t T) (ls : hashmap_list_t T) := - (ntable, ls) - -/- [hashmap_main::hashmap::HashMap::{0}::move_elements_from_list]: decreases_by tactic -/ -syntax "hashmap_hash_map_move_elements_from_list_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hashmap_hash_map_move_elements_from_list_loop_decreases $ntable -$ls) =>`(tactic| sorry) - -/- [hashmap_main::hashmap::HashMap::{0}::move_elements]: termination measure -/ -@[simp] -def hashmap_hash_map_move_elements_loop_terminates (T : Type) - (ntable : hashmap_hash_map_t T) (slots : Vec (hashmap_list_t T)) (i : Usize) - := - (ntable, slots, i) - -/- [hashmap_main::hashmap::HashMap::{0}::move_elements]: decreases_by tactic -/ -syntax "hashmap_hash_map_move_elements_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hashmap_hash_map_move_elements_loop_decreases $ntable $slots $i) => - `(tactic| sorry) - -/- [hashmap_main::hashmap::HashMap::{0}::contains_key_in_list]: termination measure -/ -@[simp] -def hashmap_hash_map_contains_key_in_list_loop_terminates (T : Type) - (key : Usize) (ls : hashmap_list_t T) := - (key, ls) - -/- [hashmap_main::hashmap::HashMap::{0}::contains_key_in_list]: decreases_by tactic -/ -syntax "hashmap_hash_map_contains_key_in_list_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hashmap_hash_map_contains_key_in_list_loop_decreases $key $ls) => - `(tactic| sorry) - -/- [hashmap_main::hashmap::HashMap::{0}::get_in_list]: termination measure -/ -@[simp] -def hashmap_hash_map_get_in_list_loop_terminates (T : Type) (key : Usize) - (ls : hashmap_list_t T) := - (key, ls) - -/- [hashmap_main::hashmap::HashMap::{0}::get_in_list]: decreases_by tactic -/ -syntax "hashmap_hash_map_get_in_list_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hashmap_hash_map_get_in_list_loop_decreases $key $ls) =>`(tactic| sorry) - -/- [hashmap_main::hashmap::HashMap::{0}::get_mut_in_list]: termination measure -/ -@[simp] -def hashmap_hash_map_get_mut_in_list_loop_terminates (T : Type) - (ls : hashmap_list_t T) (key : Usize) := - (ls, key) - -/- [hashmap_main::hashmap::HashMap::{0}::get_mut_in_list]: decreases_by tactic -/ -syntax "hashmap_hash_map_get_mut_in_list_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hashmap_hash_map_get_mut_in_list_loop_decreases $ls $key) => - `(tactic| sorry) - -/- [hashmap_main::hashmap::HashMap::{0}::remove_from_list]: termination measure -/ -@[simp] -def hashmap_hash_map_remove_from_list_loop_terminates (T : Type) (key : Usize) - (ls : hashmap_list_t T) := - (key, ls) - -/- [hashmap_main::hashmap::HashMap::{0}::remove_from_list]: decreases_by tactic -/ -syntax "hashmap_hash_map_remove_from_list_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hashmap_hash_map_remove_from_list_loop_decreases $key $ls) => - `(tactic| sorry) - diff --git a/tests/lean/hashmap_on_disk/HashmapMain/Clauses/Template.lean b/tests/lean/hashmap_on_disk/HashmapMain/Clauses/Template.lean deleted file mode 100644 index 33802597..00000000 --- a/tests/lean/hashmap_on_disk/HashmapMain/Clauses/Template.lean +++ /dev/null @@ -1,112 +0,0 @@ --- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS --- [hashmap_main]: templates for the decreases clauses -import Base.Primitives -import HashmapMain.Types - -/- [hashmap_main::hashmap::HashMap::{0}::allocate_slots]: termination measure -/ -@[simp] -def hashmap_hash_map_allocate_slots_loop_terminates (T : Type) - (slots : Vec (hashmap_list_t T)) (n : Usize) := - (slots, n) - -/- [hashmap_main::hashmap::HashMap::{0}::allocate_slots]: decreases_by tactic -/ -syntax "hashmap_hash_map_allocate_slots_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hashmap_hash_map_allocate_slots_loop_decreases $slots $n) => - `(tactic| sorry) - -/- [hashmap_main::hashmap::HashMap::{0}::clear]: termination measure -/ -@[simp] -def hashmap_hash_map_clear_loop_terminates (T : Type) - (slots : Vec (hashmap_list_t T)) (i : Usize) := - (slots, i) - -/- [hashmap_main::hashmap::HashMap::{0}::clear]: decreases_by tactic -/ -syntax "hashmap_hash_map_clear_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hashmap_hash_map_clear_loop_decreases $slots $i) =>`(tactic| sorry) - -/- [hashmap_main::hashmap::HashMap::{0}::insert_in_list]: termination measure -/ -@[simp] -def hashmap_hash_map_insert_in_list_loop_terminates (T : Type) (key : Usize) - (value : T) (ls : hashmap_list_t T) := - (key, value, ls) - -/- [hashmap_main::hashmap::HashMap::{0}::insert_in_list]: decreases_by tactic -/ -syntax "hashmap_hash_map_insert_in_list_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hashmap_hash_map_insert_in_list_loop_decreases $key $value $ls) => - `(tactic| sorry) - -/- [hashmap_main::hashmap::HashMap::{0}::move_elements_from_list]: termination measure -/ -@[simp] -def hashmap_hash_map_move_elements_from_list_loop_terminates (T : Type) - (ntable : hashmap_hash_map_t T) (ls : hashmap_list_t T) := - (ntable, ls) - -/- [hashmap_main::hashmap::HashMap::{0}::move_elements_from_list]: decreases_by tactic -/ -syntax "hashmap_hash_map_move_elements_from_list_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hashmap_hash_map_move_elements_from_list_loop_decreases $ntable -$ls) =>`(tactic| sorry) - -/- [hashmap_main::hashmap::HashMap::{0}::move_elements]: termination measure -/ -@[simp] -def hashmap_hash_map_move_elements_loop_terminates (T : Type) - (ntable : hashmap_hash_map_t T) (slots : Vec (hashmap_list_t T)) (i : Usize) - := - (ntable, slots, i) - -/- [hashmap_main::hashmap::HashMap::{0}::move_elements]: decreases_by tactic -/ -syntax "hashmap_hash_map_move_elements_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hashmap_hash_map_move_elements_loop_decreases $ntable $slots $i) => - `(tactic| sorry) - -/- [hashmap_main::hashmap::HashMap::{0}::contains_key_in_list]: termination measure -/ -@[simp] -def hashmap_hash_map_contains_key_in_list_loop_terminates (T : Type) - (key : Usize) (ls : hashmap_list_t T) := - (key, ls) - -/- [hashmap_main::hashmap::HashMap::{0}::contains_key_in_list]: decreases_by tactic -/ -syntax "hashmap_hash_map_contains_key_in_list_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hashmap_hash_map_contains_key_in_list_loop_decreases $key $ls) => - `(tactic| sorry) - -/- [hashmap_main::hashmap::HashMap::{0}::get_in_list]: termination measure -/ -@[simp] -def hashmap_hash_map_get_in_list_loop_terminates (T : Type) (key : Usize) - (ls : hashmap_list_t T) := - (key, ls) - -/- [hashmap_main::hashmap::HashMap::{0}::get_in_list]: decreases_by tactic -/ -syntax "hashmap_hash_map_get_in_list_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hashmap_hash_map_get_in_list_loop_decreases $key $ls) =>`(tactic| sorry) - -/- [hashmap_main::hashmap::HashMap::{0}::get_mut_in_list]: termination measure -/ -@[simp] -def hashmap_hash_map_get_mut_in_list_loop_terminates (T : Type) - (ls : hashmap_list_t T) (key : Usize) := - (ls, key) - -/- [hashmap_main::hashmap::HashMap::{0}::get_mut_in_list]: decreases_by tactic -/ -syntax "hashmap_hash_map_get_mut_in_list_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hashmap_hash_map_get_mut_in_list_loop_decreases $ls $key) => - `(tactic| sorry) - -/- [hashmap_main::hashmap::HashMap::{0}::remove_from_list]: termination measure -/ -@[simp] -def hashmap_hash_map_remove_from_list_loop_terminates (T : Type) (key : Usize) - (ls : hashmap_list_t T) := - (key, ls) - -/- [hashmap_main::hashmap::HashMap::{0}::remove_from_list]: decreases_by tactic -/ -syntax "hashmap_hash_map_remove_from_list_loop_decreases" term+ : tactic -macro_rules -| `(tactic| hashmap_hash_map_remove_from_list_loop_decreases $key $ls) => - `(tactic| sorry) - diff --git a/tests/lean/hashmap_on_disk/HashmapMain/ExternalFuns.lean b/tests/lean/hashmap_on_disk/HashmapMain/ExternalFuns.lean deleted file mode 100644 index a5103acc..00000000 --- a/tests/lean/hashmap_on_disk/HashmapMain/ExternalFuns.lean +++ /dev/null @@ -1,5 +0,0 @@ -import Base.Primitives -import HashmapMain.Types -import HashmapMain.Opaque - -def opaque_defs : OpaqueDefs := by sorry diff --git a/tests/lean/hashmap_on_disk/HashmapMain/Funs.lean b/tests/lean/hashmap_on_disk/HashmapMain/Funs.lean deleted file mode 100644 index 342c3833..00000000 --- a/tests/lean/hashmap_on_disk/HashmapMain/Funs.lean +++ /dev/null @@ -1,590 +0,0 @@ --- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS --- [hashmap_main]: function definitions -import Base.Primitives -import HashmapMain.Types -import HashmapMain.ExternalFuns -import HashmapMain.Clauses.Clauses - -/- [hashmap_main::hashmap::hash_key] -/ -def hashmap_hash_key_fwd (k : Usize) : Result Usize := - Result.ret k - -/- [hashmap_main::hashmap::HashMap::{0}::allocate_slots] -/ -def hashmap_hash_map_allocate_slots_loop_fwd - (T : Type) (slots : Vec (hashmap_list_t T)) (n : Usize) : - (Result (Vec (hashmap_list_t T))) - := - if h: n > (Usize.ofInt 0 (by intlit)) - then - do - let slots0 ← vec_push_back (hashmap_list_t T) slots hashmap_list_t.Nil - let n0 ← n - (Usize.ofInt 1 (by intlit)) - hashmap_hash_map_allocate_slots_loop_fwd T slots0 n0 - else Result.ret slots -termination_by hashmap_hash_map_allocate_slots_loop_fwd slots n => - hashmap_hash_map_allocate_slots_loop_terminates T slots n -decreasing_by hashmap_hash_map_allocate_slots_loop_decreases slots n - -/- [hashmap_main::hashmap::HashMap::{0}::allocate_slots] -/ -def hashmap_hash_map_allocate_slots_fwd - (T : Type) (slots : Vec (hashmap_list_t T)) (n : Usize) : - Result (Vec (hashmap_list_t T)) - := - hashmap_hash_map_allocate_slots_loop_fwd T slots n - -/- [hashmap_main::hashmap::HashMap::{0}::new_with_capacity] -/ -def hashmap_hash_map_new_with_capacity_fwd - (T : Type) (capacity : Usize) (max_load_dividend : Usize) - (max_load_divisor : Usize) : - Result (hashmap_hash_map_t T) - := - do - let v := vec_new (hashmap_list_t T) - let slots ← hashmap_hash_map_allocate_slots_fwd T v capacity - let i ← capacity * max_load_dividend - let i0 ← i / max_load_divisor - Result.ret - { - hashmap_hash_map_num_entries := (Usize.ofInt 0 (by intlit)), - hashmap_hash_map_max_load_factor := - (max_load_dividend, max_load_divisor), - hashmap_hash_map_max_load := i0, - hashmap_hash_map_slots := slots - } - -/- [hashmap_main::hashmap::HashMap::{0}::new] -/ -def hashmap_hash_map_new_fwd (T : Type) : Result (hashmap_hash_map_t T) := - hashmap_hash_map_new_with_capacity_fwd T (Usize.ofInt 32 (by intlit)) - (Usize.ofInt 4 (by intlit)) (Usize.ofInt 5 (by intlit)) - -/- [hashmap_main::hashmap::HashMap::{0}::clear] -/ -def hashmap_hash_map_clear_loop_fwd_back - (T : Type) (slots : Vec (hashmap_list_t T)) (i : Usize) : - (Result (Vec (hashmap_list_t T))) - := - let i0 := vec_len (hashmap_list_t T) slots - if h: i < i0 - then - do - let i1 ← i + (Usize.ofInt 1 (by intlit)) - let slots0 ← - vec_index_mut_back (hashmap_list_t T) slots i hashmap_list_t.Nil - hashmap_hash_map_clear_loop_fwd_back T slots0 i1 - else Result.ret slots -termination_by hashmap_hash_map_clear_loop_fwd_back slots i => - hashmap_hash_map_clear_loop_terminates T slots i -decreasing_by hashmap_hash_map_clear_loop_decreases slots i - -/- [hashmap_main::hashmap::HashMap::{0}::clear] -/ -def hashmap_hash_map_clear_fwd_back - (T : Type) (self : hashmap_hash_map_t T) : Result (hashmap_hash_map_t T) := - do - let v ← - hashmap_hash_map_clear_loop_fwd_back T self.hashmap_hash_map_slots - (Usize.ofInt 0 (by intlit)) - Result.ret - { - self - with - hashmap_hash_map_num_entries := (Usize.ofInt 0 (by intlit)), - hashmap_hash_map_slots := v - } - -/- [hashmap_main::hashmap::HashMap::{0}::len] -/ -def hashmap_hash_map_len_fwd - (T : Type) (self : hashmap_hash_map_t T) : Result Usize := - Result.ret self.hashmap_hash_map_num_entries - -/- [hashmap_main::hashmap::HashMap::{0}::insert_in_list] -/ -def hashmap_hash_map_insert_in_list_loop_fwd - (T : Type) (key : Usize) (value : T) (ls : hashmap_list_t T) : - (Result Bool) - := - match h: ls with - | hashmap_list_t.Cons ckey cvalue tl => - if h: ckey = key - then Result.ret false - else hashmap_hash_map_insert_in_list_loop_fwd T key value tl - | hashmap_list_t.Nil => Result.ret true -termination_by hashmap_hash_map_insert_in_list_loop_fwd key value ls => - hashmap_hash_map_insert_in_list_loop_terminates T key value ls -decreasing_by hashmap_hash_map_insert_in_list_loop_decreases key value ls - -/- [hashmap_main::hashmap::HashMap::{0}::insert_in_list] -/ -def hashmap_hash_map_insert_in_list_fwd - (T : Type) (key : Usize) (value : T) (ls : hashmap_list_t T) : Result Bool := - hashmap_hash_map_insert_in_list_loop_fwd T key value ls - -/- [hashmap_main::hashmap::HashMap::{0}::insert_in_list] -/ -def hashmap_hash_map_insert_in_list_loop_back - (T : Type) (key : Usize) (value : T) (ls : hashmap_list_t T) : - (Result (hashmap_list_t T)) - := - match h: ls with - | hashmap_list_t.Cons ckey cvalue tl => - if h: ckey = key - then Result.ret (hashmap_list_t.Cons ckey value tl) - else - do - let tl0 ← hashmap_hash_map_insert_in_list_loop_back T key value tl - Result.ret (hashmap_list_t.Cons ckey cvalue tl0) - | hashmap_list_t.Nil => - let l := hashmap_list_t.Nil - Result.ret (hashmap_list_t.Cons key value l) -termination_by hashmap_hash_map_insert_in_list_loop_back key value ls => - hashmap_hash_map_insert_in_list_loop_terminates T key value ls -decreasing_by hashmap_hash_map_insert_in_list_loop_decreases key value ls - -/- [hashmap_main::hashmap::HashMap::{0}::insert_in_list] -/ -def hashmap_hash_map_insert_in_list_back - (T : Type) (key : Usize) (value : T) (ls : hashmap_list_t T) : - Result (hashmap_list_t T) - := - hashmap_hash_map_insert_in_list_loop_back T key value ls - -/- [hashmap_main::hashmap::HashMap::{0}::insert_no_resize] -/ -def hashmap_hash_map_insert_no_resize_fwd_back - (T : Type) (self : hashmap_hash_map_t T) (key : Usize) (value : T) : - Result (hashmap_hash_map_t T) - := - do - let hash ← hashmap_hash_key_fwd key - let i := vec_len (hashmap_list_t T) self.hashmap_hash_map_slots - let hash_mod ← hash % i - let l ← - vec_index_mut_fwd (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod - let inserted ← hashmap_hash_map_insert_in_list_fwd T key value l - if h: inserted - then - do - let i0 ← self.hashmap_hash_map_num_entries + - (Usize.ofInt 1 (by intlit)) - let l0 ← hashmap_hash_map_insert_in_list_back T key value l - let v ← - vec_index_mut_back (hashmap_list_t T) self.hashmap_hash_map_slots - hash_mod l0 - Result.ret - { - self - with - hashmap_hash_map_num_entries := i0, hashmap_hash_map_slots := v - } - else - do - let l0 ← hashmap_hash_map_insert_in_list_back T key value l - let v ← - vec_index_mut_back (hashmap_list_t T) self.hashmap_hash_map_slots - hash_mod l0 - Result.ret { self with hashmap_hash_map_slots := v } - -/- [core::num::u32::{9}::MAX] -/ -def core_num_u32_max_body : Result U32 := - Result.ret (U32.ofInt 4294967295 (by intlit)) -def core_num_u32_max_c : U32 := eval_global core_num_u32_max_body (by simp) - -/- [hashmap_main::hashmap::HashMap::{0}::move_elements_from_list] -/ -def hashmap_hash_map_move_elements_from_list_loop_fwd_back - (T : Type) (ntable : hashmap_hash_map_t T) (ls : hashmap_list_t T) : - (Result (hashmap_hash_map_t T)) - := - match h: ls with - | hashmap_list_t.Cons k v tl => - do - let ntable0 ← hashmap_hash_map_insert_no_resize_fwd_back T ntable k v - hashmap_hash_map_move_elements_from_list_loop_fwd_back T ntable0 tl - | hashmap_list_t.Nil => Result.ret ntable -termination_by hashmap_hash_map_move_elements_from_list_loop_fwd_back ntable ls - => - hashmap_hash_map_move_elements_from_list_loop_terminates T ntable ls -decreasing_by hashmap_hash_map_move_elements_from_list_loop_decreases ntable ls - -/- [hashmap_main::hashmap::HashMap::{0}::move_elements_from_list] -/ -def hashmap_hash_map_move_elements_from_list_fwd_back - (T : Type) (ntable : hashmap_hash_map_t T) (ls : hashmap_list_t T) : - Result (hashmap_hash_map_t T) - := - hashmap_hash_map_move_elements_from_list_loop_fwd_back T ntable ls - -/- [hashmap_main::hashmap::HashMap::{0}::move_elements] -/ -def hashmap_hash_map_move_elements_loop_fwd_back - (T : Type) (ntable : hashmap_hash_map_t T) (slots : Vec (hashmap_list_t T)) - (i : Usize) : - (Result ((hashmap_hash_map_t T) × (Vec (hashmap_list_t T)))) - := - let i0 := vec_len (hashmap_list_t T) slots - if h: i < i0 - then - do - let l ← vec_index_mut_fwd (hashmap_list_t T) slots i - let ls := mem_replace_fwd (hashmap_list_t T) l hashmap_list_t.Nil - let ntable0 ← - hashmap_hash_map_move_elements_from_list_fwd_back T ntable ls - let i1 ← i + (Usize.ofInt 1 (by intlit)) - let l0 := mem_replace_back (hashmap_list_t T) l hashmap_list_t.Nil - let slots0 ← vec_index_mut_back (hashmap_list_t T) slots i l0 - hashmap_hash_map_move_elements_loop_fwd_back T ntable0 slots0 i1 - else Result.ret (ntable, slots) -termination_by hashmap_hash_map_move_elements_loop_fwd_back ntable slots i => - hashmap_hash_map_move_elements_loop_terminates T ntable slots i -decreasing_by hashmap_hash_map_move_elements_loop_decreases ntable slots i - -/- [hashmap_main::hashmap::HashMap::{0}::move_elements] -/ -def hashmap_hash_map_move_elements_fwd_back - (T : Type) (ntable : hashmap_hash_map_t T) (slots : Vec (hashmap_list_t T)) - (i : Usize) : - Result ((hashmap_hash_map_t T) × (Vec (hashmap_list_t T))) - := - hashmap_hash_map_move_elements_loop_fwd_back T ntable slots i - -/- [hashmap_main::hashmap::HashMap::{0}::try_resize] -/ -def hashmap_hash_map_try_resize_fwd_back - (T : Type) (self : hashmap_hash_map_t T) : Result (hashmap_hash_map_t T) := - do - let max_usize ← Scalar.cast .Usize core_num_u32_max_c - let capacity := vec_len (hashmap_list_t T) self.hashmap_hash_map_slots - let n1 ← max_usize / (Usize.ofInt 2 (by intlit)) - let (i, i0) := self.hashmap_hash_map_max_load_factor - let i1 ← n1 / i - if h: capacity <= i1 - then - do - let i2 ← capacity * (Usize.ofInt 2 (by intlit)) - let ntable ← hashmap_hash_map_new_with_capacity_fwd T i2 i i0 - let (ntable0, _) ← - hashmap_hash_map_move_elements_fwd_back T ntable - self.hashmap_hash_map_slots (Usize.ofInt 0 (by intlit)) - Result.ret - { - ntable0 - with - hashmap_hash_map_num_entries := self.hashmap_hash_map_num_entries, - hashmap_hash_map_max_load_factor := (i, i0) - } - else Result.ret { self with hashmap_hash_map_max_load_factor := (i, i0) } - -/- [hashmap_main::hashmap::HashMap::{0}::insert] -/ -def hashmap_hash_map_insert_fwd_back - (T : Type) (self : hashmap_hash_map_t T) (key : Usize) (value : T) : - Result (hashmap_hash_map_t T) - := - do - let self0 ← hashmap_hash_map_insert_no_resize_fwd_back T self key value - let i ← hashmap_hash_map_len_fwd T self0 - if h: i > self0.hashmap_hash_map_max_load - then hashmap_hash_map_try_resize_fwd_back T self0 - else Result.ret self0 - -/- [hashmap_main::hashmap::HashMap::{0}::contains_key_in_list] -/ -def hashmap_hash_map_contains_key_in_list_loop_fwd - (T : Type) (key : Usize) (ls : hashmap_list_t T) : (Result Bool) := - match h: ls with - | hashmap_list_t.Cons ckey t tl => - if h: ckey = key - then Result.ret true - else hashmap_hash_map_contains_key_in_list_loop_fwd T key tl - | hashmap_list_t.Nil => Result.ret false -termination_by hashmap_hash_map_contains_key_in_list_loop_fwd key ls => - hashmap_hash_map_contains_key_in_list_loop_terminates T key ls -decreasing_by hashmap_hash_map_contains_key_in_list_loop_decreases key ls - -/- [hashmap_main::hashmap::HashMap::{0}::contains_key_in_list] -/ -def hashmap_hash_map_contains_key_in_list_fwd - (T : Type) (key : Usize) (ls : hashmap_list_t T) : Result Bool := - hashmap_hash_map_contains_key_in_list_loop_fwd T key ls - -/- [hashmap_main::hashmap::HashMap::{0}::contains_key] -/ -def hashmap_hash_map_contains_key_fwd - (T : Type) (self : hashmap_hash_map_t T) (key : Usize) : Result Bool := - do - let hash ← hashmap_hash_key_fwd key - let i := vec_len (hashmap_list_t T) self.hashmap_hash_map_slots - let hash_mod ← hash % i - let l ← - vec_index_fwd (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod - hashmap_hash_map_contains_key_in_list_fwd T key l - -/- [hashmap_main::hashmap::HashMap::{0}::get_in_list] -/ -def hashmap_hash_map_get_in_list_loop_fwd - (T : Type) (key : Usize) (ls : hashmap_list_t T) : (Result T) := - match h: ls with - | hashmap_list_t.Cons ckey cvalue tl => - if h: ckey = key - then Result.ret cvalue - else hashmap_hash_map_get_in_list_loop_fwd T key tl - | hashmap_list_t.Nil => Result.fail Error.panic -termination_by hashmap_hash_map_get_in_list_loop_fwd key ls => - hashmap_hash_map_get_in_list_loop_terminates T key ls -decreasing_by hashmap_hash_map_get_in_list_loop_decreases key ls - -/- [hashmap_main::hashmap::HashMap::{0}::get_in_list] -/ -def hashmap_hash_map_get_in_list_fwd - (T : Type) (key : Usize) (ls : hashmap_list_t T) : Result T := - hashmap_hash_map_get_in_list_loop_fwd T key ls - -/- [hashmap_main::hashmap::HashMap::{0}::get] -/ -def hashmap_hash_map_get_fwd - (T : Type) (self : hashmap_hash_map_t T) (key : Usize) : Result T := - do - let hash ← hashmap_hash_key_fwd key - let i := vec_len (hashmap_list_t T) self.hashmap_hash_map_slots - let hash_mod ← hash % i - let l ← - vec_index_fwd (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod - hashmap_hash_map_get_in_list_fwd T key l - -/- [hashmap_main::hashmap::HashMap::{0}::get_mut_in_list] -/ -def hashmap_hash_map_get_mut_in_list_loop_fwd - (T : Type) (ls : hashmap_list_t T) (key : Usize) : (Result T) := - match h: ls with - | hashmap_list_t.Cons ckey cvalue tl => - if h: ckey = key - then Result.ret cvalue - else hashmap_hash_map_get_mut_in_list_loop_fwd T tl key - | hashmap_list_t.Nil => Result.fail Error.panic -termination_by hashmap_hash_map_get_mut_in_list_loop_fwd ls key => - hashmap_hash_map_get_mut_in_list_loop_terminates T ls key -decreasing_by hashmap_hash_map_get_mut_in_list_loop_decreases ls key - -/- [hashmap_main::hashmap::HashMap::{0}::get_mut_in_list] -/ -def hashmap_hash_map_get_mut_in_list_fwd - (T : Type) (ls : hashmap_list_t T) (key : Usize) : Result T := - hashmap_hash_map_get_mut_in_list_loop_fwd T ls key - -/- [hashmap_main::hashmap::HashMap::{0}::get_mut_in_list] -/ -def hashmap_hash_map_get_mut_in_list_loop_back - (T : Type) (ls : hashmap_list_t T) (key : Usize) (ret0 : T) : - (Result (hashmap_list_t T)) - := - match h: ls with - | hashmap_list_t.Cons ckey cvalue tl => - if h: ckey = key - then Result.ret (hashmap_list_t.Cons ckey ret0 tl) - else - do - let tl0 ← hashmap_hash_map_get_mut_in_list_loop_back T tl key ret0 - Result.ret (hashmap_list_t.Cons ckey cvalue tl0) - | hashmap_list_t.Nil => Result.fail Error.panic -termination_by hashmap_hash_map_get_mut_in_list_loop_back ls key ret0 => - hashmap_hash_map_get_mut_in_list_loop_terminates T ls key -decreasing_by hashmap_hash_map_get_mut_in_list_loop_decreases ls key - -/- [hashmap_main::hashmap::HashMap::{0}::get_mut_in_list] -/ -def hashmap_hash_map_get_mut_in_list_back - (T : Type) (ls : hashmap_list_t T) (key : Usize) (ret0 : T) : - Result (hashmap_list_t T) - := - hashmap_hash_map_get_mut_in_list_loop_back T ls key ret0 - -/- [hashmap_main::hashmap::HashMap::{0}::get_mut] -/ -def hashmap_hash_map_get_mut_fwd - (T : Type) (self : hashmap_hash_map_t T) (key : Usize) : Result T := - do - let hash ← hashmap_hash_key_fwd key - let i := vec_len (hashmap_list_t T) self.hashmap_hash_map_slots - let hash_mod ← hash % i - let l ← - vec_index_mut_fwd (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod - hashmap_hash_map_get_mut_in_list_fwd T l key - -/- [hashmap_main::hashmap::HashMap::{0}::get_mut] -/ -def hashmap_hash_map_get_mut_back - (T : Type) (self : hashmap_hash_map_t T) (key : Usize) (ret0 : T) : - Result (hashmap_hash_map_t T) - := - do - let hash ← hashmap_hash_key_fwd key - let i := vec_len (hashmap_list_t T) self.hashmap_hash_map_slots - let hash_mod ← hash % i - let l ← - vec_index_mut_fwd (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod - let l0 ← hashmap_hash_map_get_mut_in_list_back T l key ret0 - let v ← - vec_index_mut_back (hashmap_list_t T) self.hashmap_hash_map_slots - hash_mod l0 - Result.ret { self with hashmap_hash_map_slots := v } - -/- [hashmap_main::hashmap::HashMap::{0}::remove_from_list] -/ -def hashmap_hash_map_remove_from_list_loop_fwd - (T : Type) (key : Usize) (ls : hashmap_list_t T) : (Result (Option T)) := - match h: ls with - | hashmap_list_t.Cons ckey t tl => - if h: ckey = key - then - let mv_ls := - mem_replace_fwd (hashmap_list_t T) (hashmap_list_t.Cons ckey t tl) - hashmap_list_t.Nil - match h: mv_ls with - | hashmap_list_t.Cons i cvalue tl0 => Result.ret (Option.some cvalue) - | hashmap_list_t.Nil => Result.fail Error.panic - else hashmap_hash_map_remove_from_list_loop_fwd T key tl - | hashmap_list_t.Nil => Result.ret Option.none -termination_by hashmap_hash_map_remove_from_list_loop_fwd key ls => - hashmap_hash_map_remove_from_list_loop_terminates T key ls -decreasing_by hashmap_hash_map_remove_from_list_loop_decreases key ls - -/- [hashmap_main::hashmap::HashMap::{0}::remove_from_list] -/ -def hashmap_hash_map_remove_from_list_fwd - (T : Type) (key : Usize) (ls : hashmap_list_t T) : Result (Option T) := - hashmap_hash_map_remove_from_list_loop_fwd T key ls - -/- [hashmap_main::hashmap::HashMap::{0}::remove_from_list] -/ -def hashmap_hash_map_remove_from_list_loop_back - (T : Type) (key : Usize) (ls : hashmap_list_t T) : - (Result (hashmap_list_t T)) - := - match h: ls with - | hashmap_list_t.Cons ckey t tl => - if h: ckey = key - then - let mv_ls := - mem_replace_fwd (hashmap_list_t T) (hashmap_list_t.Cons ckey t tl) - hashmap_list_t.Nil - match h: mv_ls with - | hashmap_list_t.Cons i cvalue tl0 => Result.ret tl0 - | hashmap_list_t.Nil => Result.fail Error.panic - else - do - let tl0 ← hashmap_hash_map_remove_from_list_loop_back T key tl - Result.ret (hashmap_list_t.Cons ckey t tl0) - | hashmap_list_t.Nil => Result.ret hashmap_list_t.Nil -termination_by hashmap_hash_map_remove_from_list_loop_back key ls => - hashmap_hash_map_remove_from_list_loop_terminates T key ls -decreasing_by hashmap_hash_map_remove_from_list_loop_decreases key ls - -/- [hashmap_main::hashmap::HashMap::{0}::remove_from_list] -/ -def hashmap_hash_map_remove_from_list_back - (T : Type) (key : Usize) (ls : hashmap_list_t T) : - Result (hashmap_list_t T) - := - hashmap_hash_map_remove_from_list_loop_back T key ls - -/- [hashmap_main::hashmap::HashMap::{0}::remove] -/ -def hashmap_hash_map_remove_fwd - (T : Type) (self : hashmap_hash_map_t T) (key : Usize) : Result (Option T) := - do - let hash ← hashmap_hash_key_fwd key - let i := vec_len (hashmap_list_t T) self.hashmap_hash_map_slots - let hash_mod ← hash % i - let l ← - vec_index_mut_fwd (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod - let x ← hashmap_hash_map_remove_from_list_fwd T key l - match h: x with - | Option.none => Result.ret Option.none - | Option.some x0 => - do - let _ ← self.hashmap_hash_map_num_entries - - (Usize.ofInt 1 (by intlit)) - Result.ret (Option.some x0) - -/- [hashmap_main::hashmap::HashMap::{0}::remove] -/ -def hashmap_hash_map_remove_back - (T : Type) (self : hashmap_hash_map_t T) (key : Usize) : - Result (hashmap_hash_map_t T) - := - do - let hash ← hashmap_hash_key_fwd key - let i := vec_len (hashmap_list_t T) self.hashmap_hash_map_slots - let hash_mod ← hash % i - let l ← - vec_index_mut_fwd (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod - let x ← hashmap_hash_map_remove_from_list_fwd T key l - match h: x with - | Option.none => - do - let l0 ← hashmap_hash_map_remove_from_list_back T key l - let v ← - vec_index_mut_back (hashmap_list_t T) self.hashmap_hash_map_slots - hash_mod l0 - Result.ret { self with hashmap_hash_map_slots := v } - | Option.some x0 => - do - let i0 ← self.hashmap_hash_map_num_entries - - (Usize.ofInt 1 (by intlit)) - let l0 ← hashmap_hash_map_remove_from_list_back T key l - let v ← - vec_index_mut_back (hashmap_list_t T) self.hashmap_hash_map_slots - hash_mod l0 - Result.ret - { - self - with - hashmap_hash_map_num_entries := i0, hashmap_hash_map_slots := v - } - -/- [hashmap_main::hashmap::test1] -/ -def hashmap_test1_fwd : Result Unit := - do - let hm ← hashmap_hash_map_new_fwd U64 - let hm0 ← - hashmap_hash_map_insert_fwd_back U64 hm (Usize.ofInt 0 (by intlit)) - (U64.ofInt 42 (by intlit)) - let hm1 ← - hashmap_hash_map_insert_fwd_back U64 hm0 (Usize.ofInt 128 (by intlit)) - (U64.ofInt 18 (by intlit)) - let hm2 ← - hashmap_hash_map_insert_fwd_back U64 hm1 (Usize.ofInt 1024 (by intlit)) - (U64.ofInt 138 (by intlit)) - let hm3 ← - hashmap_hash_map_insert_fwd_back U64 hm2 (Usize.ofInt 1056 (by intlit)) - (U64.ofInt 256 (by intlit)) - let i ← hashmap_hash_map_get_fwd U64 hm3 (Usize.ofInt 128 (by intlit)) - if h: not (i = (U64.ofInt 18 (by intlit))) - then Result.fail Error.panic - else - do - let hm4 ← - hashmap_hash_map_get_mut_back U64 hm3 (Usize.ofInt 1024 (by intlit)) - (U64.ofInt 56 (by intlit)) - let i0 ← - hashmap_hash_map_get_fwd U64 hm4 (Usize.ofInt 1024 (by intlit)) - if h: not (i0 = (U64.ofInt 56 (by intlit))) - then Result.fail Error.panic - else - do - let x ← - hashmap_hash_map_remove_fwd U64 hm4 - (Usize.ofInt 1024 (by intlit)) - match h: x with - | Option.none => Result.fail Error.panic - | Option.some x0 => - if h: not (x0 = (U64.ofInt 56 (by intlit))) - then Result.fail Error.panic - else - do - let hm5 ← - hashmap_hash_map_remove_back U64 hm4 - (Usize.ofInt 1024 (by intlit)) - let i1 ← - hashmap_hash_map_get_fwd U64 hm5 - (Usize.ofInt 0 (by intlit)) - if h: not (i1 = (U64.ofInt 42 (by intlit))) - then Result.fail Error.panic - else - do - let i2 ← - hashmap_hash_map_get_fwd U64 hm5 - (Usize.ofInt 128 (by intlit)) - if h: not (i2 = (U64.ofInt 18 (by intlit))) - then Result.fail Error.panic - else - do - let i3 ← - hashmap_hash_map_get_fwd U64 hm5 - (Usize.ofInt 1056 (by intlit)) - if h: not (i3 = (U64.ofInt 256 (by intlit))) - then Result.fail Error.panic - else Result.ret () - -/- [hashmap_main::insert_on_disk] -/ -def insert_on_disk_fwd - (key : Usize) (value : U64) (st : State) : Result (State × Unit) := - do - let (st0, hm) ← opaque_defs.hashmap_utils_deserialize_fwd st - let hm0 ← hashmap_hash_map_insert_fwd_back U64 hm key value - let (st1, _) ← opaque_defs.hashmap_utils_serialize_fwd hm0 st0 - Result.ret (st1, ()) - -/- [hashmap_main::main] -/ -def main_fwd : Result Unit := - Result.ret () - diff --git a/tests/lean/hashmap_on_disk/HashmapMain/Opaque.lean b/tests/lean/hashmap_on_disk/HashmapMain/Opaque.lean deleted file mode 100644 index d98f431a..00000000 --- a/tests/lean/hashmap_on_disk/HashmapMain/Opaque.lean +++ /dev/null @@ -1,15 +0,0 @@ --- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS --- [hashmap_main]: opaque function definitions -import Base.Primitives -import HashmapMain.Types - -structure OpaqueDefs where - - /- [hashmap_main::hashmap_utils::deserialize] -/ - hashmap_utils_deserialize_fwd - : State -> Result (State × (hashmap_hash_map_t U64)) - - /- [hashmap_main::hashmap_utils::serialize] -/ - hashmap_utils_serialize_fwd - : hashmap_hash_map_t U64 -> State -> Result (State × Unit) - diff --git a/tests/lean/hashmap_on_disk/HashmapMain/Types.lean b/tests/lean/hashmap_on_disk/HashmapMain/Types.lean deleted file mode 100644 index 0509fbbd..00000000 --- a/tests/lean/hashmap_on_disk/HashmapMain/Types.lean +++ /dev/null @@ -1,19 +0,0 @@ --- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS --- [hashmap_main]: type definitions -import Base.Primitives - -/- [hashmap_main::hashmap::List] -/ -inductive hashmap_list_t (T : Type) := -| Cons : Usize -> T -> hashmap_list_t T -> hashmap_list_t T -| Nil : hashmap_list_t T - -/- [hashmap_main::hashmap::HashMap] -/ -structure hashmap_hash_map_t (T : Type) where - hashmap_hash_map_num_entries : Usize - hashmap_hash_map_max_load_factor : (Usize × Usize) - hashmap_hash_map_max_load : Usize - hashmap_hash_map_slots : Vec (hashmap_list_t T) - -/- The state type used in the state-error monad -/ -axiom State : Type - diff --git a/tests/lean/hashmap_on_disk/lake-manifest.json b/tests/lean/hashmap_on_disk/lake-manifest.json deleted file mode 100644 index 57b071ca..00000000 --- a/tests/lean/hashmap_on_disk/lake-manifest.json +++ /dev/null @@ -1,27 +0,0 @@ -{"version": 4, - "packagesDir": "./lake-packages", - "packages": - [{"git": - {"url": "https://github.com/leanprover-community/mathlib4.git", - "subDir?": null, - "rev": "4037792ead804d7bfa8868e2c4684d4223c15ece", - "name": "mathlib", - "inputRev?": null}}, - {"git": - {"url": "https://github.com/gebner/quote4", - "subDir?": null, - "rev": "2412c4fdf4a8b689f4467618e5e7b371ae5014aa", - "name": "Qq", - "inputRev?": "master"}}, - {"git": - {"url": "https://github.com/JLimperg/aesop", - "subDir?": null, - "rev": "7fe9ecd9339b0e1796e89d243b776849c305c690", - "name": "aesop", - "inputRev?": "master"}}, - {"git": - {"url": "https://github.com/leanprover/std4", - "subDir?": null, - "rev": "24897887905b3a1254b244369f5dd2cf6174b0ee", - "name": "std", - "inputRev?": "main"}}]} diff --git a/tests/lean/hashmap_on_disk/lakefile.lean b/tests/lean/hashmap_on_disk/lakefile.lean deleted file mode 100644 index 70daf427..00000000 --- a/tests/lean/hashmap_on_disk/lakefile.lean +++ /dev/null @@ -1,12 +0,0 @@ -import Lake -open Lake DSL - -require mathlib from git - "https://github.com/leanprover-community/mathlib4.git" - -package «hashmap_main» {} - -lean_lib «Base» {} - -@[default_target] -lean_lib «HashmapMain» {} diff --git a/tests/lean/hashmap_on_disk/lean-toolchain b/tests/lean/hashmap_on_disk/lean-toolchain deleted file mode 100644 index bbf57f10..00000000 --- a/tests/lean/hashmap_on_disk/lean-toolchain +++ /dev/null @@ -1 +0,0 @@ -leanprover/lean4:nightly-2023-01-21 diff --git a/tests/lean/lake-manifest.json b/tests/lean/lake-manifest.json new file mode 100644 index 00000000..1397c6f0 --- /dev/null +++ b/tests/lean/lake-manifest.json @@ -0,0 +1,34 @@ +{"version": 4, + "packagesDir": "lake-packages", + "packages": + [{"git": + {"url": "https://github.com/EdAyers/ProofWidgets4", + "subDir?": null, + "rev": "c43db94a8f495dad37829e9d7ad65483d68c86b8", + "name": "proofwidgets", + "inputRev?": "v0.0.11"}}, + {"path": {"name": "Base", "dir": "./../../backends/lean"}}, + {"git": + {"url": "https://github.com/leanprover-community/mathlib4.git", + "subDir?": null, + "rev": "cb02d09e1d5611d22efc2b406e7893f246b2f51e", + "name": "mathlib", + "inputRev?": null}}, + {"git": + {"url": "https://github.com/gebner/quote4", + "subDir?": null, + "rev": "c71f94e34c1cda52eef5c93dc9da409ab2727420", + "name": "Qq", + "inputRev?": "master"}}, + {"git": + {"url": "https://github.com/JLimperg/aesop", + "subDir?": null, + "rev": "ca73109cc40837bc61df8024c9016da4b4f99d4c", + "name": "aesop", + "inputRev?": "master"}}, + {"git": + {"url": "https://github.com/leanprover/std4", + "subDir?": null, + "rev": "e68aa8f5fe47aad78987df45f99094afbcb5e936", + "name": "std", + "inputRev?": "main"}}]} diff --git a/tests/lean/lakefile.lean b/tests/lean/lakefile.lean new file mode 100644 index 00000000..da4293dd --- /dev/null +++ b/tests/lean/lakefile.lean @@ -0,0 +1,14 @@ +import Lake +open Lake DSL + +require mathlib from git + "https://github.com/leanprover-community/mathlib4.git" + +require Base from "../../backends/lean" + +package «tests» {} + +@[default_target] +lean_lib «Tests» {} + +lean_lib hashmap diff --git a/tests/lean/lean-toolchain b/tests/lean/lean-toolchain index bbf57f10..42e7d786 100644 --- a/tests/lean/lean-toolchain +++ b/tests/lean/lean-toolchain @@ -1 +1 @@ -leanprover/lean4:nightly-2023-01-21 +leanprover/lean4:nightly-2023-06-20 \ No newline at end of file diff --git a/tests/lean/misc-constants/Base/Primitives.lean b/tests/lean/misc-constants/Base/Primitives.lean deleted file mode 100644 index 4a66a453..00000000 --- a/tests/lean/misc-constants/Base/Primitives.lean +++ /dev/null @@ -1,583 +0,0 @@ -import Lean -import Lean.Meta.Tactic.Simp -import Init.Data.List.Basic -import Mathlib.Tactic.RunCmd - --------------------- --- ASSERT COMMAND -- --------------------- - -open Lean Elab Command Term Meta - -syntax (name := assert) "#assert" term: command - -@[command_elab assert] -unsafe -def assertImpl : CommandElab := fun (_stx: Syntax) => do - runTermElabM (fun _ => do - let r ← evalTerm Bool (mkConst ``Bool) _stx[1] - if not r then - logInfo "Assertion failed for: " - logInfo _stx[1] - logError "Expression reduced to false" - pure ()) - -#eval 2 == 2 -#assert (2 == 2) - -------------- --- PRELUDE -- -------------- - --- Results & monadic combinators - -inductive Error where - | assertionFailure: Error - | integerOverflow: Error - | divisionByZero: Error - | arrayOutOfBounds: Error - | maximumSizeExceeded: Error - | panic: Error -deriving Repr, BEq - -open Error - -inductive Result (α : Type u) where - | ret (v: α): Result α - | fail (e: Error): Result α -deriving Repr, BEq - -open Result - -instance Result_Inhabited (α : Type u) : Inhabited (Result α) := - Inhabited.mk (fail panic) - -/- HELPERS -/ - -def ret? {α: Type} (r: Result α): Bool := - match r with - | Result.ret _ => true - | Result.fail _ => false - -def massert (b:Bool) : Result Unit := - if b then .ret () else fail assertionFailure - -def eval_global {α: Type} (x: Result α) (_: ret? x): α := - match x with - | Result.fail _ => by contradiction - | Result.ret x => x - -/- DO-DSL SUPPORT -/ - -def bind (x: Result α) (f: α -> Result β) : Result β := - match x with - | ret v => f v - | fail v => fail v - --- Allows using Result in do-blocks -instance : Bind Result where - bind := bind - --- Allows using return x in do-blocks -instance : Pure Result where - pure := fun x => ret x - -/- CUSTOM-DSL SUPPORT -/ - --- Let-binding the Result of a monadic operation is oftentimes not sufficient, --- because we may need a hypothesis for equational reasoning in the scope. We --- rely on subtype, and a custom let-binding operator, in effect recreating our --- own variant of the do-dsl - -def Result.attach {α: Type} (o : Result α): Result { x : α // o = ret x } := - match o with - | .ret x => .ret ⟨x, rfl⟩ - | .fail e => .fail e - -macro "let" e:term " ⟵ " f:term : doElem => - `(doElem| let ⟨$e, h⟩ ← Result.attach $f) - --- TODO: any way to factorize both definitions? -macro "let" e:term " <-- " f:term : doElem => - `(doElem| let ⟨$e, h⟩ ← Result.attach $f) - --- We call the hypothesis `h`, in effect making it unavailable to the user --- (because too much shadowing). But in practice, once can use the French single --- quote notation (input with f< and f>), where `‹ h ›` finds a suitable --- hypothesis in the context, this is equivalent to `have x: h := by assumption in x` -#eval do - let y <-- .ret (0: Nat) - let _: y = 0 := by cases ‹ ret 0 = ret y › ; decide - let r: { x: Nat // x = 0 } := ⟨ y, by assumption ⟩ - .ret r - ----------------------- --- MACHINE INTEGERS -- ----------------------- - --- We redefine our machine integers types. - --- For Isize/Usize, we reuse `getNumBits` from `USize`. You cannot reduce `getNumBits` --- using the simplifier, meaning that proofs do not depend on the compile-time value of --- USize.size. (Lean assumes 32 or 64-bit platforms, and Rust doesn't really support, at --- least officially, 16-bit microcontrollers, so this seems like a fine design decision --- for now.) - --- Note from Chris Bailey: "If there's more than one salient property of your --- definition then the subtyping strategy might get messy, and the property part --- of a subtype is less discoverable by the simplifier or tactics like --- library_search." So, we will not add refinements on the return values of the --- operations defined on Primitives, but will rather rely on custom lemmas to --- invert on possible return values of the primitive operations. - --- Machine integer constants, done via `ofNatCore`, which requires a proof that --- the `Nat` fits within the desired integer type. We provide a custom tactic. - -open System.Platform.getNumBits - --- TODO: is there a way of only importing System.Platform.getNumBits? --- -@[simp] def size_num_bits : Nat := (System.Platform.getNumBits ()).val - --- Remark: Lean seems to use < for the comparisons with the upper bounds by convention. --- We keep the F* convention for now. -@[simp] def Isize.min : Int := - (HPow.hPow 2 (size_num_bits - 1)) -@[simp] def Isize.max : Int := (HPow.hPow 2 (size_num_bits - 1)) - 1 -@[simp] def I8.min : Int := - (HPow.hPow 2 7) -@[simp] def I8.max : Int := HPow.hPow 2 7 - 1 -@[simp] def I16.min : Int := - (HPow.hPow 2 15) -@[simp] def I16.max : Int := HPow.hPow 2 15 - 1 -@[simp] def I32.min : Int := -(HPow.hPow 2 31) -@[simp] def I32.max : Int := HPow.hPow 2 31 - 1 -@[simp] def I64.min : Int := -(HPow.hPow 2 63) -@[simp] def I64.max : Int := HPow.hPow 2 63 - 1 -@[simp] def I128.min : Int := -(HPow.hPow 2 127) -@[simp] def I128.max : Int := HPow.hPow 2 127 - 1 -@[simp] def Usize.min : Int := 0 -@[simp] def Usize.max : Int := HPow.hPow 2 size_num_bits - 1 -@[simp] def U8.min : Int := 0 -@[simp] def U8.max : Int := HPow.hPow 2 8 - 1 -@[simp] def U16.min : Int := 0 -@[simp] def U16.max : Int := HPow.hPow 2 16 - 1 -@[simp] def U32.min : Int := 0 -@[simp] def U32.max : Int := HPow.hPow 2 32 - 1 -@[simp] def U64.min : Int := 0 -@[simp] def U64.max : Int := HPow.hPow 2 64 - 1 -@[simp] def U128.min : Int := 0 -@[simp] def U128.max : Int := HPow.hPow 2 128 - 1 - -#assert (I8.min == -128) -#assert (I8.max == 127) -#assert (I16.min == -32768) -#assert (I16.max == 32767) -#assert (I32.min == -2147483648) -#assert (I32.max == 2147483647) -#assert (I64.min == -9223372036854775808) -#assert (I64.max == 9223372036854775807) -#assert (I128.min == -170141183460469231731687303715884105728) -#assert (I128.max == 170141183460469231731687303715884105727) -#assert (U8.min == 0) -#assert (U8.max == 255) -#assert (U16.min == 0) -#assert (U16.max == 65535) -#assert (U32.min == 0) -#assert (U32.max == 4294967295) -#assert (U64.min == 0) -#assert (U64.max == 18446744073709551615) -#assert (U128.min == 0) -#assert (U128.max == 340282366920938463463374607431768211455) - -inductive ScalarTy := -| Isize -| I8 -| I16 -| I32 -| I64 -| I128 -| Usize -| U8 -| U16 -| U32 -| U64 -| U128 - -def Scalar.min (ty : ScalarTy) : Int := - match ty with - | .Isize => Isize.min - | .I8 => I8.min - | .I16 => I16.min - | .I32 => I32.min - | .I64 => I64.min - | .I128 => I128.min - | .Usize => Usize.min - | .U8 => U8.min - | .U16 => U16.min - | .U32 => U32.min - | .U64 => U64.min - | .U128 => U128.min - -def Scalar.max (ty : ScalarTy) : Int := - match ty with - | .Isize => Isize.max - | .I8 => I8.max - | .I16 => I16.max - | .I32 => I32.max - | .I64 => I64.max - | .I128 => I128.max - | .Usize => Usize.max - | .U8 => U8.max - | .U16 => U16.max - | .U32 => U32.max - | .U64 => U64.max - | .U128 => U128.max - --- "Conservative" bounds --- We use those because we can't compare to the isize bounds (which can't --- reduce at compile-time). Whenever we perform an arithmetic operation like --- addition we need to check that the result is in bounds: we first compare --- to the conservative bounds, which reduce, then compare to the real bounds. --- This is useful for the various #asserts that we want to reduce at --- type-checking time. -def Scalar.cMin (ty : ScalarTy) : Int := - match ty with - | .Isize => I32.min - | _ => Scalar.min ty - -def Scalar.cMax (ty : ScalarTy) : Int := - match ty with - | .Isize => I32.max - | .Usize => U32.max - | _ => Scalar.max ty - -theorem Scalar.cMin_bound ty : Scalar.min ty <= Scalar.cMin ty := by sorry -theorem Scalar.cMax_bound ty : Scalar.min ty <= Scalar.cMin ty := by sorry - -structure Scalar (ty : ScalarTy) where - val : Int - hmin : Scalar.min ty <= val - hmax : val <= Scalar.max ty - -theorem Scalar.bound_suffices (ty : ScalarTy) (x : Int) : - Scalar.cMin ty <= x && x <= Scalar.cMax ty -> - (decide (Scalar.min ty ≤ x) && decide (x ≤ Scalar.max ty)) = true - := by sorry - -def Scalar.ofIntCore {ty : ScalarTy} (x : Int) - (hmin : Scalar.min ty <= x) (hmax : x <= Scalar.max ty) : Scalar ty := - { val := x, hmin := hmin, hmax := hmax } - -def Scalar.ofInt {ty : ScalarTy} (x : Int) - (h : Scalar.min ty <= x && x <= Scalar.max ty) : Scalar ty := - let hmin: Scalar.min ty <= x := by sorry - let hmax: x <= Scalar.max ty := by sorry - Scalar.ofIntCore x hmin hmax - --- Further thoughts: look at what has been done here: --- https://github.com/leanprover-community/mathlib4/blob/master/Mathlib/Data/Fin/Basic.lean --- and --- https://github.com/leanprover-community/mathlib4/blob/master/Mathlib/Data/UInt.lean --- which both contain a fair amount of reasoning already! -def Scalar.tryMk (ty : ScalarTy) (x : Int) : Result (Scalar ty) := - -- TODO: write this with only one if then else - if hmin_cons: Scalar.cMin ty <= x || Scalar.min ty <= x then - if hmax_cons: x <= Scalar.cMax ty || x <= Scalar.max ty then - let hmin: Scalar.min ty <= x := by sorry - let hmax: x <= Scalar.max ty := by sorry - return Scalar.ofIntCore x hmin hmax - else fail integerOverflow - else fail integerOverflow - -def Scalar.neg {ty : ScalarTy} (x : Scalar ty) : Result (Scalar ty) := Scalar.tryMk ty (- x.val) - -def Scalar.div {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - if y.val != 0 then Scalar.tryMk ty (x.val / y.val) else fail divisionByZero - --- Checking that the % operation in Lean computes the same as the remainder operation in Rust -#assert 1 % 2 = (1:Int) -#assert (-1) % 2 = -1 -#assert 1 % (-2) = 1 -#assert (-1) % (-2) = -1 - -def Scalar.rem {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - if y.val != 0 then Scalar.tryMk ty (x.val % y.val) else fail divisionByZero - -def Scalar.add {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - Scalar.tryMk ty (x.val + y.val) - -def Scalar.sub {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - Scalar.tryMk ty (x.val - y.val) - -def Scalar.mul {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - Scalar.tryMk ty (x.val * y.val) - --- TODO: instances of +, -, * etc. for scalars - --- Cast an integer from a [src_ty] to a [tgt_ty] --- TODO: check the semantics of casts in Rust -def Scalar.cast {src_ty : ScalarTy} (tgt_ty : ScalarTy) (x : Scalar src_ty) : Result (Scalar tgt_ty) := - Scalar.tryMk tgt_ty x.val - --- The scalar types --- We declare the definitions as reducible so that Lean can unfold them (useful --- for type class resolution for instance). -@[reducible] def Isize := Scalar .Isize -@[reducible] def I8 := Scalar .I8 -@[reducible] def I16 := Scalar .I16 -@[reducible] def I32 := Scalar .I32 -@[reducible] def I64 := Scalar .I64 -@[reducible] def I128 := Scalar .I128 -@[reducible] def Usize := Scalar .Usize -@[reducible] def U8 := Scalar .U8 -@[reducible] def U16 := Scalar .U16 -@[reducible] def U32 := Scalar .U32 -@[reducible] def U64 := Scalar .U64 -@[reducible] def U128 := Scalar .U128 - --- TODO: below: not sure this is the best way. --- Should we rather overload operations like +, -, etc.? --- Also, it is possible to automate the generation of those definitions --- with macros (but would it be a good idea? It would be less easy to --- read the file, which is not supposed to change a lot) - --- Negation - -/-- -Remark: there is no heterogeneous negation in the Lean prelude: we thus introduce -one here. - -The notation typeclass for heterogeneous addition. -This enables the notation `- a : β` where `a : α`. --/ -class HNeg (α : Type u) (β : outParam (Type v)) where - /-- `- a` computes the negation of `a`. - The meaning of this notation is type-dependent. -/ - hNeg : α → β - -prefix:75 "-" => HNeg.hNeg - -instance : HNeg Isize (Result Isize) where hNeg x := Scalar.neg x -instance : HNeg I8 (Result I8) where hNeg x := Scalar.neg x -instance : HNeg I16 (Result I16) where hNeg x := Scalar.neg x -instance : HNeg I32 (Result I32) where hNeg x := Scalar.neg x -instance : HNeg I64 (Result I64) where hNeg x := Scalar.neg x -instance : HNeg I128 (Result I128) where hNeg x := Scalar.neg x - --- Addition -instance {ty} : HAdd (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hAdd x y := Scalar.add x y - --- Substraction -instance {ty} : HSub (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hSub x y := Scalar.sub x y - --- Multiplication -instance {ty} : HMul (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hMul x y := Scalar.mul x y - --- Division -instance {ty} : HDiv (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hDiv x y := Scalar.div x y - --- Remainder -instance {ty} : HMod (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hMod x y := Scalar.rem x y - --- ofIntCore --- TODO: typeclass? -def Isize.ofIntCore := @Scalar.ofIntCore .Isize -def I8.ofIntCore := @Scalar.ofIntCore .I8 -def I16.ofIntCore := @Scalar.ofIntCore .I16 -def I32.ofIntCore := @Scalar.ofIntCore .I32 -def I64.ofIntCore := @Scalar.ofIntCore .I64 -def I128.ofIntCore := @Scalar.ofIntCore .I128 -def Usize.ofIntCore := @Scalar.ofIntCore .Usize -def U8.ofIntCore := @Scalar.ofIntCore .U8 -def U16.ofIntCore := @Scalar.ofIntCore .U16 -def U32.ofIntCore := @Scalar.ofIntCore .U32 -def U64.ofIntCore := @Scalar.ofIntCore .U64 -def U128.ofIntCore := @Scalar.ofIntCore .U128 - --- ofInt --- TODO: typeclass? -def Isize.ofInt := @Scalar.ofInt .Isize -def I8.ofInt := @Scalar.ofInt .I8 -def I16.ofInt := @Scalar.ofInt .I16 -def I32.ofInt := @Scalar.ofInt .I32 -def I64.ofInt := @Scalar.ofInt .I64 -def I128.ofInt := @Scalar.ofInt .I128 -def Usize.ofInt := @Scalar.ofInt .Usize -def U8.ofInt := @Scalar.ofInt .U8 -def U16.ofInt := @Scalar.ofInt .U16 -def U32.ofInt := @Scalar.ofInt .U32 -def U64.ofInt := @Scalar.ofInt .U64 -def U128.ofInt := @Scalar.ofInt .U128 - --- Comparisons -instance {ty} : LT (Scalar ty) where - lt a b := LT.lt a.val b.val - -instance {ty} : LE (Scalar ty) where le a b := LE.le a.val b.val - -instance Scalar.decLt {ty} (a b : Scalar ty) : Decidable (LT.lt a b) := Int.decLt .. -instance Scalar.decLe {ty} (a b : Scalar ty) : Decidable (LE.le a b) := Int.decLe .. - -theorem Scalar.eq_of_val_eq {ty} : ∀ {i j : Scalar ty}, Eq i.val j.val → Eq i j - | ⟨_, _, _⟩, ⟨_, _, _⟩, rfl => rfl - -theorem Scalar.val_eq_of_eq {ty} {i j : Scalar ty} (h : Eq i j) : Eq i.val j.val := - h ▸ rfl - -theorem Scalar.ne_of_val_ne {ty} {i j : Scalar ty} (h : Not (Eq i.val j.val)) : Not (Eq i j) := - fun h' => absurd (val_eq_of_eq h') h - -instance (ty : ScalarTy) : DecidableEq (Scalar ty) := - fun i j => - match decEq i.val j.val with - | isTrue h => isTrue (Scalar.eq_of_val_eq h) - | isFalse h => isFalse (Scalar.ne_of_val_ne h) - -def Scalar.toInt {ty} (n : Scalar ty) : Int := n.val - --- Tactic to prove that integers are in bounds -syntax "intlit" : tactic - -macro_rules - | `(tactic| intlit) => `(tactic| apply Scalar.bound_suffices ; decide) - --- -- We now define a type class that subsumes the various machine integer types, so --- -- as to write a concise definition for scalar_cast, rather than exhaustively --- -- enumerating all of the possible pairs. We remark that Rust has sane semantics --- -- and fails if a cast operation would involve a truncation or modulo. - --- class MachineInteger (t: Type) where --- size: Nat --- val: t -> Fin size --- ofNatCore: (n:Nat) -> LT.lt n size -> t - --- set_option hygiene false in --- run_cmd --- for typeName in [`UInt8, `UInt16, `UInt32, `UInt64, `USize].map Lean.mkIdent do --- Lean.Elab.Command.elabCommand (← `( --- namespace $typeName --- instance: MachineInteger $typeName where --- size := size --- val := val --- ofNatCore := ofNatCore --- end $typeName --- )) - --- -- Aeneas only instantiates the destination type (`src` is implicit). We rely on --- -- Lean to infer `src`. - --- def scalar_cast { src: Type } (dst: Type) [ MachineInteger src ] [ MachineInteger dst ] (x: src): Result dst := --- if h: MachineInteger.val x < MachineInteger.size dst then --- .ret (MachineInteger.ofNatCore (MachineInteger.val x).val h) --- else --- .fail integerOverflow - -------------- --- VECTORS -- -------------- - -def Vec (α : Type u) := { l : List α // List.length l <= Usize.max } - -def vec_new (α : Type u): Vec α := ⟨ [], by sorry ⟩ - -def vec_len (α : Type u) (v : Vec α) : Usize := - let ⟨ v, l ⟩ := v - Usize.ofIntCore (List.length v) (by sorry) l - -def vec_push_fwd (α : Type u) (_ : Vec α) (_ : α) : Unit := () - -def vec_push_back (α : Type u) (v : Vec α) (x : α) : Result (Vec α) - := - if h : List.length v.val <= U32.max || List.length v.val <= Usize.max then - return ⟨ List.concat v.val x, by sorry ⟩ - else - fail maximumSizeExceeded - -def vec_insert_fwd (α : Type u) (v: Vec α) (i: Usize) (_: α): Result Unit := - if i.val < List.length v.val then - .ret () - else - .fail arrayOutOfBounds - -def vec_insert_back (α : Type u) (v: Vec α) (i: Usize) (x: α): Result (Vec α) := - if i.val < List.length v.val then - -- TODO: maybe we should redefine a list library which uses integers - -- (instead of natural numbers) - let i : Nat := - match i.val with - | .ofNat n => n - | .negSucc n => by sorry -- TODO: we can't get here - let isLt: i < USize.size := by sorry - let i : Fin USize.size := { val := i, isLt := isLt } - .ret ⟨ List.set v.val i.val x, by - have h: List.length v.val <= Usize.max := v.property - rewrite [ List.length_set v.val i.val x ] - assumption - ⟩ - else - .fail arrayOutOfBounds - -def vec_index_fwd (α : Type u) (v: Vec α) (i: Usize): Result α := - if i.val < List.length v.val then - let i : Nat := - match i.val with - | .ofNat n => n - | .negSucc n => by sorry -- TODO: we can't get here - let isLt: i < USize.size := by sorry - let i : Fin USize.size := { val := i, isLt := isLt } - let h: i < List.length v.val := by sorry - .ret (List.get v.val ⟨i.val, h⟩) - else - .fail arrayOutOfBounds - -def vec_index_back (α : Type u) (v: Vec α) (i: Usize) (_: α): Result Unit := - if i.val < List.length v.val then - .ret () - else - .fail arrayOutOfBounds - -def vec_index_mut_fwd (α : Type u) (v: Vec α) (i: Usize): Result α := - if i.val < List.length v.val then - let i : Nat := - match i.val with - | .ofNat n => n - | .negSucc n => by sorry -- TODO: we can't get here - let isLt: i < USize.size := by sorry - let i : Fin USize.size := { val := i, isLt := isLt } - let h: i < List.length v.val := by sorry - .ret (List.get v.val ⟨i.val, h⟩) - else - .fail arrayOutOfBounds - -def vec_index_mut_back (α : Type u) (v: Vec α) (i: Usize) (x: α): Result (Vec α) := - if i.val < List.length v.val then - let i : Nat := - match i.val with - | .ofNat n => n - | .negSucc n => by sorry -- TODO: we can't get here - let isLt: i < USize.size := by sorry - let i : Fin USize.size := { val := i, isLt := isLt } - .ret ⟨ List.set v.val i.val x, by - have h: List.length v.val <= Usize.max := v.property - rewrite [ List.length_set v.val i.val x ] - assumption - ⟩ - else - .fail arrayOutOfBounds - ----------- --- MISC -- ----------- - -def mem_replace_fwd (a : Type) (x : a) (_ : a) : a := - x - -def mem_replace_back (a : Type) (_ : a) (y : a) : a := - y - -/-- Aeneas-translated function -- useful to reduce non-recursive definitions. - Use with `simp [ aeneas ]` -/ -register_simp_attr aeneas diff --git a/tests/lean/misc-constants/Constants.lean b/tests/lean/misc-constants/Constants.lean deleted file mode 100644 index 8306ed85..00000000 --- a/tests/lean/misc-constants/Constants.lean +++ /dev/null @@ -1,131 +0,0 @@ --- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS --- [constants] -import Base.Primitives - -/- [constants::X0] -/ -def x0_body : Result U32 := Result.ret (U32.ofInt 0 (by intlit)) -def x0_c : U32 := eval_global x0_body (by simp) - -/- [core::num::u32::{9}::MAX] -/ -def core_num_u32_max_body : Result U32 := - Result.ret (U32.ofInt 4294967295 (by intlit)) -def core_num_u32_max_c : U32 := eval_global core_num_u32_max_body (by simp) - -/- [constants::X1] -/ -def x1_body : Result U32 := Result.ret core_num_u32_max_c -def x1_c : U32 := eval_global x1_body (by simp) - -/- [constants::X2] -/ -def x2_body : Result U32 := Result.ret (U32.ofInt 3 (by intlit)) -def x2_c : U32 := eval_global x2_body (by simp) - -/- [constants::incr] -/ -def incr_fwd (n : U32) : Result U32 := - n + (U32.ofInt 1 (by intlit)) - -/- [constants::X3] -/ -def x3_body : Result U32 := incr_fwd (U32.ofInt 32 (by intlit)) -def x3_c : U32 := eval_global x3_body (by simp) - -/- [constants::mk_pair0] -/ -def mk_pair0_fwd (x : U32) (y : U32) : Result (U32 × U32) := - Result.ret (x, y) - -/- [constants::Pair] -/ -structure pair_t (T1 T2 : Type) where - pair_x : T1 - pair_y : T2 - -/- [constants::mk_pair1] -/ -def mk_pair1_fwd (x : U32) (y : U32) : Result (pair_t U32 U32) := - Result.ret { pair_x := x, pair_y := y } - -/- [constants::P0] -/ -def p0_body : Result (U32 × U32) := - mk_pair0_fwd (U32.ofInt 0 (by intlit)) (U32.ofInt 1 (by intlit)) -def p0_c : (U32 × U32) := eval_global p0_body (by simp) - -/- [constants::P1] -/ -def p1_body : Result (pair_t U32 U32) := - mk_pair1_fwd (U32.ofInt 0 (by intlit)) (U32.ofInt 1 (by intlit)) -def p1_c : pair_t U32 U32 := eval_global p1_body (by simp) - -/- [constants::P2] -/ -def p2_body : Result (U32 × U32) := - Result.ret ((U32.ofInt 0 (by intlit)), (U32.ofInt 1 (by intlit))) -def p2_c : (U32 × U32) := eval_global p2_body (by simp) - -/- [constants::P3] -/ -def p3_body : Result (pair_t U32 U32) := - Result.ret - { pair_x := (U32.ofInt 0 (by intlit)), pair_y := (U32.ofInt 1 (by intlit)) } -def p3_c : pair_t U32 U32 := eval_global p3_body (by simp) - -/- [constants::Wrap] -/ -structure wrap_t (T : Type) where - wrap_val : T - -/- [constants::Wrap::{0}::new] -/ -def wrap_new_fwd (T : Type) (val : T) : Result (wrap_t T) := - Result.ret { wrap_val := val } - -/- [constants::Y] -/ -def y_body : Result (wrap_t I32) := wrap_new_fwd I32 (I32.ofInt 2 (by intlit)) -def y_c : wrap_t I32 := eval_global y_body (by simp) - -/- [constants::unwrap_y] -/ -def unwrap_y_fwd : Result I32 := - Result.ret y_c.wrap_val - -/- [constants::YVAL] -/ -def yval_body : Result I32 := unwrap_y_fwd -def yval_c : I32 := eval_global yval_body (by simp) - -/- [constants::get_z1::Z1] -/ -def get_z1_z1_body : Result I32 := Result.ret (I32.ofInt 3 (by intlit)) -def get_z1_z1_c : I32 := eval_global get_z1_z1_body (by simp) - -/- [constants::get_z1] -/ -def get_z1_fwd : Result I32 := - Result.ret get_z1_z1_c - -/- [constants::add] -/ -def add_fwd (a : I32) (b : I32) : Result I32 := - a + b - -/- [constants::Q1] -/ -def q1_body : Result I32 := Result.ret (I32.ofInt 5 (by intlit)) -def q1_c : I32 := eval_global q1_body (by simp) - -/- [constants::Q2] -/ -def q2_body : Result I32 := Result.ret q1_c -def q2_c : I32 := eval_global q2_body (by simp) - -/- [constants::Q3] -/ -def q3_body : Result I32 := add_fwd q2_c (I32.ofInt 3 (by intlit)) -def q3_c : I32 := eval_global q3_body (by simp) - -/- [constants::get_z2] -/ -def get_z2_fwd : Result I32 := - do - let i ← get_z1_fwd - let i0 ← add_fwd i q3_c - add_fwd q1_c i0 - -/- [constants::S1] -/ -def s1_body : Result U32 := Result.ret (U32.ofInt 6 (by intlit)) -def s1_c : U32 := eval_global s1_body (by simp) - -/- [constants::S2] -/ -def s2_body : Result U32 := incr_fwd s1_c -def s2_c : U32 := eval_global s2_body (by simp) - -/- [constants::S3] -/ -def s3_body : Result (pair_t U32 U32) := Result.ret p3_c -def s3_c : pair_t U32 U32 := eval_global s3_body (by simp) - -/- [constants::S4] -/ -def s4_body : Result (pair_t U32 U32) := - mk_pair1_fwd (U32.ofInt 7 (by intlit)) (U32.ofInt 8 (by intlit)) -def s4_c : pair_t U32 U32 := eval_global s4_body (by simp) - diff --git a/tests/lean/misc-constants/lake-manifest.json b/tests/lean/misc-constants/lake-manifest.json deleted file mode 100644 index 57b071ca..00000000 --- a/tests/lean/misc-constants/lake-manifest.json +++ /dev/null @@ -1,27 +0,0 @@ -{"version": 4, - "packagesDir": "./lake-packages", - "packages": - [{"git": - {"url": "https://github.com/leanprover-community/mathlib4.git", - "subDir?": null, - "rev": "4037792ead804d7bfa8868e2c4684d4223c15ece", - "name": "mathlib", - "inputRev?": null}}, - {"git": - {"url": "https://github.com/gebner/quote4", - "subDir?": null, - "rev": "2412c4fdf4a8b689f4467618e5e7b371ae5014aa", - "name": "Qq", - "inputRev?": "master"}}, - {"git": - {"url": "https://github.com/JLimperg/aesop", - "subDir?": null, - "rev": "7fe9ecd9339b0e1796e89d243b776849c305c690", - "name": "aesop", - "inputRev?": "master"}}, - {"git": - {"url": "https://github.com/leanprover/std4", - "subDir?": null, - "rev": "24897887905b3a1254b244369f5dd2cf6174b0ee", - "name": "std", - "inputRev?": "main"}}]} diff --git a/tests/lean/misc-constants/lakefile.lean b/tests/lean/misc-constants/lakefile.lean deleted file mode 100644 index 01aacb90..00000000 --- a/tests/lean/misc-constants/lakefile.lean +++ /dev/null @@ -1,12 +0,0 @@ -import Lake -open Lake DSL - -require mathlib from git - "https://github.com/leanprover-community/mathlib4.git" - -package «constants» {} - -lean_lib «Base» {} - -@[default_target] -lean_lib «Constants» {} diff --git a/tests/lean/misc-constants/lean-toolchain b/tests/lean/misc-constants/lean-toolchain deleted file mode 100644 index bbf57f10..00000000 --- a/tests/lean/misc-constants/lean-toolchain +++ /dev/null @@ -1 +0,0 @@ -leanprover/lean4:nightly-2023-01-21 diff --git a/tests/lean/misc-external/Base/Primitives.lean b/tests/lean/misc-external/Base/Primitives.lean deleted file mode 100644 index 4a66a453..00000000 --- a/tests/lean/misc-external/Base/Primitives.lean +++ /dev/null @@ -1,583 +0,0 @@ -import Lean -import Lean.Meta.Tactic.Simp -import Init.Data.List.Basic -import Mathlib.Tactic.RunCmd - --------------------- --- ASSERT COMMAND -- --------------------- - -open Lean Elab Command Term Meta - -syntax (name := assert) "#assert" term: command - -@[command_elab assert] -unsafe -def assertImpl : CommandElab := fun (_stx: Syntax) => do - runTermElabM (fun _ => do - let r ← evalTerm Bool (mkConst ``Bool) _stx[1] - if not r then - logInfo "Assertion failed for: " - logInfo _stx[1] - logError "Expression reduced to false" - pure ()) - -#eval 2 == 2 -#assert (2 == 2) - -------------- --- PRELUDE -- -------------- - --- Results & monadic combinators - -inductive Error where - | assertionFailure: Error - | integerOverflow: Error - | divisionByZero: Error - | arrayOutOfBounds: Error - | maximumSizeExceeded: Error - | panic: Error -deriving Repr, BEq - -open Error - -inductive Result (α : Type u) where - | ret (v: α): Result α - | fail (e: Error): Result α -deriving Repr, BEq - -open Result - -instance Result_Inhabited (α : Type u) : Inhabited (Result α) := - Inhabited.mk (fail panic) - -/- HELPERS -/ - -def ret? {α: Type} (r: Result α): Bool := - match r with - | Result.ret _ => true - | Result.fail _ => false - -def massert (b:Bool) : Result Unit := - if b then .ret () else fail assertionFailure - -def eval_global {α: Type} (x: Result α) (_: ret? x): α := - match x with - | Result.fail _ => by contradiction - | Result.ret x => x - -/- DO-DSL SUPPORT -/ - -def bind (x: Result α) (f: α -> Result β) : Result β := - match x with - | ret v => f v - | fail v => fail v - --- Allows using Result in do-blocks -instance : Bind Result where - bind := bind - --- Allows using return x in do-blocks -instance : Pure Result where - pure := fun x => ret x - -/- CUSTOM-DSL SUPPORT -/ - --- Let-binding the Result of a monadic operation is oftentimes not sufficient, --- because we may need a hypothesis for equational reasoning in the scope. We --- rely on subtype, and a custom let-binding operator, in effect recreating our --- own variant of the do-dsl - -def Result.attach {α: Type} (o : Result α): Result { x : α // o = ret x } := - match o with - | .ret x => .ret ⟨x, rfl⟩ - | .fail e => .fail e - -macro "let" e:term " ⟵ " f:term : doElem => - `(doElem| let ⟨$e, h⟩ ← Result.attach $f) - --- TODO: any way to factorize both definitions? -macro "let" e:term " <-- " f:term : doElem => - `(doElem| let ⟨$e, h⟩ ← Result.attach $f) - --- We call the hypothesis `h`, in effect making it unavailable to the user --- (because too much shadowing). But in practice, once can use the French single --- quote notation (input with f< and f>), where `‹ h ›` finds a suitable --- hypothesis in the context, this is equivalent to `have x: h := by assumption in x` -#eval do - let y <-- .ret (0: Nat) - let _: y = 0 := by cases ‹ ret 0 = ret y › ; decide - let r: { x: Nat // x = 0 } := ⟨ y, by assumption ⟩ - .ret r - ----------------------- --- MACHINE INTEGERS -- ----------------------- - --- We redefine our machine integers types. - --- For Isize/Usize, we reuse `getNumBits` from `USize`. You cannot reduce `getNumBits` --- using the simplifier, meaning that proofs do not depend on the compile-time value of --- USize.size. (Lean assumes 32 or 64-bit platforms, and Rust doesn't really support, at --- least officially, 16-bit microcontrollers, so this seems like a fine design decision --- for now.) - --- Note from Chris Bailey: "If there's more than one salient property of your --- definition then the subtyping strategy might get messy, and the property part --- of a subtype is less discoverable by the simplifier or tactics like --- library_search." So, we will not add refinements on the return values of the --- operations defined on Primitives, but will rather rely on custom lemmas to --- invert on possible return values of the primitive operations. - --- Machine integer constants, done via `ofNatCore`, which requires a proof that --- the `Nat` fits within the desired integer type. We provide a custom tactic. - -open System.Platform.getNumBits - --- TODO: is there a way of only importing System.Platform.getNumBits? --- -@[simp] def size_num_bits : Nat := (System.Platform.getNumBits ()).val - --- Remark: Lean seems to use < for the comparisons with the upper bounds by convention. --- We keep the F* convention for now. -@[simp] def Isize.min : Int := - (HPow.hPow 2 (size_num_bits - 1)) -@[simp] def Isize.max : Int := (HPow.hPow 2 (size_num_bits - 1)) - 1 -@[simp] def I8.min : Int := - (HPow.hPow 2 7) -@[simp] def I8.max : Int := HPow.hPow 2 7 - 1 -@[simp] def I16.min : Int := - (HPow.hPow 2 15) -@[simp] def I16.max : Int := HPow.hPow 2 15 - 1 -@[simp] def I32.min : Int := -(HPow.hPow 2 31) -@[simp] def I32.max : Int := HPow.hPow 2 31 - 1 -@[simp] def I64.min : Int := -(HPow.hPow 2 63) -@[simp] def I64.max : Int := HPow.hPow 2 63 - 1 -@[simp] def I128.min : Int := -(HPow.hPow 2 127) -@[simp] def I128.max : Int := HPow.hPow 2 127 - 1 -@[simp] def Usize.min : Int := 0 -@[simp] def Usize.max : Int := HPow.hPow 2 size_num_bits - 1 -@[simp] def U8.min : Int := 0 -@[simp] def U8.max : Int := HPow.hPow 2 8 - 1 -@[simp] def U16.min : Int := 0 -@[simp] def U16.max : Int := HPow.hPow 2 16 - 1 -@[simp] def U32.min : Int := 0 -@[simp] def U32.max : Int := HPow.hPow 2 32 - 1 -@[simp] def U64.min : Int := 0 -@[simp] def U64.max : Int := HPow.hPow 2 64 - 1 -@[simp] def U128.min : Int := 0 -@[simp] def U128.max : Int := HPow.hPow 2 128 - 1 - -#assert (I8.min == -128) -#assert (I8.max == 127) -#assert (I16.min == -32768) -#assert (I16.max == 32767) -#assert (I32.min == -2147483648) -#assert (I32.max == 2147483647) -#assert (I64.min == -9223372036854775808) -#assert (I64.max == 9223372036854775807) -#assert (I128.min == -170141183460469231731687303715884105728) -#assert (I128.max == 170141183460469231731687303715884105727) -#assert (U8.min == 0) -#assert (U8.max == 255) -#assert (U16.min == 0) -#assert (U16.max == 65535) -#assert (U32.min == 0) -#assert (U32.max == 4294967295) -#assert (U64.min == 0) -#assert (U64.max == 18446744073709551615) -#assert (U128.min == 0) -#assert (U128.max == 340282366920938463463374607431768211455) - -inductive ScalarTy := -| Isize -| I8 -| I16 -| I32 -| I64 -| I128 -| Usize -| U8 -| U16 -| U32 -| U64 -| U128 - -def Scalar.min (ty : ScalarTy) : Int := - match ty with - | .Isize => Isize.min - | .I8 => I8.min - | .I16 => I16.min - | .I32 => I32.min - | .I64 => I64.min - | .I128 => I128.min - | .Usize => Usize.min - | .U8 => U8.min - | .U16 => U16.min - | .U32 => U32.min - | .U64 => U64.min - | .U128 => U128.min - -def Scalar.max (ty : ScalarTy) : Int := - match ty with - | .Isize => Isize.max - | .I8 => I8.max - | .I16 => I16.max - | .I32 => I32.max - | .I64 => I64.max - | .I128 => I128.max - | .Usize => Usize.max - | .U8 => U8.max - | .U16 => U16.max - | .U32 => U32.max - | .U64 => U64.max - | .U128 => U128.max - --- "Conservative" bounds --- We use those because we can't compare to the isize bounds (which can't --- reduce at compile-time). Whenever we perform an arithmetic operation like --- addition we need to check that the result is in bounds: we first compare --- to the conservative bounds, which reduce, then compare to the real bounds. --- This is useful for the various #asserts that we want to reduce at --- type-checking time. -def Scalar.cMin (ty : ScalarTy) : Int := - match ty with - | .Isize => I32.min - | _ => Scalar.min ty - -def Scalar.cMax (ty : ScalarTy) : Int := - match ty with - | .Isize => I32.max - | .Usize => U32.max - | _ => Scalar.max ty - -theorem Scalar.cMin_bound ty : Scalar.min ty <= Scalar.cMin ty := by sorry -theorem Scalar.cMax_bound ty : Scalar.min ty <= Scalar.cMin ty := by sorry - -structure Scalar (ty : ScalarTy) where - val : Int - hmin : Scalar.min ty <= val - hmax : val <= Scalar.max ty - -theorem Scalar.bound_suffices (ty : ScalarTy) (x : Int) : - Scalar.cMin ty <= x && x <= Scalar.cMax ty -> - (decide (Scalar.min ty ≤ x) && decide (x ≤ Scalar.max ty)) = true - := by sorry - -def Scalar.ofIntCore {ty : ScalarTy} (x : Int) - (hmin : Scalar.min ty <= x) (hmax : x <= Scalar.max ty) : Scalar ty := - { val := x, hmin := hmin, hmax := hmax } - -def Scalar.ofInt {ty : ScalarTy} (x : Int) - (h : Scalar.min ty <= x && x <= Scalar.max ty) : Scalar ty := - let hmin: Scalar.min ty <= x := by sorry - let hmax: x <= Scalar.max ty := by sorry - Scalar.ofIntCore x hmin hmax - --- Further thoughts: look at what has been done here: --- https://github.com/leanprover-community/mathlib4/blob/master/Mathlib/Data/Fin/Basic.lean --- and --- https://github.com/leanprover-community/mathlib4/blob/master/Mathlib/Data/UInt.lean --- which both contain a fair amount of reasoning already! -def Scalar.tryMk (ty : ScalarTy) (x : Int) : Result (Scalar ty) := - -- TODO: write this with only one if then else - if hmin_cons: Scalar.cMin ty <= x || Scalar.min ty <= x then - if hmax_cons: x <= Scalar.cMax ty || x <= Scalar.max ty then - let hmin: Scalar.min ty <= x := by sorry - let hmax: x <= Scalar.max ty := by sorry - return Scalar.ofIntCore x hmin hmax - else fail integerOverflow - else fail integerOverflow - -def Scalar.neg {ty : ScalarTy} (x : Scalar ty) : Result (Scalar ty) := Scalar.tryMk ty (- x.val) - -def Scalar.div {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - if y.val != 0 then Scalar.tryMk ty (x.val / y.val) else fail divisionByZero - --- Checking that the % operation in Lean computes the same as the remainder operation in Rust -#assert 1 % 2 = (1:Int) -#assert (-1) % 2 = -1 -#assert 1 % (-2) = 1 -#assert (-1) % (-2) = -1 - -def Scalar.rem {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - if y.val != 0 then Scalar.tryMk ty (x.val % y.val) else fail divisionByZero - -def Scalar.add {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - Scalar.tryMk ty (x.val + y.val) - -def Scalar.sub {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - Scalar.tryMk ty (x.val - y.val) - -def Scalar.mul {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - Scalar.tryMk ty (x.val * y.val) - --- TODO: instances of +, -, * etc. for scalars - --- Cast an integer from a [src_ty] to a [tgt_ty] --- TODO: check the semantics of casts in Rust -def Scalar.cast {src_ty : ScalarTy} (tgt_ty : ScalarTy) (x : Scalar src_ty) : Result (Scalar tgt_ty) := - Scalar.tryMk tgt_ty x.val - --- The scalar types --- We declare the definitions as reducible so that Lean can unfold them (useful --- for type class resolution for instance). -@[reducible] def Isize := Scalar .Isize -@[reducible] def I8 := Scalar .I8 -@[reducible] def I16 := Scalar .I16 -@[reducible] def I32 := Scalar .I32 -@[reducible] def I64 := Scalar .I64 -@[reducible] def I128 := Scalar .I128 -@[reducible] def Usize := Scalar .Usize -@[reducible] def U8 := Scalar .U8 -@[reducible] def U16 := Scalar .U16 -@[reducible] def U32 := Scalar .U32 -@[reducible] def U64 := Scalar .U64 -@[reducible] def U128 := Scalar .U128 - --- TODO: below: not sure this is the best way. --- Should we rather overload operations like +, -, etc.? --- Also, it is possible to automate the generation of those definitions --- with macros (but would it be a good idea? It would be less easy to --- read the file, which is not supposed to change a lot) - --- Negation - -/-- -Remark: there is no heterogeneous negation in the Lean prelude: we thus introduce -one here. - -The notation typeclass for heterogeneous addition. -This enables the notation `- a : β` where `a : α`. --/ -class HNeg (α : Type u) (β : outParam (Type v)) where - /-- `- a` computes the negation of `a`. - The meaning of this notation is type-dependent. -/ - hNeg : α → β - -prefix:75 "-" => HNeg.hNeg - -instance : HNeg Isize (Result Isize) where hNeg x := Scalar.neg x -instance : HNeg I8 (Result I8) where hNeg x := Scalar.neg x -instance : HNeg I16 (Result I16) where hNeg x := Scalar.neg x -instance : HNeg I32 (Result I32) where hNeg x := Scalar.neg x -instance : HNeg I64 (Result I64) where hNeg x := Scalar.neg x -instance : HNeg I128 (Result I128) where hNeg x := Scalar.neg x - --- Addition -instance {ty} : HAdd (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hAdd x y := Scalar.add x y - --- Substraction -instance {ty} : HSub (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hSub x y := Scalar.sub x y - --- Multiplication -instance {ty} : HMul (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hMul x y := Scalar.mul x y - --- Division -instance {ty} : HDiv (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hDiv x y := Scalar.div x y - --- Remainder -instance {ty} : HMod (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hMod x y := Scalar.rem x y - --- ofIntCore --- TODO: typeclass? -def Isize.ofIntCore := @Scalar.ofIntCore .Isize -def I8.ofIntCore := @Scalar.ofIntCore .I8 -def I16.ofIntCore := @Scalar.ofIntCore .I16 -def I32.ofIntCore := @Scalar.ofIntCore .I32 -def I64.ofIntCore := @Scalar.ofIntCore .I64 -def I128.ofIntCore := @Scalar.ofIntCore .I128 -def Usize.ofIntCore := @Scalar.ofIntCore .Usize -def U8.ofIntCore := @Scalar.ofIntCore .U8 -def U16.ofIntCore := @Scalar.ofIntCore .U16 -def U32.ofIntCore := @Scalar.ofIntCore .U32 -def U64.ofIntCore := @Scalar.ofIntCore .U64 -def U128.ofIntCore := @Scalar.ofIntCore .U128 - --- ofInt --- TODO: typeclass? -def Isize.ofInt := @Scalar.ofInt .Isize -def I8.ofInt := @Scalar.ofInt .I8 -def I16.ofInt := @Scalar.ofInt .I16 -def I32.ofInt := @Scalar.ofInt .I32 -def I64.ofInt := @Scalar.ofInt .I64 -def I128.ofInt := @Scalar.ofInt .I128 -def Usize.ofInt := @Scalar.ofInt .Usize -def U8.ofInt := @Scalar.ofInt .U8 -def U16.ofInt := @Scalar.ofInt .U16 -def U32.ofInt := @Scalar.ofInt .U32 -def U64.ofInt := @Scalar.ofInt .U64 -def U128.ofInt := @Scalar.ofInt .U128 - --- Comparisons -instance {ty} : LT (Scalar ty) where - lt a b := LT.lt a.val b.val - -instance {ty} : LE (Scalar ty) where le a b := LE.le a.val b.val - -instance Scalar.decLt {ty} (a b : Scalar ty) : Decidable (LT.lt a b) := Int.decLt .. -instance Scalar.decLe {ty} (a b : Scalar ty) : Decidable (LE.le a b) := Int.decLe .. - -theorem Scalar.eq_of_val_eq {ty} : ∀ {i j : Scalar ty}, Eq i.val j.val → Eq i j - | ⟨_, _, _⟩, ⟨_, _, _⟩, rfl => rfl - -theorem Scalar.val_eq_of_eq {ty} {i j : Scalar ty} (h : Eq i j) : Eq i.val j.val := - h ▸ rfl - -theorem Scalar.ne_of_val_ne {ty} {i j : Scalar ty} (h : Not (Eq i.val j.val)) : Not (Eq i j) := - fun h' => absurd (val_eq_of_eq h') h - -instance (ty : ScalarTy) : DecidableEq (Scalar ty) := - fun i j => - match decEq i.val j.val with - | isTrue h => isTrue (Scalar.eq_of_val_eq h) - | isFalse h => isFalse (Scalar.ne_of_val_ne h) - -def Scalar.toInt {ty} (n : Scalar ty) : Int := n.val - --- Tactic to prove that integers are in bounds -syntax "intlit" : tactic - -macro_rules - | `(tactic| intlit) => `(tactic| apply Scalar.bound_suffices ; decide) - --- -- We now define a type class that subsumes the various machine integer types, so --- -- as to write a concise definition for scalar_cast, rather than exhaustively --- -- enumerating all of the possible pairs. We remark that Rust has sane semantics --- -- and fails if a cast operation would involve a truncation or modulo. - --- class MachineInteger (t: Type) where --- size: Nat --- val: t -> Fin size --- ofNatCore: (n:Nat) -> LT.lt n size -> t - --- set_option hygiene false in --- run_cmd --- for typeName in [`UInt8, `UInt16, `UInt32, `UInt64, `USize].map Lean.mkIdent do --- Lean.Elab.Command.elabCommand (← `( --- namespace $typeName --- instance: MachineInteger $typeName where --- size := size --- val := val --- ofNatCore := ofNatCore --- end $typeName --- )) - --- -- Aeneas only instantiates the destination type (`src` is implicit). We rely on --- -- Lean to infer `src`. - --- def scalar_cast { src: Type } (dst: Type) [ MachineInteger src ] [ MachineInteger dst ] (x: src): Result dst := --- if h: MachineInteger.val x < MachineInteger.size dst then --- .ret (MachineInteger.ofNatCore (MachineInteger.val x).val h) --- else --- .fail integerOverflow - -------------- --- VECTORS -- -------------- - -def Vec (α : Type u) := { l : List α // List.length l <= Usize.max } - -def vec_new (α : Type u): Vec α := ⟨ [], by sorry ⟩ - -def vec_len (α : Type u) (v : Vec α) : Usize := - let ⟨ v, l ⟩ := v - Usize.ofIntCore (List.length v) (by sorry) l - -def vec_push_fwd (α : Type u) (_ : Vec α) (_ : α) : Unit := () - -def vec_push_back (α : Type u) (v : Vec α) (x : α) : Result (Vec α) - := - if h : List.length v.val <= U32.max || List.length v.val <= Usize.max then - return ⟨ List.concat v.val x, by sorry ⟩ - else - fail maximumSizeExceeded - -def vec_insert_fwd (α : Type u) (v: Vec α) (i: Usize) (_: α): Result Unit := - if i.val < List.length v.val then - .ret () - else - .fail arrayOutOfBounds - -def vec_insert_back (α : Type u) (v: Vec α) (i: Usize) (x: α): Result (Vec α) := - if i.val < List.length v.val then - -- TODO: maybe we should redefine a list library which uses integers - -- (instead of natural numbers) - let i : Nat := - match i.val with - | .ofNat n => n - | .negSucc n => by sorry -- TODO: we can't get here - let isLt: i < USize.size := by sorry - let i : Fin USize.size := { val := i, isLt := isLt } - .ret ⟨ List.set v.val i.val x, by - have h: List.length v.val <= Usize.max := v.property - rewrite [ List.length_set v.val i.val x ] - assumption - ⟩ - else - .fail arrayOutOfBounds - -def vec_index_fwd (α : Type u) (v: Vec α) (i: Usize): Result α := - if i.val < List.length v.val then - let i : Nat := - match i.val with - | .ofNat n => n - | .negSucc n => by sorry -- TODO: we can't get here - let isLt: i < USize.size := by sorry - let i : Fin USize.size := { val := i, isLt := isLt } - let h: i < List.length v.val := by sorry - .ret (List.get v.val ⟨i.val, h⟩) - else - .fail arrayOutOfBounds - -def vec_index_back (α : Type u) (v: Vec α) (i: Usize) (_: α): Result Unit := - if i.val < List.length v.val then - .ret () - else - .fail arrayOutOfBounds - -def vec_index_mut_fwd (α : Type u) (v: Vec α) (i: Usize): Result α := - if i.val < List.length v.val then - let i : Nat := - match i.val with - | .ofNat n => n - | .negSucc n => by sorry -- TODO: we can't get here - let isLt: i < USize.size := by sorry - let i : Fin USize.size := { val := i, isLt := isLt } - let h: i < List.length v.val := by sorry - .ret (List.get v.val ⟨i.val, h⟩) - else - .fail arrayOutOfBounds - -def vec_index_mut_back (α : Type u) (v: Vec α) (i: Usize) (x: α): Result (Vec α) := - if i.val < List.length v.val then - let i : Nat := - match i.val with - | .ofNat n => n - | .negSucc n => by sorry -- TODO: we can't get here - let isLt: i < USize.size := by sorry - let i : Fin USize.size := { val := i, isLt := isLt } - .ret ⟨ List.set v.val i.val x, by - have h: List.length v.val <= Usize.max := v.property - rewrite [ List.length_set v.val i.val x ] - assumption - ⟩ - else - .fail arrayOutOfBounds - ----------- --- MISC -- ----------- - -def mem_replace_fwd (a : Type) (x : a) (_ : a) : a := - x - -def mem_replace_back (a : Type) (_ : a) (y : a) : a := - y - -/-- Aeneas-translated function -- useful to reduce non-recursive definitions. - Use with `simp [ aeneas ]` -/ -register_simp_attr aeneas diff --git a/tests/lean/misc-external/External.lean b/tests/lean/misc-external/External.lean deleted file mode 100644 index b95db309..00000000 --- a/tests/lean/misc-external/External.lean +++ /dev/null @@ -1 +0,0 @@ -import External.Funs diff --git a/tests/lean/misc-external/External/ExternalFuns.lean b/tests/lean/misc-external/External/ExternalFuns.lean deleted file mode 100644 index 6bd4f4a9..00000000 --- a/tests/lean/misc-external/External/ExternalFuns.lean +++ /dev/null @@ -1,5 +0,0 @@ -import Base.Primitives -import External.Types -import External.Opaque - -def opaque_defs : OpaqueDefs := sorry diff --git a/tests/lean/misc-external/External/Funs.lean b/tests/lean/misc-external/External/Funs.lean deleted file mode 100644 index eeb83989..00000000 --- a/tests/lean/misc-external/External/Funs.lean +++ /dev/null @@ -1,84 +0,0 @@ --- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS --- [external]: function definitions -import Base.Primitives -import External.Types -import External.ExternalFuns - -/- [external::swap] -/ -def swap_fwd - (T : Type) (x : T) (y : T) (st : State) : Result (State × Unit) := - do - let (st0, _) ← opaque_defs.core_mem_swap_fwd T x y st - let (st1, _) ← opaque_defs.core_mem_swap_back0 T x y st st0 - let (st2, _) ← opaque_defs.core_mem_swap_back1 T x y st st1 - Result.ret (st2, ()) - -/- [external::swap] -/ -def swap_back - (T : Type) (x : T) (y : T) (st : State) (st0 : State) : - Result (State × (T × T)) - := - do - let (st1, _) ← opaque_defs.core_mem_swap_fwd T x y st - let (st2, x0) ← opaque_defs.core_mem_swap_back0 T x y st st1 - let (_, y0) ← opaque_defs.core_mem_swap_back1 T x y st st2 - Result.ret (st0, (x0, y0)) - -/- [external::test_new_non_zero_u32] -/ -def test_new_non_zero_u32_fwd - (x : U32) (st : State) : Result (State × core_num_nonzero_non_zero_u32_t) := - do - let (st0, opt) ← opaque_defs.core_num_nonzero_non_zero_u32_new_fwd x st - opaque_defs.core_option_option_unwrap_fwd core_num_nonzero_non_zero_u32_t - opt st0 - -/- [external::test_vec] -/ -def test_vec_fwd : Result Unit := - do - let v := vec_new U32 - let _ ← vec_push_back U32 v (U32.ofInt 0 (by intlit)) - Result.ret () - -/- [external::custom_swap] -/ -def custom_swap_fwd - (T : Type) (x : T) (y : T) (st : State) : Result (State × T) := - do - let (st0, _) ← opaque_defs.core_mem_swap_fwd T x y st - let (st1, x0) ← opaque_defs.core_mem_swap_back0 T x y st st0 - let (st2, _) ← opaque_defs.core_mem_swap_back1 T x y st st1 - Result.ret (st2, x0) - -/- [external::custom_swap] -/ -def custom_swap_back - (T : Type) (x : T) (y : T) (st : State) (ret0 : T) (st0 : State) : - Result (State × (T × T)) - := - do - let (st1, _) ← opaque_defs.core_mem_swap_fwd T x y st - let (st2, _) ← opaque_defs.core_mem_swap_back0 T x y st st1 - let (_, y0) ← opaque_defs.core_mem_swap_back1 T x y st st2 - Result.ret (st0, (ret0, y0)) - -/- [external::test_custom_swap] -/ -def test_custom_swap_fwd - (x : U32) (y : U32) (st : State) : Result (State × Unit) := - do - let (st0, _) ← custom_swap_fwd U32 x y st - Result.ret (st0, ()) - -/- [external::test_custom_swap] -/ -def test_custom_swap_back - (x : U32) (y : U32) (st : State) (st0 : State) : - Result (State × (U32 × U32)) - := - custom_swap_back U32 x y st (U32.ofInt 1 (by intlit)) st0 - -/- [external::test_swap_non_zero] -/ -def test_swap_non_zero_fwd (x : U32) (st : State) : Result (State × U32) := - do - let (st0, _) ← swap_fwd U32 x (U32.ofInt 0 (by intlit)) st - let (st1, (x0, _)) ← swap_back U32 x (U32.ofInt 0 (by intlit)) st st0 - if h: x0 = (U32.ofInt 0 (by intlit)) - then Result.fail Error.panic - else Result.ret (st1, x0) - diff --git a/tests/lean/misc-external/External/Opaque.lean b/tests/lean/misc-external/External/Opaque.lean deleted file mode 100644 index d641912b..00000000 --- a/tests/lean/misc-external/External/Opaque.lean +++ /dev/null @@ -1,27 +0,0 @@ --- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS --- [external]: opaque function definitions -import Base.Primitives -import External.Types - -structure OpaqueDefs where - - /- [core::mem::swap] -/ - core_mem_swap_fwd (T : Type) : T -> T -> State -> Result (State × Unit) - - /- [core::mem::swap] -/ - core_mem_swap_back0 - (T : Type) : T -> T -> State -> State -> Result (State × T) - - /- [core::mem::swap] -/ - core_mem_swap_back1 - (T : Type) : T -> T -> State -> State -> Result (State × T) - - /- [core::num::nonzero::NonZeroU32::{14}::new] -/ - core_num_nonzero_non_zero_u32_new_fwd - : - U32 -> State -> Result (State × (Option core_num_nonzero_non_zero_u32_t)) - - /- [core::option::Option::{0}::unwrap] -/ - core_option_option_unwrap_fwd - (T : Type) : Option T -> State -> Result (State × T) - diff --git a/tests/lean/misc-external/External/Types.lean b/tests/lean/misc-external/External/Types.lean deleted file mode 100644 index ed1842be..00000000 --- a/tests/lean/misc-external/External/Types.lean +++ /dev/null @@ -1,10 +0,0 @@ --- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS --- [external]: type definitions -import Base.Primitives - -/- [core::num::nonzero::NonZeroU32] -/ -axiom core_num_nonzero_non_zero_u32_t : Type - -/- The state type used in the state-error monad -/ -axiom State : Type - diff --git a/tests/lean/misc-external/lake-manifest.json b/tests/lean/misc-external/lake-manifest.json deleted file mode 100644 index 57b071ca..00000000 --- a/tests/lean/misc-external/lake-manifest.json +++ /dev/null @@ -1,27 +0,0 @@ -{"version": 4, - "packagesDir": "./lake-packages", - "packages": - [{"git": - {"url": "https://github.com/leanprover-community/mathlib4.git", - "subDir?": null, - "rev": "4037792ead804d7bfa8868e2c4684d4223c15ece", - "name": "mathlib", - "inputRev?": null}}, - {"git": - {"url": "https://github.com/gebner/quote4", - "subDir?": null, - "rev": "2412c4fdf4a8b689f4467618e5e7b371ae5014aa", - "name": "Qq", - "inputRev?": "master"}}, - {"git": - {"url": "https://github.com/JLimperg/aesop", - "subDir?": null, - "rev": "7fe9ecd9339b0e1796e89d243b776849c305c690", - "name": "aesop", - "inputRev?": "master"}}, - {"git": - {"url": "https://github.com/leanprover/std4", - "subDir?": null, - "rev": "24897887905b3a1254b244369f5dd2cf6174b0ee", - "name": "std", - "inputRev?": "main"}}]} diff --git a/tests/lean/misc-external/lakefile.lean b/tests/lean/misc-external/lakefile.lean deleted file mode 100644 index 6cc4aae4..00000000 --- a/tests/lean/misc-external/lakefile.lean +++ /dev/null @@ -1,12 +0,0 @@ -import Lake -open Lake DSL - -require mathlib from git - "https://github.com/leanprover-community/mathlib4.git" - -package «external» {} - -lean_lib «Base» {} - -@[default_target] -lean_lib «External» {} diff --git a/tests/lean/misc-external/lean-toolchain b/tests/lean/misc-external/lean-toolchain deleted file mode 100644 index bbf57f10..00000000 --- a/tests/lean/misc-external/lean-toolchain +++ /dev/null @@ -1 +0,0 @@ -leanprover/lean4:nightly-2023-01-21 diff --git a/tests/lean/misc-loops/Base/Primitives.lean b/tests/lean/misc-loops/Base/Primitives.lean deleted file mode 100644 index 4a66a453..00000000 --- a/tests/lean/misc-loops/Base/Primitives.lean +++ /dev/null @@ -1,583 +0,0 @@ -import Lean -import Lean.Meta.Tactic.Simp -import Init.Data.List.Basic -import Mathlib.Tactic.RunCmd - --------------------- --- ASSERT COMMAND -- --------------------- - -open Lean Elab Command Term Meta - -syntax (name := assert) "#assert" term: command - -@[command_elab assert] -unsafe -def assertImpl : CommandElab := fun (_stx: Syntax) => do - runTermElabM (fun _ => do - let r ← evalTerm Bool (mkConst ``Bool) _stx[1] - if not r then - logInfo "Assertion failed for: " - logInfo _stx[1] - logError "Expression reduced to false" - pure ()) - -#eval 2 == 2 -#assert (2 == 2) - -------------- --- PRELUDE -- -------------- - --- Results & monadic combinators - -inductive Error where - | assertionFailure: Error - | integerOverflow: Error - | divisionByZero: Error - | arrayOutOfBounds: Error - | maximumSizeExceeded: Error - | panic: Error -deriving Repr, BEq - -open Error - -inductive Result (α : Type u) where - | ret (v: α): Result α - | fail (e: Error): Result α -deriving Repr, BEq - -open Result - -instance Result_Inhabited (α : Type u) : Inhabited (Result α) := - Inhabited.mk (fail panic) - -/- HELPERS -/ - -def ret? {α: Type} (r: Result α): Bool := - match r with - | Result.ret _ => true - | Result.fail _ => false - -def massert (b:Bool) : Result Unit := - if b then .ret () else fail assertionFailure - -def eval_global {α: Type} (x: Result α) (_: ret? x): α := - match x with - | Result.fail _ => by contradiction - | Result.ret x => x - -/- DO-DSL SUPPORT -/ - -def bind (x: Result α) (f: α -> Result β) : Result β := - match x with - | ret v => f v - | fail v => fail v - --- Allows using Result in do-blocks -instance : Bind Result where - bind := bind - --- Allows using return x in do-blocks -instance : Pure Result where - pure := fun x => ret x - -/- CUSTOM-DSL SUPPORT -/ - --- Let-binding the Result of a monadic operation is oftentimes not sufficient, --- because we may need a hypothesis for equational reasoning in the scope. We --- rely on subtype, and a custom let-binding operator, in effect recreating our --- own variant of the do-dsl - -def Result.attach {α: Type} (o : Result α): Result { x : α // o = ret x } := - match o with - | .ret x => .ret ⟨x, rfl⟩ - | .fail e => .fail e - -macro "let" e:term " ⟵ " f:term : doElem => - `(doElem| let ⟨$e, h⟩ ← Result.attach $f) - --- TODO: any way to factorize both definitions? -macro "let" e:term " <-- " f:term : doElem => - `(doElem| let ⟨$e, h⟩ ← Result.attach $f) - --- We call the hypothesis `h`, in effect making it unavailable to the user --- (because too much shadowing). But in practice, once can use the French single --- quote notation (input with f< and f>), where `‹ h ›` finds a suitable --- hypothesis in the context, this is equivalent to `have x: h := by assumption in x` -#eval do - let y <-- .ret (0: Nat) - let _: y = 0 := by cases ‹ ret 0 = ret y › ; decide - let r: { x: Nat // x = 0 } := ⟨ y, by assumption ⟩ - .ret r - ----------------------- --- MACHINE INTEGERS -- ----------------------- - --- We redefine our machine integers types. - --- For Isize/Usize, we reuse `getNumBits` from `USize`. You cannot reduce `getNumBits` --- using the simplifier, meaning that proofs do not depend on the compile-time value of --- USize.size. (Lean assumes 32 or 64-bit platforms, and Rust doesn't really support, at --- least officially, 16-bit microcontrollers, so this seems like a fine design decision --- for now.) - --- Note from Chris Bailey: "If there's more than one salient property of your --- definition then the subtyping strategy might get messy, and the property part --- of a subtype is less discoverable by the simplifier or tactics like --- library_search." So, we will not add refinements on the return values of the --- operations defined on Primitives, but will rather rely on custom lemmas to --- invert on possible return values of the primitive operations. - --- Machine integer constants, done via `ofNatCore`, which requires a proof that --- the `Nat` fits within the desired integer type. We provide a custom tactic. - -open System.Platform.getNumBits - --- TODO: is there a way of only importing System.Platform.getNumBits? --- -@[simp] def size_num_bits : Nat := (System.Platform.getNumBits ()).val - --- Remark: Lean seems to use < for the comparisons with the upper bounds by convention. --- We keep the F* convention for now. -@[simp] def Isize.min : Int := - (HPow.hPow 2 (size_num_bits - 1)) -@[simp] def Isize.max : Int := (HPow.hPow 2 (size_num_bits - 1)) - 1 -@[simp] def I8.min : Int := - (HPow.hPow 2 7) -@[simp] def I8.max : Int := HPow.hPow 2 7 - 1 -@[simp] def I16.min : Int := - (HPow.hPow 2 15) -@[simp] def I16.max : Int := HPow.hPow 2 15 - 1 -@[simp] def I32.min : Int := -(HPow.hPow 2 31) -@[simp] def I32.max : Int := HPow.hPow 2 31 - 1 -@[simp] def I64.min : Int := -(HPow.hPow 2 63) -@[simp] def I64.max : Int := HPow.hPow 2 63 - 1 -@[simp] def I128.min : Int := -(HPow.hPow 2 127) -@[simp] def I128.max : Int := HPow.hPow 2 127 - 1 -@[simp] def Usize.min : Int := 0 -@[simp] def Usize.max : Int := HPow.hPow 2 size_num_bits - 1 -@[simp] def U8.min : Int := 0 -@[simp] def U8.max : Int := HPow.hPow 2 8 - 1 -@[simp] def U16.min : Int := 0 -@[simp] def U16.max : Int := HPow.hPow 2 16 - 1 -@[simp] def U32.min : Int := 0 -@[simp] def U32.max : Int := HPow.hPow 2 32 - 1 -@[simp] def U64.min : Int := 0 -@[simp] def U64.max : Int := HPow.hPow 2 64 - 1 -@[simp] def U128.min : Int := 0 -@[simp] def U128.max : Int := HPow.hPow 2 128 - 1 - -#assert (I8.min == -128) -#assert (I8.max == 127) -#assert (I16.min == -32768) -#assert (I16.max == 32767) -#assert (I32.min == -2147483648) -#assert (I32.max == 2147483647) -#assert (I64.min == -9223372036854775808) -#assert (I64.max == 9223372036854775807) -#assert (I128.min == -170141183460469231731687303715884105728) -#assert (I128.max == 170141183460469231731687303715884105727) -#assert (U8.min == 0) -#assert (U8.max == 255) -#assert (U16.min == 0) -#assert (U16.max == 65535) -#assert (U32.min == 0) -#assert (U32.max == 4294967295) -#assert (U64.min == 0) -#assert (U64.max == 18446744073709551615) -#assert (U128.min == 0) -#assert (U128.max == 340282366920938463463374607431768211455) - -inductive ScalarTy := -| Isize -| I8 -| I16 -| I32 -| I64 -| I128 -| Usize -| U8 -| U16 -| U32 -| U64 -| U128 - -def Scalar.min (ty : ScalarTy) : Int := - match ty with - | .Isize => Isize.min - | .I8 => I8.min - | .I16 => I16.min - | .I32 => I32.min - | .I64 => I64.min - | .I128 => I128.min - | .Usize => Usize.min - | .U8 => U8.min - | .U16 => U16.min - | .U32 => U32.min - | .U64 => U64.min - | .U128 => U128.min - -def Scalar.max (ty : ScalarTy) : Int := - match ty with - | .Isize => Isize.max - | .I8 => I8.max - | .I16 => I16.max - | .I32 => I32.max - | .I64 => I64.max - | .I128 => I128.max - | .Usize => Usize.max - | .U8 => U8.max - | .U16 => U16.max - | .U32 => U32.max - | .U64 => U64.max - | .U128 => U128.max - --- "Conservative" bounds --- We use those because we can't compare to the isize bounds (which can't --- reduce at compile-time). Whenever we perform an arithmetic operation like --- addition we need to check that the result is in bounds: we first compare --- to the conservative bounds, which reduce, then compare to the real bounds. --- This is useful for the various #asserts that we want to reduce at --- type-checking time. -def Scalar.cMin (ty : ScalarTy) : Int := - match ty with - | .Isize => I32.min - | _ => Scalar.min ty - -def Scalar.cMax (ty : ScalarTy) : Int := - match ty with - | .Isize => I32.max - | .Usize => U32.max - | _ => Scalar.max ty - -theorem Scalar.cMin_bound ty : Scalar.min ty <= Scalar.cMin ty := by sorry -theorem Scalar.cMax_bound ty : Scalar.min ty <= Scalar.cMin ty := by sorry - -structure Scalar (ty : ScalarTy) where - val : Int - hmin : Scalar.min ty <= val - hmax : val <= Scalar.max ty - -theorem Scalar.bound_suffices (ty : ScalarTy) (x : Int) : - Scalar.cMin ty <= x && x <= Scalar.cMax ty -> - (decide (Scalar.min ty ≤ x) && decide (x ≤ Scalar.max ty)) = true - := by sorry - -def Scalar.ofIntCore {ty : ScalarTy} (x : Int) - (hmin : Scalar.min ty <= x) (hmax : x <= Scalar.max ty) : Scalar ty := - { val := x, hmin := hmin, hmax := hmax } - -def Scalar.ofInt {ty : ScalarTy} (x : Int) - (h : Scalar.min ty <= x && x <= Scalar.max ty) : Scalar ty := - let hmin: Scalar.min ty <= x := by sorry - let hmax: x <= Scalar.max ty := by sorry - Scalar.ofIntCore x hmin hmax - --- Further thoughts: look at what has been done here: --- https://github.com/leanprover-community/mathlib4/blob/master/Mathlib/Data/Fin/Basic.lean --- and --- https://github.com/leanprover-community/mathlib4/blob/master/Mathlib/Data/UInt.lean --- which both contain a fair amount of reasoning already! -def Scalar.tryMk (ty : ScalarTy) (x : Int) : Result (Scalar ty) := - -- TODO: write this with only one if then else - if hmin_cons: Scalar.cMin ty <= x || Scalar.min ty <= x then - if hmax_cons: x <= Scalar.cMax ty || x <= Scalar.max ty then - let hmin: Scalar.min ty <= x := by sorry - let hmax: x <= Scalar.max ty := by sorry - return Scalar.ofIntCore x hmin hmax - else fail integerOverflow - else fail integerOverflow - -def Scalar.neg {ty : ScalarTy} (x : Scalar ty) : Result (Scalar ty) := Scalar.tryMk ty (- x.val) - -def Scalar.div {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - if y.val != 0 then Scalar.tryMk ty (x.val / y.val) else fail divisionByZero - --- Checking that the % operation in Lean computes the same as the remainder operation in Rust -#assert 1 % 2 = (1:Int) -#assert (-1) % 2 = -1 -#assert 1 % (-2) = 1 -#assert (-1) % (-2) = -1 - -def Scalar.rem {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - if y.val != 0 then Scalar.tryMk ty (x.val % y.val) else fail divisionByZero - -def Scalar.add {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - Scalar.tryMk ty (x.val + y.val) - -def Scalar.sub {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - Scalar.tryMk ty (x.val - y.val) - -def Scalar.mul {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - Scalar.tryMk ty (x.val * y.val) - --- TODO: instances of +, -, * etc. for scalars - --- Cast an integer from a [src_ty] to a [tgt_ty] --- TODO: check the semantics of casts in Rust -def Scalar.cast {src_ty : ScalarTy} (tgt_ty : ScalarTy) (x : Scalar src_ty) : Result (Scalar tgt_ty) := - Scalar.tryMk tgt_ty x.val - --- The scalar types --- We declare the definitions as reducible so that Lean can unfold them (useful --- for type class resolution for instance). -@[reducible] def Isize := Scalar .Isize -@[reducible] def I8 := Scalar .I8 -@[reducible] def I16 := Scalar .I16 -@[reducible] def I32 := Scalar .I32 -@[reducible] def I64 := Scalar .I64 -@[reducible] def I128 := Scalar .I128 -@[reducible] def Usize := Scalar .Usize -@[reducible] def U8 := Scalar .U8 -@[reducible] def U16 := Scalar .U16 -@[reducible] def U32 := Scalar .U32 -@[reducible] def U64 := Scalar .U64 -@[reducible] def U128 := Scalar .U128 - --- TODO: below: not sure this is the best way. --- Should we rather overload operations like +, -, etc.? --- Also, it is possible to automate the generation of those definitions --- with macros (but would it be a good idea? It would be less easy to --- read the file, which is not supposed to change a lot) - --- Negation - -/-- -Remark: there is no heterogeneous negation in the Lean prelude: we thus introduce -one here. - -The notation typeclass for heterogeneous addition. -This enables the notation `- a : β` where `a : α`. --/ -class HNeg (α : Type u) (β : outParam (Type v)) where - /-- `- a` computes the negation of `a`. - The meaning of this notation is type-dependent. -/ - hNeg : α → β - -prefix:75 "-" => HNeg.hNeg - -instance : HNeg Isize (Result Isize) where hNeg x := Scalar.neg x -instance : HNeg I8 (Result I8) where hNeg x := Scalar.neg x -instance : HNeg I16 (Result I16) where hNeg x := Scalar.neg x -instance : HNeg I32 (Result I32) where hNeg x := Scalar.neg x -instance : HNeg I64 (Result I64) where hNeg x := Scalar.neg x -instance : HNeg I128 (Result I128) where hNeg x := Scalar.neg x - --- Addition -instance {ty} : HAdd (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hAdd x y := Scalar.add x y - --- Substraction -instance {ty} : HSub (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hSub x y := Scalar.sub x y - --- Multiplication -instance {ty} : HMul (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hMul x y := Scalar.mul x y - --- Division -instance {ty} : HDiv (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hDiv x y := Scalar.div x y - --- Remainder -instance {ty} : HMod (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hMod x y := Scalar.rem x y - --- ofIntCore --- TODO: typeclass? -def Isize.ofIntCore := @Scalar.ofIntCore .Isize -def I8.ofIntCore := @Scalar.ofIntCore .I8 -def I16.ofIntCore := @Scalar.ofIntCore .I16 -def I32.ofIntCore := @Scalar.ofIntCore .I32 -def I64.ofIntCore := @Scalar.ofIntCore .I64 -def I128.ofIntCore := @Scalar.ofIntCore .I128 -def Usize.ofIntCore := @Scalar.ofIntCore .Usize -def U8.ofIntCore := @Scalar.ofIntCore .U8 -def U16.ofIntCore := @Scalar.ofIntCore .U16 -def U32.ofIntCore := @Scalar.ofIntCore .U32 -def U64.ofIntCore := @Scalar.ofIntCore .U64 -def U128.ofIntCore := @Scalar.ofIntCore .U128 - --- ofInt --- TODO: typeclass? -def Isize.ofInt := @Scalar.ofInt .Isize -def I8.ofInt := @Scalar.ofInt .I8 -def I16.ofInt := @Scalar.ofInt .I16 -def I32.ofInt := @Scalar.ofInt .I32 -def I64.ofInt := @Scalar.ofInt .I64 -def I128.ofInt := @Scalar.ofInt .I128 -def Usize.ofInt := @Scalar.ofInt .Usize -def U8.ofInt := @Scalar.ofInt .U8 -def U16.ofInt := @Scalar.ofInt .U16 -def U32.ofInt := @Scalar.ofInt .U32 -def U64.ofInt := @Scalar.ofInt .U64 -def U128.ofInt := @Scalar.ofInt .U128 - --- Comparisons -instance {ty} : LT (Scalar ty) where - lt a b := LT.lt a.val b.val - -instance {ty} : LE (Scalar ty) where le a b := LE.le a.val b.val - -instance Scalar.decLt {ty} (a b : Scalar ty) : Decidable (LT.lt a b) := Int.decLt .. -instance Scalar.decLe {ty} (a b : Scalar ty) : Decidable (LE.le a b) := Int.decLe .. - -theorem Scalar.eq_of_val_eq {ty} : ∀ {i j : Scalar ty}, Eq i.val j.val → Eq i j - | ⟨_, _, _⟩, ⟨_, _, _⟩, rfl => rfl - -theorem Scalar.val_eq_of_eq {ty} {i j : Scalar ty} (h : Eq i j) : Eq i.val j.val := - h ▸ rfl - -theorem Scalar.ne_of_val_ne {ty} {i j : Scalar ty} (h : Not (Eq i.val j.val)) : Not (Eq i j) := - fun h' => absurd (val_eq_of_eq h') h - -instance (ty : ScalarTy) : DecidableEq (Scalar ty) := - fun i j => - match decEq i.val j.val with - | isTrue h => isTrue (Scalar.eq_of_val_eq h) - | isFalse h => isFalse (Scalar.ne_of_val_ne h) - -def Scalar.toInt {ty} (n : Scalar ty) : Int := n.val - --- Tactic to prove that integers are in bounds -syntax "intlit" : tactic - -macro_rules - | `(tactic| intlit) => `(tactic| apply Scalar.bound_suffices ; decide) - --- -- We now define a type class that subsumes the various machine integer types, so --- -- as to write a concise definition for scalar_cast, rather than exhaustively --- -- enumerating all of the possible pairs. We remark that Rust has sane semantics --- -- and fails if a cast operation would involve a truncation or modulo. - --- class MachineInteger (t: Type) where --- size: Nat --- val: t -> Fin size --- ofNatCore: (n:Nat) -> LT.lt n size -> t - --- set_option hygiene false in --- run_cmd --- for typeName in [`UInt8, `UInt16, `UInt32, `UInt64, `USize].map Lean.mkIdent do --- Lean.Elab.Command.elabCommand (← `( --- namespace $typeName --- instance: MachineInteger $typeName where --- size := size --- val := val --- ofNatCore := ofNatCore --- end $typeName --- )) - --- -- Aeneas only instantiates the destination type (`src` is implicit). We rely on --- -- Lean to infer `src`. - --- def scalar_cast { src: Type } (dst: Type) [ MachineInteger src ] [ MachineInteger dst ] (x: src): Result dst := --- if h: MachineInteger.val x < MachineInteger.size dst then --- .ret (MachineInteger.ofNatCore (MachineInteger.val x).val h) --- else --- .fail integerOverflow - -------------- --- VECTORS -- -------------- - -def Vec (α : Type u) := { l : List α // List.length l <= Usize.max } - -def vec_new (α : Type u): Vec α := ⟨ [], by sorry ⟩ - -def vec_len (α : Type u) (v : Vec α) : Usize := - let ⟨ v, l ⟩ := v - Usize.ofIntCore (List.length v) (by sorry) l - -def vec_push_fwd (α : Type u) (_ : Vec α) (_ : α) : Unit := () - -def vec_push_back (α : Type u) (v : Vec α) (x : α) : Result (Vec α) - := - if h : List.length v.val <= U32.max || List.length v.val <= Usize.max then - return ⟨ List.concat v.val x, by sorry ⟩ - else - fail maximumSizeExceeded - -def vec_insert_fwd (α : Type u) (v: Vec α) (i: Usize) (_: α): Result Unit := - if i.val < List.length v.val then - .ret () - else - .fail arrayOutOfBounds - -def vec_insert_back (α : Type u) (v: Vec α) (i: Usize) (x: α): Result (Vec α) := - if i.val < List.length v.val then - -- TODO: maybe we should redefine a list library which uses integers - -- (instead of natural numbers) - let i : Nat := - match i.val with - | .ofNat n => n - | .negSucc n => by sorry -- TODO: we can't get here - let isLt: i < USize.size := by sorry - let i : Fin USize.size := { val := i, isLt := isLt } - .ret ⟨ List.set v.val i.val x, by - have h: List.length v.val <= Usize.max := v.property - rewrite [ List.length_set v.val i.val x ] - assumption - ⟩ - else - .fail arrayOutOfBounds - -def vec_index_fwd (α : Type u) (v: Vec α) (i: Usize): Result α := - if i.val < List.length v.val then - let i : Nat := - match i.val with - | .ofNat n => n - | .negSucc n => by sorry -- TODO: we can't get here - let isLt: i < USize.size := by sorry - let i : Fin USize.size := { val := i, isLt := isLt } - let h: i < List.length v.val := by sorry - .ret (List.get v.val ⟨i.val, h⟩) - else - .fail arrayOutOfBounds - -def vec_index_back (α : Type u) (v: Vec α) (i: Usize) (_: α): Result Unit := - if i.val < List.length v.val then - .ret () - else - .fail arrayOutOfBounds - -def vec_index_mut_fwd (α : Type u) (v: Vec α) (i: Usize): Result α := - if i.val < List.length v.val then - let i : Nat := - match i.val with - | .ofNat n => n - | .negSucc n => by sorry -- TODO: we can't get here - let isLt: i < USize.size := by sorry - let i : Fin USize.size := { val := i, isLt := isLt } - let h: i < List.length v.val := by sorry - .ret (List.get v.val ⟨i.val, h⟩) - else - .fail arrayOutOfBounds - -def vec_index_mut_back (α : Type u) (v: Vec α) (i: Usize) (x: α): Result (Vec α) := - if i.val < List.length v.val then - let i : Nat := - match i.val with - | .ofNat n => n - | .negSucc n => by sorry -- TODO: we can't get here - let isLt: i < USize.size := by sorry - let i : Fin USize.size := { val := i, isLt := isLt } - .ret ⟨ List.set v.val i.val x, by - have h: List.length v.val <= Usize.max := v.property - rewrite [ List.length_set v.val i.val x ] - assumption - ⟩ - else - .fail arrayOutOfBounds - ----------- --- MISC -- ----------- - -def mem_replace_fwd (a : Type) (x : a) (_ : a) : a := - x - -def mem_replace_back (a : Type) (_ : a) (y : a) : a := - y - -/-- Aeneas-translated function -- useful to reduce non-recursive definitions. - Use with `simp [ aeneas ]` -/ -register_simp_attr aeneas diff --git a/tests/lean/misc-loops/Loops.lean b/tests/lean/misc-loops/Loops.lean deleted file mode 100644 index 60c73776..00000000 --- a/tests/lean/misc-loops/Loops.lean +++ /dev/null @@ -1 +0,0 @@ -import Loops.Funs diff --git a/tests/lean/misc-loops/Loops/Clauses/Clauses.lean b/tests/lean/misc-loops/Loops/Clauses/Clauses.lean deleted file mode 100644 index 89a7ce34..00000000 --- a/tests/lean/misc-loops/Loops/Clauses/Clauses.lean +++ /dev/null @@ -1,205 +0,0 @@ --- [loops]: decreases clauses -import Base.Primitives -import Loops.Types - -/- [loops::sum]: termination measure -/ -@[simp] -def sum_loop_terminates (max : U32) (i : U32) (s : U32) := (max, i, s) - -syntax "sum_loop_decreases" term+ : tactic - -macro_rules -| `(tactic| sum_loop_decreases $max $i $s) =>`(tactic| sorry) - -/- [loops::sum_with_mut_borrows]: termination measure -/ -@[simp] -def sum_with_mut_borrows_loop_terminates (max : U32) (mi : U32) (ms : U32) := - (max, mi, ms) - -syntax "sum_with_mut_borrows_loop_decreases" term+ : tactic - -macro_rules -| `(tactic| sum_with_mut_borrows_loop_decreases $max $mi $ms) =>`(tactic| sorry) - -/- [loops::sum_with_shared_borrows]: termination measure -/ -@[simp] -def sum_with_shared_borrows_loop_terminates (max : U32) (i : U32) (s : U32) := - (max, i, s) - -syntax "sum_with_shared_borrows_loop_decreases" term+ : tactic - -macro_rules -| `(tactic| sum_with_shared_borrows_loop_decreases $max $i $s) =>`(tactic| sorry) - -/- [loops::clear]: termination measure -/ -@[simp] def clear_loop_terminates (v : Vec U32) (i : Usize) := (v, i) - -syntax "clear_loop_decreases" term+ : tactic - -macro_rules -| `(tactic| clear_loop_decreases $v $i) =>`(tactic| sorry) - -/- [loops::list_mem]: termination measure -/ -@[simp] -def list_mem_loop_terminates (x : U32) (ls : list_t U32) := (x, ls) - -syntax "list_mem_loop_decreases" term+ : tactic - -macro_rules -| `(tactic| list_mem_loop_decreases $x $ls) =>`(tactic| sorry) - -/- [loops::list_nth_mut_loop]: termination measure -/ -@[simp] -def list_nth_mut_loop_loop_terminates (T : Type) (ls : list_t T) (i : U32) := - (ls, i) - -syntax "list_nth_mut_loop_loop_decreases" term+ : tactic - -macro_rules -| `(tactic| list_nth_mut_loop_loop_decreases $ls $i) =>`(tactic| sorry) - -/- [loops::list_nth_shared_loop]: termination measure -/ -@[simp] -def list_nth_shared_loop_loop_terminates (T : Type) (ls : list_t T) (i : U32) := - (ls, i) - -syntax "list_nth_shared_loop_loop_decreases" term+ : tactic - -macro_rules -| `(tactic| list_nth_shared_loop_loop_decreases $ls $i) =>`(tactic| sorry) - -/- [loops::get_elem_mut]: termination measure -/ -@[simp] -def get_elem_mut_loop_terminates (x : Usize) (ls : list_t Usize) := (x, ls) - -syntax "get_elem_mut_loop_decreases" term+ : tactic - -macro_rules -| `(tactic| get_elem_mut_loop_decreases $x $ls) =>`(tactic| sorry) - -/- [loops::get_elem_shared]: termination measure -/ -@[simp] -def get_elem_shared_loop_terminates (x : Usize) (ls : list_t Usize) := (x, ls) - -syntax "get_elem_shared_loop_decreases" term+ : tactic - -macro_rules -| `(tactic| get_elem_shared_loop_decreases $x $ls) =>`(tactic| sorry) - -/- [loops::list_nth_mut_loop_with_id]: termination measure -/ -@[simp] -def list_nth_mut_loop_with_id_loop_terminates (T : Type) (i : U32) - (ls : list_t T) := - (i, ls) - -syntax "list_nth_mut_loop_with_id_loop_decreases" term+ : tactic - -macro_rules -| `(tactic| list_nth_mut_loop_with_id_loop_decreases $i $ls) =>`(tactic| sorry) - -/- [loops::list_nth_shared_loop_with_id]: termination measure -/ -@[simp] -def list_nth_shared_loop_with_id_loop_terminates (T : Type) (i : U32) - (ls : list_t T) := - (i, ls) - -syntax "list_nth_shared_loop_with_id_loop_decreases" term+ : tactic - -macro_rules -| `(tactic| list_nth_shared_loop_with_id_loop_decreases $i $ls) =>`(tactic| sorry) - -/- [loops::list_nth_mut_loop_pair]: termination measure -/ -@[simp] -def list_nth_mut_loop_pair_loop_terminates (T : Type) (ls0 : list_t T) - (ls1 : list_t T) (i : U32) := - (ls0, ls1, i) - -syntax "list_nth_mut_loop_pair_loop_decreases" term+ : tactic - -macro_rules -| `(tactic| list_nth_mut_loop_pair_loop_decreases $ls0 $ls1 $i) =>`(tactic| sorry) - -/- [loops::list_nth_shared_loop_pair]: termination measure -/ -@[simp] -def list_nth_shared_loop_pair_loop_terminates (T : Type) (ls0 : list_t T) - (ls1 : list_t T) (i : U32) := - (ls0, ls1, i) - -syntax "list_nth_shared_loop_pair_loop_decreases" term+ : tactic - -macro_rules -| `(tactic| list_nth_shared_loop_pair_loop_decreases $ls0 $ls1 $i) => - `(tactic| sorry) - -/- [loops::list_nth_mut_loop_pair_merge]: termination measure -/ -@[simp] -def list_nth_mut_loop_pair_merge_loop_terminates (T : Type) (ls0 : list_t T) - (ls1 : list_t T) (i : U32) := - (ls0, ls1, i) - -syntax "list_nth_mut_loop_pair_merge_loop_decreases" term+ : tactic - -macro_rules -| `(tactic| list_nth_mut_loop_pair_merge_loop_decreases $ls0 $ls1 $i) => - `(tactic| sorry) - -/- [loops::list_nth_shared_loop_pair_merge]: termination measure -/ -@[simp] -def list_nth_shared_loop_pair_merge_loop_terminates (T : Type) (ls0 : list_t T) - (ls1 : list_t T) (i : U32) := - (ls0, ls1, i) - -syntax "list_nth_shared_loop_pair_merge_loop_decreases" term+ : tactic - -macro_rules -| `(tactic| list_nth_shared_loop_pair_merge_loop_decreases $ls0 $ls1 $i) => - `(tactic| sorry) - -/- [loops::list_nth_mut_shared_loop_pair]: termination measure -/ -@[simp] -def list_nth_mut_shared_loop_pair_loop_terminates (T : Type) (ls0 : list_t T) - (ls1 : list_t T) (i : U32) := - (ls0, ls1, i) - -syntax "list_nth_mut_shared_loop_pair_loop_decreases" term+ : tactic - -macro_rules -| `(tactic| list_nth_mut_shared_loop_pair_loop_decreases $ls0 $ls1 $i) => - `(tactic| sorry) - -/- [loops::list_nth_mut_shared_loop_pair_merge]: termination measure -/ -@[simp] -def list_nth_mut_shared_loop_pair_merge_loop_terminates (T : Type) - (ls0 : list_t T) (ls1 : list_t T) (i : U32) := - (ls0, ls1, i) - -syntax "list_nth_mut_shared_loop_pair_merge_loop_decreases" term+ : tactic - -macro_rules -| `(tactic| list_nth_mut_shared_loop_pair_merge_loop_decreases $ls0 $ls1 $i) => - `(tactic| sorry) - -/- [loops::list_nth_shared_mut_loop_pair]: termination measure -/ -@[simp] -def list_nth_shared_mut_loop_pair_loop_terminates (T : Type) (ls0 : list_t T) - (ls1 : list_t T) (i : U32) := - (ls0, ls1, i) - -syntax "list_nth_shared_mut_loop_pair_loop_decreases" term+ : tactic - -macro_rules -| `(tactic| list_nth_shared_mut_loop_pair_loop_decreases $ls0 $ls1 $i) => - `(tactic| sorry) - -/- [loops::list_nth_shared_mut_loop_pair_merge]: termination measure -/ -@[simp] -def list_nth_shared_mut_loop_pair_merge_loop_terminates (T : Type) - (ls0 : list_t T) (ls1 : list_t T) (i : U32) := - (ls0, ls1, i) - -syntax "list_nth_shared_mut_loop_pair_merge_loop_decreases" term+ : tactic - -macro_rules -| `(tactic| list_nth_shared_mut_loop_pair_merge_loop_decreases $ls0 $ls1 $i) => - `(tactic| sorry) - diff --git a/tests/lean/misc-loops/Loops/Clauses/Template.lean b/tests/lean/misc-loops/Loops/Clauses/Template.lean deleted file mode 100644 index 2e28a6c0..00000000 --- a/tests/lean/misc-loops/Loops/Clauses/Template.lean +++ /dev/null @@ -1,205 +0,0 @@ --- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS --- [loops]: templates for the decreases clauses -import Base.Primitives -import Loops.Types - -/- [loops::sum]: termination measure -/ -@[simp] def sum_loop_terminates (max : U32) (i : U32) (s : U32) := (max, i, s) - -/- [loops::sum]: decreases_by tactic -/ -syntax "sum_loop_decreases" term+ : tactic -macro_rules -| `(tactic| sum_loop_decreases $max $i $s) =>`(tactic| sorry) - -/- [loops::sum_with_mut_borrows]: termination measure -/ -@[simp] -def sum_with_mut_borrows_loop_terminates (max : U32) (mi : U32) (ms : U32) := - (max, mi, ms) - -/- [loops::sum_with_mut_borrows]: decreases_by tactic -/ -syntax "sum_with_mut_borrows_loop_decreases" term+ : tactic -macro_rules -| `(tactic| sum_with_mut_borrows_loop_decreases $max $mi $ms) =>`(tactic| sorry) - -/- [loops::sum_with_shared_borrows]: termination measure -/ -@[simp] -def sum_with_shared_borrows_loop_terminates (max : U32) (i : U32) (s : U32) := - (max, i, s) - -/- [loops::sum_with_shared_borrows]: decreases_by tactic -/ -syntax "sum_with_shared_borrows_loop_decreases" term+ : tactic -macro_rules -| `(tactic| sum_with_shared_borrows_loop_decreases $max $i $s) =>`(tactic| sorry) - -/- [loops::clear]: termination measure -/ -@[simp] def clear_loop_terminates (v : Vec U32) (i : Usize) := (v, i) - -/- [loops::clear]: decreases_by tactic -/ -syntax "clear_loop_decreases" term+ : tactic -macro_rules -| `(tactic| clear_loop_decreases $v $i) =>`(tactic| sorry) - -/- [loops::list_mem]: termination measure -/ -@[simp] def list_mem_loop_terminates (x : U32) (ls : list_t U32) := (x, ls) - -/- [loops::list_mem]: decreases_by tactic -/ -syntax "list_mem_loop_decreases" term+ : tactic -macro_rules -| `(tactic| list_mem_loop_decreases $x $ls) =>`(tactic| sorry) - -/- [loops::list_nth_mut_loop]: termination measure -/ -@[simp] -def list_nth_mut_loop_loop_terminates (T : Type) (ls : list_t T) (i : U32) := - (ls, i) - -/- [loops::list_nth_mut_loop]: decreases_by tactic -/ -syntax "list_nth_mut_loop_loop_decreases" term+ : tactic -macro_rules -| `(tactic| list_nth_mut_loop_loop_decreases $ls $i) =>`(tactic| sorry) - -/- [loops::list_nth_shared_loop]: termination measure -/ -@[simp] -def list_nth_shared_loop_loop_terminates (T : Type) (ls : list_t T) (i : U32) - := - (ls, i) - -/- [loops::list_nth_shared_loop]: decreases_by tactic -/ -syntax "list_nth_shared_loop_loop_decreases" term+ : tactic -macro_rules -| `(tactic| list_nth_shared_loop_loop_decreases $ls $i) =>`(tactic| sorry) - -/- [loops::get_elem_mut]: termination measure -/ -@[simp] -def get_elem_mut_loop_terminates (x : Usize) (ls : list_t Usize) := (x, ls) - -/- [loops::get_elem_mut]: decreases_by tactic -/ -syntax "get_elem_mut_loop_decreases" term+ : tactic -macro_rules -| `(tactic| get_elem_mut_loop_decreases $x $ls) =>`(tactic| sorry) - -/- [loops::get_elem_shared]: termination measure -/ -@[simp] -def get_elem_shared_loop_terminates (x : Usize) (ls : list_t Usize) := (x, ls) - -/- [loops::get_elem_shared]: decreases_by tactic -/ -syntax "get_elem_shared_loop_decreases" term+ : tactic -macro_rules -| `(tactic| get_elem_shared_loop_decreases $x $ls) =>`(tactic| sorry) - -/- [loops::list_nth_mut_loop_with_id]: termination measure -/ -@[simp] -def list_nth_mut_loop_with_id_loop_terminates (T : Type) (i : U32) - (ls : list_t T) := - (i, ls) - -/- [loops::list_nth_mut_loop_with_id]: decreases_by tactic -/ -syntax "list_nth_mut_loop_with_id_loop_decreases" term+ : tactic -macro_rules -| `(tactic| list_nth_mut_loop_with_id_loop_decreases $i $ls) =>`(tactic| sorry) - -/- [loops::list_nth_shared_loop_with_id]: termination measure -/ -@[simp] -def list_nth_shared_loop_with_id_loop_terminates (T : Type) (i : U32) - (ls : list_t T) := - (i, ls) - -/- [loops::list_nth_shared_loop_with_id]: decreases_by tactic -/ -syntax "list_nth_shared_loop_with_id_loop_decreases" term+ : tactic -macro_rules -| `(tactic| list_nth_shared_loop_with_id_loop_decreases $i $ls) =>`(tactic| sorry) - -/- [loops::list_nth_mut_loop_pair]: termination measure -/ -@[simp] -def list_nth_mut_loop_pair_loop_terminates (T : Type) (ls0 : list_t T) - (ls1 : list_t T) (i : U32) := - (ls0, ls1, i) - -/- [loops::list_nth_mut_loop_pair]: decreases_by tactic -/ -syntax "list_nth_mut_loop_pair_loop_decreases" term+ : tactic -macro_rules -| `(tactic| list_nth_mut_loop_pair_loop_decreases $ls0 $ls1 $i) =>`(tactic| sorry) - -/- [loops::list_nth_shared_loop_pair]: termination measure -/ -@[simp] -def list_nth_shared_loop_pair_loop_terminates (T : Type) (ls0 : list_t T) - (ls1 : list_t T) (i : U32) := - (ls0, ls1, i) - -/- [loops::list_nth_shared_loop_pair]: decreases_by tactic -/ -syntax "list_nth_shared_loop_pair_loop_decreases" term+ : tactic -macro_rules -| `(tactic| list_nth_shared_loop_pair_loop_decreases $ls0 $ls1 $i) => - `(tactic| sorry) - -/- [loops::list_nth_mut_loop_pair_merge]: termination measure -/ -@[simp] -def list_nth_mut_loop_pair_merge_loop_terminates (T : Type) (ls0 : list_t T) - (ls1 : list_t T) (i : U32) := - (ls0, ls1, i) - -/- [loops::list_nth_mut_loop_pair_merge]: decreases_by tactic -/ -syntax "list_nth_mut_loop_pair_merge_loop_decreases" term+ : tactic -macro_rules -| `(tactic| list_nth_mut_loop_pair_merge_loop_decreases $ls0 $ls1 $i) => - `(tactic| sorry) - -/- [loops::list_nth_shared_loop_pair_merge]: termination measure -/ -@[simp] -def list_nth_shared_loop_pair_merge_loop_terminates (T : Type) (ls0 : list_t T) - (ls1 : list_t T) (i : U32) := - (ls0, ls1, i) - -/- [loops::list_nth_shared_loop_pair_merge]: decreases_by tactic -/ -syntax "list_nth_shared_loop_pair_merge_loop_decreases" term+ : tactic -macro_rules -| `(tactic| list_nth_shared_loop_pair_merge_loop_decreases $ls0 $ls1 $i) => - `(tactic| sorry) - -/- [loops::list_nth_mut_shared_loop_pair]: termination measure -/ -@[simp] -def list_nth_mut_shared_loop_pair_loop_terminates (T : Type) (ls0 : list_t T) - (ls1 : list_t T) (i : U32) := - (ls0, ls1, i) - -/- [loops::list_nth_mut_shared_loop_pair]: decreases_by tactic -/ -syntax "list_nth_mut_shared_loop_pair_loop_decreases" term+ : tactic -macro_rules -| `(tactic| list_nth_mut_shared_loop_pair_loop_decreases $ls0 $ls1 $i) => - `(tactic| sorry) - -/- [loops::list_nth_mut_shared_loop_pair_merge]: termination measure -/ -@[simp] -def list_nth_mut_shared_loop_pair_merge_loop_terminates (T : Type) - (ls0 : list_t T) (ls1 : list_t T) (i : U32) := - (ls0, ls1, i) - -/- [loops::list_nth_mut_shared_loop_pair_merge]: decreases_by tactic -/ -syntax "list_nth_mut_shared_loop_pair_merge_loop_decreases" term+ : tactic -macro_rules -| `(tactic| list_nth_mut_shared_loop_pair_merge_loop_decreases $ls0 $ls1 $i) => - `(tactic| sorry) - -/- [loops::list_nth_shared_mut_loop_pair]: termination measure -/ -@[simp] -def list_nth_shared_mut_loop_pair_loop_terminates (T : Type) (ls0 : list_t T) - (ls1 : list_t T) (i : U32) := - (ls0, ls1, i) - -/- [loops::list_nth_shared_mut_loop_pair]: decreases_by tactic -/ -syntax "list_nth_shared_mut_loop_pair_loop_decreases" term+ : tactic -macro_rules -| `(tactic| list_nth_shared_mut_loop_pair_loop_decreases $ls0 $ls1 $i) => - `(tactic| sorry) - -/- [loops::list_nth_shared_mut_loop_pair_merge]: termination measure -/ -@[simp] -def list_nth_shared_mut_loop_pair_merge_loop_terminates (T : Type) - (ls0 : list_t T) (ls1 : list_t T) (i : U32) := - (ls0, ls1, i) - -/- [loops::list_nth_shared_mut_loop_pair_merge]: decreases_by tactic -/ -syntax "list_nth_shared_mut_loop_pair_merge_loop_decreases" term+ : tactic -macro_rules -| `(tactic| list_nth_shared_mut_loop_pair_merge_loop_decreases $ls0 $ls1 $i) => - `(tactic| sorry) - diff --git a/tests/lean/misc-loops/Loops/Funs.lean b/tests/lean/misc-loops/Loops/Funs.lean deleted file mode 100644 index fd8d62d7..00000000 --- a/tests/lean/misc-loops/Loops/Funs.lean +++ /dev/null @@ -1,705 +0,0 @@ --- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS --- [loops]: function definitions -import Base.Primitives -import Loops.Types -import Loops.Clauses.Clauses - -/- [loops::sum] -/ -def sum_loop_fwd (max : U32) (i : U32) (s : U32) : (Result U32) := - if h: i < max - then - do - let s0 ← s + i - let i0 ← i + (U32.ofInt 1 (by intlit)) - sum_loop_fwd max i0 s0 - else s * (U32.ofInt 2 (by intlit)) -termination_by sum_loop_fwd max i s => sum_loop_terminates max i s -decreasing_by sum_loop_decreases max i s - -/- [loops::sum] -/ -def sum_fwd (max : U32) : Result U32 := - sum_loop_fwd max (U32.ofInt 0 (by intlit)) (U32.ofInt 0 (by intlit)) - -/- [loops::sum_with_mut_borrows] -/ -def sum_with_mut_borrows_loop_fwd - (max : U32) (mi : U32) (ms : U32) : (Result U32) := - if h: mi < max - then - do - let ms0 ← ms + mi - let mi0 ← mi + (U32.ofInt 1 (by intlit)) - sum_with_mut_borrows_loop_fwd max mi0 ms0 - else ms * (U32.ofInt 2 (by intlit)) -termination_by sum_with_mut_borrows_loop_fwd max mi ms => - sum_with_mut_borrows_loop_terminates max mi ms -decreasing_by sum_with_mut_borrows_loop_decreases max mi ms - -/- [loops::sum_with_mut_borrows] -/ -def sum_with_mut_borrows_fwd (max : U32) : Result U32 := - sum_with_mut_borrows_loop_fwd max (U32.ofInt 0 (by intlit)) - (U32.ofInt 0 (by intlit)) - -/- [loops::sum_with_shared_borrows] -/ -def sum_with_shared_borrows_loop_fwd - (max : U32) (i : U32) (s : U32) : (Result U32) := - if h: i < max - then - do - let i0 ← i + (U32.ofInt 1 (by intlit)) - let s0 ← s + i0 - sum_with_shared_borrows_loop_fwd max i0 s0 - else s * (U32.ofInt 2 (by intlit)) -termination_by sum_with_shared_borrows_loop_fwd max i s => - sum_with_shared_borrows_loop_terminates max i s -decreasing_by sum_with_shared_borrows_loop_decreases max i s - -/- [loops::sum_with_shared_borrows] -/ -def sum_with_shared_borrows_fwd (max : U32) : Result U32 := - sum_with_shared_borrows_loop_fwd max (U32.ofInt 0 (by intlit)) - (U32.ofInt 0 (by intlit)) - -/- [loops::clear] -/ -def clear_loop_fwd_back (v : Vec U32) (i : Usize) : (Result (Vec U32)) := - let i0 := vec_len U32 v - if h: i < i0 - then - do - let i1 ← i + (Usize.ofInt 1 (by intlit)) - let v0 ← vec_index_mut_back U32 v i (U32.ofInt 0 (by intlit)) - clear_loop_fwd_back v0 i1 - else Result.ret v -termination_by clear_loop_fwd_back v i => clear_loop_terminates v i -decreasing_by clear_loop_decreases v i - -/- [loops::clear] -/ -def clear_fwd_back (v : Vec U32) : Result (Vec U32) := - clear_loop_fwd_back v (Usize.ofInt 0 (by intlit)) - -/- [loops::list_mem] -/ -def list_mem_loop_fwd (x : U32) (ls : list_t U32) : (Result Bool) := - match h: ls with - | list_t.Cons y tl => - if h: y = x - then Result.ret true - else list_mem_loop_fwd x tl - | list_t.Nil => Result.ret false -termination_by list_mem_loop_fwd x ls => list_mem_loop_terminates x ls -decreasing_by list_mem_loop_decreases x ls - -/- [loops::list_mem] -/ -def list_mem_fwd (x : U32) (ls : list_t U32) : Result Bool := - list_mem_loop_fwd x ls - -/- [loops::list_nth_mut_loop] -/ -def list_nth_mut_loop_loop_fwd - (T : Type) (ls : list_t T) (i : U32) : (Result T) := - match h: ls with - | list_t.Cons x tl => - if h: i = (U32.ofInt 0 (by intlit)) - then Result.ret x - else - do - let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_mut_loop_loop_fwd T tl i0 - | list_t.Nil => Result.fail Error.panic -termination_by list_nth_mut_loop_loop_fwd ls i => - list_nth_mut_loop_loop_terminates T ls i -decreasing_by list_nth_mut_loop_loop_decreases ls i - -/- [loops::list_nth_mut_loop] -/ -def list_nth_mut_loop_fwd (T : Type) (ls : list_t T) (i : U32) : Result T := - list_nth_mut_loop_loop_fwd T ls i - -/- [loops::list_nth_mut_loop] -/ -def list_nth_mut_loop_loop_back - (T : Type) (ls : list_t T) (i : U32) (ret0 : T) : (Result (list_t T)) := - match h: ls with - | list_t.Cons x tl => - if h: i = (U32.ofInt 0 (by intlit)) - then Result.ret (list_t.Cons ret0 tl) - else - do - let i0 ← i - (U32.ofInt 1 (by intlit)) - let tl0 ← list_nth_mut_loop_loop_back T tl i0 ret0 - Result.ret (list_t.Cons x tl0) - | list_t.Nil => Result.fail Error.panic -termination_by list_nth_mut_loop_loop_back ls i ret0 => - list_nth_mut_loop_loop_terminates T ls i -decreasing_by list_nth_mut_loop_loop_decreases ls i - -/- [loops::list_nth_mut_loop] -/ -def list_nth_mut_loop_back - (T : Type) (ls : list_t T) (i : U32) (ret0 : T) : Result (list_t T) := - list_nth_mut_loop_loop_back T ls i ret0 - -/- [loops::list_nth_shared_loop] -/ -def list_nth_shared_loop_loop_fwd - (T : Type) (ls : list_t T) (i : U32) : (Result T) := - match h: ls with - | list_t.Cons x tl => - if h: i = (U32.ofInt 0 (by intlit)) - then Result.ret x - else - do - let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_shared_loop_loop_fwd T tl i0 - | list_t.Nil => Result.fail Error.panic -termination_by list_nth_shared_loop_loop_fwd ls i => - list_nth_shared_loop_loop_terminates T ls i -decreasing_by list_nth_shared_loop_loop_decreases ls i - -/- [loops::list_nth_shared_loop] -/ -def list_nth_shared_loop_fwd (T : Type) (ls : list_t T) (i : U32) : Result T := - list_nth_shared_loop_loop_fwd T ls i - -/- [loops::get_elem_mut] -/ -def get_elem_mut_loop_fwd (x : Usize) (ls : list_t Usize) : (Result Usize) := - match h: ls with - | list_t.Cons y tl => - if h: y = x - then Result.ret y - else get_elem_mut_loop_fwd x tl - | list_t.Nil => Result.fail Error.panic -termination_by get_elem_mut_loop_fwd x ls => get_elem_mut_loop_terminates x ls -decreasing_by get_elem_mut_loop_decreases x ls - -/- [loops::get_elem_mut] -/ -def get_elem_mut_fwd (slots : Vec (list_t Usize)) (x : Usize) : Result Usize := - do - let l ← - vec_index_mut_fwd (list_t Usize) slots (Usize.ofInt 0 (by intlit)) - get_elem_mut_loop_fwd x l - -/- [loops::get_elem_mut] -/ -def get_elem_mut_loop_back - (x : Usize) (ls : list_t Usize) (ret0 : Usize) : (Result (list_t Usize)) := - match h: ls with - | list_t.Cons y tl => - if h: y = x - then Result.ret (list_t.Cons ret0 tl) - else - do - let tl0 ← get_elem_mut_loop_back x tl ret0 - Result.ret (list_t.Cons y tl0) - | list_t.Nil => Result.fail Error.panic -termination_by get_elem_mut_loop_back x ls ret0 => - get_elem_mut_loop_terminates x ls -decreasing_by get_elem_mut_loop_decreases x ls - -/- [loops::get_elem_mut] -/ -def get_elem_mut_back - (slots : Vec (list_t Usize)) (x : Usize) (ret0 : Usize) : - Result (Vec (list_t Usize)) - := - do - let l ← - vec_index_mut_fwd (list_t Usize) slots (Usize.ofInt 0 (by intlit)) - let l0 ← get_elem_mut_loop_back x l ret0 - vec_index_mut_back (list_t Usize) slots (Usize.ofInt 0 (by intlit)) l0 - -/- [loops::get_elem_shared] -/ -def get_elem_shared_loop_fwd - (x : Usize) (ls : list_t Usize) : (Result Usize) := - match h: ls with - | list_t.Cons y tl => - if h: y = x - then Result.ret y - else get_elem_shared_loop_fwd x tl - | list_t.Nil => Result.fail Error.panic -termination_by get_elem_shared_loop_fwd x ls => - get_elem_shared_loop_terminates x ls -decreasing_by get_elem_shared_loop_decreases x ls - -/- [loops::get_elem_shared] -/ -def get_elem_shared_fwd - (slots : Vec (list_t Usize)) (x : Usize) : Result Usize := - do - let l ← vec_index_fwd (list_t Usize) slots (Usize.ofInt 0 (by intlit)) - get_elem_shared_loop_fwd x l - -/- [loops::id_mut] -/ -def id_mut_fwd (T : Type) (ls : list_t T) : Result (list_t T) := - Result.ret ls - -/- [loops::id_mut] -/ -def id_mut_back - (T : Type) (ls : list_t T) (ret0 : list_t T) : Result (list_t T) := - Result.ret ret0 - -/- [loops::id_shared] -/ -def id_shared_fwd (T : Type) (ls : list_t T) : Result (list_t T) := - Result.ret ls - -/- [loops::list_nth_mut_loop_with_id] -/ -def list_nth_mut_loop_with_id_loop_fwd - (T : Type) (i : U32) (ls : list_t T) : (Result T) := - match h: ls with - | list_t.Cons x tl => - if h: i = (U32.ofInt 0 (by intlit)) - then Result.ret x - else - do - let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_mut_loop_with_id_loop_fwd T i0 tl - | list_t.Nil => Result.fail Error.panic -termination_by list_nth_mut_loop_with_id_loop_fwd i ls => - list_nth_mut_loop_with_id_loop_terminates T i ls -decreasing_by list_nth_mut_loop_with_id_loop_decreases i ls - -/- [loops::list_nth_mut_loop_with_id] -/ -def list_nth_mut_loop_with_id_fwd - (T : Type) (ls : list_t T) (i : U32) : Result T := - do - let ls0 ← id_mut_fwd T ls - list_nth_mut_loop_with_id_loop_fwd T i ls0 - -/- [loops::list_nth_mut_loop_with_id] -/ -def list_nth_mut_loop_with_id_loop_back - (T : Type) (i : U32) (ls : list_t T) (ret0 : T) : (Result (list_t T)) := - match h: ls with - | list_t.Cons x tl => - if h: i = (U32.ofInt 0 (by intlit)) - then Result.ret (list_t.Cons ret0 tl) - else - do - let i0 ← i - (U32.ofInt 1 (by intlit)) - let tl0 ← list_nth_mut_loop_with_id_loop_back T i0 tl ret0 - Result.ret (list_t.Cons x tl0) - | list_t.Nil => Result.fail Error.panic -termination_by list_nth_mut_loop_with_id_loop_back i ls ret0 => - list_nth_mut_loop_with_id_loop_terminates T i ls -decreasing_by list_nth_mut_loop_with_id_loop_decreases i ls - -/- [loops::list_nth_mut_loop_with_id] -/ -def list_nth_mut_loop_with_id_back - (T : Type) (ls : list_t T) (i : U32) (ret0 : T) : Result (list_t T) := - do - let ls0 ← id_mut_fwd T ls - let l ← list_nth_mut_loop_with_id_loop_back T i ls0 ret0 - id_mut_back T ls l - -/- [loops::list_nth_shared_loop_with_id] -/ -def list_nth_shared_loop_with_id_loop_fwd - (T : Type) (i : U32) (ls : list_t T) : (Result T) := - match h: ls with - | list_t.Cons x tl => - if h: i = (U32.ofInt 0 (by intlit)) - then Result.ret x - else - do - let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_shared_loop_with_id_loop_fwd T i0 tl - | list_t.Nil => Result.fail Error.panic -termination_by list_nth_shared_loop_with_id_loop_fwd i ls => - list_nth_shared_loop_with_id_loop_terminates T i ls -decreasing_by list_nth_shared_loop_with_id_loop_decreases i ls - -/- [loops::list_nth_shared_loop_with_id] -/ -def list_nth_shared_loop_with_id_fwd - (T : Type) (ls : list_t T) (i : U32) : Result T := - do - let ls0 ← id_shared_fwd T ls - list_nth_shared_loop_with_id_loop_fwd T i ls0 - -/- [loops::list_nth_mut_loop_pair] -/ -def list_nth_mut_loop_pair_loop_fwd - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : (Result (T × T)) := - match h: ls0 with - | list_t.Cons x0 tl0 => - match h: ls1 with - | list_t.Cons x1 tl1 => - if h: i = (U32.ofInt 0 (by intlit)) - then Result.ret (x0, x1) - else - do - let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_mut_loop_pair_loop_fwd T tl0 tl1 i0 - | list_t.Nil => Result.fail Error.panic - | list_t.Nil => Result.fail Error.panic -termination_by list_nth_mut_loop_pair_loop_fwd ls0 ls1 i => - list_nth_mut_loop_pair_loop_terminates T ls0 ls1 i -decreasing_by list_nth_mut_loop_pair_loop_decreases ls0 ls1 i - -/- [loops::list_nth_mut_loop_pair] -/ -def list_nth_mut_loop_pair_fwd - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := - list_nth_mut_loop_pair_loop_fwd T ls0 ls1 i - -/- [loops::list_nth_mut_loop_pair] -/ -def list_nth_mut_loop_pair_loop_back'a - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : - (Result (list_t T)) - := - match h: ls0 with - | list_t.Cons x0 tl0 => - match h: ls1 with - | list_t.Cons x1 tl1 => - if h: i = (U32.ofInt 0 (by intlit)) - then Result.ret (list_t.Cons ret0 tl0) - else - do - let i0 ← i - (U32.ofInt 1 (by intlit)) - let tl00 ← list_nth_mut_loop_pair_loop_back'a T tl0 tl1 i0 ret0 - Result.ret (list_t.Cons x0 tl00) - | list_t.Nil => Result.fail Error.panic - | list_t.Nil => Result.fail Error.panic -termination_by list_nth_mut_loop_pair_loop_back'a ls0 ls1 i ret0 => - list_nth_mut_loop_pair_loop_terminates T ls0 ls1 i -decreasing_by list_nth_mut_loop_pair_loop_decreases ls0 ls1 i - -/- [loops::list_nth_mut_loop_pair] -/ -def list_nth_mut_loop_pair_back'a - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : - Result (list_t T) - := - list_nth_mut_loop_pair_loop_back'a T ls0 ls1 i ret0 - -/- [loops::list_nth_mut_loop_pair] -/ -def list_nth_mut_loop_pair_loop_back'b - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : - (Result (list_t T)) - := - match h: ls0 with - | list_t.Cons x0 tl0 => - match h: ls1 with - | list_t.Cons x1 tl1 => - if h: i = (U32.ofInt 0 (by intlit)) - then Result.ret (list_t.Cons ret0 tl1) - else - do - let i0 ← i - (U32.ofInt 1 (by intlit)) - let tl10 ← list_nth_mut_loop_pair_loop_back'b T tl0 tl1 i0 ret0 - Result.ret (list_t.Cons x1 tl10) - | list_t.Nil => Result.fail Error.panic - | list_t.Nil => Result.fail Error.panic -termination_by list_nth_mut_loop_pair_loop_back'b ls0 ls1 i ret0 => - list_nth_mut_loop_pair_loop_terminates T ls0 ls1 i -decreasing_by list_nth_mut_loop_pair_loop_decreases ls0 ls1 i - -/- [loops::list_nth_mut_loop_pair] -/ -def list_nth_mut_loop_pair_back'b - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : - Result (list_t T) - := - list_nth_mut_loop_pair_loop_back'b T ls0 ls1 i ret0 - -/- [loops::list_nth_shared_loop_pair] -/ -def list_nth_shared_loop_pair_loop_fwd - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : (Result (T × T)) := - match h: ls0 with - | list_t.Cons x0 tl0 => - match h: ls1 with - | list_t.Cons x1 tl1 => - if h: i = (U32.ofInt 0 (by intlit)) - then Result.ret (x0, x1) - else - do - let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_shared_loop_pair_loop_fwd T tl0 tl1 i0 - | list_t.Nil => Result.fail Error.panic - | list_t.Nil => Result.fail Error.panic -termination_by list_nth_shared_loop_pair_loop_fwd ls0 ls1 i => - list_nth_shared_loop_pair_loop_terminates T ls0 ls1 i -decreasing_by list_nth_shared_loop_pair_loop_decreases ls0 ls1 i - -/- [loops::list_nth_shared_loop_pair] -/ -def list_nth_shared_loop_pair_fwd - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := - list_nth_shared_loop_pair_loop_fwd T ls0 ls1 i - -/- [loops::list_nth_mut_loop_pair_merge] -/ -def list_nth_mut_loop_pair_merge_loop_fwd - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : (Result (T × T)) := - match h: ls0 with - | list_t.Cons x0 tl0 => - match h: ls1 with - | list_t.Cons x1 tl1 => - if h: i = (U32.ofInt 0 (by intlit)) - then Result.ret (x0, x1) - else - do - let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_mut_loop_pair_merge_loop_fwd T tl0 tl1 i0 - | list_t.Nil => Result.fail Error.panic - | list_t.Nil => Result.fail Error.panic -termination_by list_nth_mut_loop_pair_merge_loop_fwd ls0 ls1 i => - list_nth_mut_loop_pair_merge_loop_terminates T ls0 ls1 i -decreasing_by list_nth_mut_loop_pair_merge_loop_decreases ls0 ls1 i - -/- [loops::list_nth_mut_loop_pair_merge] -/ -def list_nth_mut_loop_pair_merge_fwd - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := - list_nth_mut_loop_pair_merge_loop_fwd T ls0 ls1 i - -/- [loops::list_nth_mut_loop_pair_merge] -/ -def list_nth_mut_loop_pair_merge_loop_back - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : (T × T)) : - (Result ((list_t T) × (list_t T))) - := - match h: ls0 with - | list_t.Cons x0 tl0 => - match h: ls1 with - | list_t.Cons x1 tl1 => - if h: i = (U32.ofInt 0 (by intlit)) - then - let (t, t0) := ret0 - Result.ret (list_t.Cons t tl0, list_t.Cons t0 tl1) - else - do - let i0 ← i - (U32.ofInt 1 (by intlit)) - let (tl00, tl10) ← - list_nth_mut_loop_pair_merge_loop_back T tl0 tl1 i0 ret0 - Result.ret (list_t.Cons x0 tl00, list_t.Cons x1 tl10) - | list_t.Nil => Result.fail Error.panic - | list_t.Nil => Result.fail Error.panic -termination_by list_nth_mut_loop_pair_merge_loop_back ls0 ls1 i ret0 => - list_nth_mut_loop_pair_merge_loop_terminates T ls0 ls1 i -decreasing_by list_nth_mut_loop_pair_merge_loop_decreases ls0 ls1 i - -/- [loops::list_nth_mut_loop_pair_merge] -/ -def list_nth_mut_loop_pair_merge_back - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : (T × T)) : - Result ((list_t T) × (list_t T)) - := - list_nth_mut_loop_pair_merge_loop_back T ls0 ls1 i ret0 - -/- [loops::list_nth_shared_loop_pair_merge] -/ -def list_nth_shared_loop_pair_merge_loop_fwd - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : (Result (T × T)) := - match h: ls0 with - | list_t.Cons x0 tl0 => - match h: ls1 with - | list_t.Cons x1 tl1 => - if h: i = (U32.ofInt 0 (by intlit)) - then Result.ret (x0, x1) - else - do - let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_shared_loop_pair_merge_loop_fwd T tl0 tl1 i0 - | list_t.Nil => Result.fail Error.panic - | list_t.Nil => Result.fail Error.panic -termination_by list_nth_shared_loop_pair_merge_loop_fwd ls0 ls1 i => - list_nth_shared_loop_pair_merge_loop_terminates T ls0 ls1 i -decreasing_by list_nth_shared_loop_pair_merge_loop_decreases ls0 ls1 i - -/- [loops::list_nth_shared_loop_pair_merge] -/ -def list_nth_shared_loop_pair_merge_fwd - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := - list_nth_shared_loop_pair_merge_loop_fwd T ls0 ls1 i - -/- [loops::list_nth_mut_shared_loop_pair] -/ -def list_nth_mut_shared_loop_pair_loop_fwd - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : (Result (T × T)) := - match h: ls0 with - | list_t.Cons x0 tl0 => - match h: ls1 with - | list_t.Cons x1 tl1 => - if h: i = (U32.ofInt 0 (by intlit)) - then Result.ret (x0, x1) - else - do - let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_mut_shared_loop_pair_loop_fwd T tl0 tl1 i0 - | list_t.Nil => Result.fail Error.panic - | list_t.Nil => Result.fail Error.panic -termination_by list_nth_mut_shared_loop_pair_loop_fwd ls0 ls1 i => - list_nth_mut_shared_loop_pair_loop_terminates T ls0 ls1 i -decreasing_by list_nth_mut_shared_loop_pair_loop_decreases ls0 ls1 i - -/- [loops::list_nth_mut_shared_loop_pair] -/ -def list_nth_mut_shared_loop_pair_fwd - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := - list_nth_mut_shared_loop_pair_loop_fwd T ls0 ls1 i - -/- [loops::list_nth_mut_shared_loop_pair] -/ -def list_nth_mut_shared_loop_pair_loop_back - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : - (Result (list_t T)) - := - match h: ls0 with - | list_t.Cons x0 tl0 => - match h: ls1 with - | list_t.Cons x1 tl1 => - if h: i = (U32.ofInt 0 (by intlit)) - then Result.ret (list_t.Cons ret0 tl0) - else - do - let i0 ← i - (U32.ofInt 1 (by intlit)) - let tl00 ← - list_nth_mut_shared_loop_pair_loop_back T tl0 tl1 i0 ret0 - Result.ret (list_t.Cons x0 tl00) - | list_t.Nil => Result.fail Error.panic - | list_t.Nil => Result.fail Error.panic -termination_by list_nth_mut_shared_loop_pair_loop_back ls0 ls1 i ret0 => - list_nth_mut_shared_loop_pair_loop_terminates T ls0 ls1 i -decreasing_by list_nth_mut_shared_loop_pair_loop_decreases ls0 ls1 i - -/- [loops::list_nth_mut_shared_loop_pair] -/ -def list_nth_mut_shared_loop_pair_back - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : - Result (list_t T) - := - list_nth_mut_shared_loop_pair_loop_back T ls0 ls1 i ret0 - -/- [loops::list_nth_mut_shared_loop_pair_merge] -/ -def list_nth_mut_shared_loop_pair_merge_loop_fwd - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : (Result (T × T)) := - match h: ls0 with - | list_t.Cons x0 tl0 => - match h: ls1 with - | list_t.Cons x1 tl1 => - if h: i = (U32.ofInt 0 (by intlit)) - then Result.ret (x0, x1) - else - do - let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_mut_shared_loop_pair_merge_loop_fwd T tl0 tl1 i0 - | list_t.Nil => Result.fail Error.panic - | list_t.Nil => Result.fail Error.panic -termination_by list_nth_mut_shared_loop_pair_merge_loop_fwd ls0 ls1 i => - list_nth_mut_shared_loop_pair_merge_loop_terminates T ls0 ls1 i -decreasing_by list_nth_mut_shared_loop_pair_merge_loop_decreases ls0 ls1 i - -/- [loops::list_nth_mut_shared_loop_pair_merge] -/ -def list_nth_mut_shared_loop_pair_merge_fwd - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := - list_nth_mut_shared_loop_pair_merge_loop_fwd T ls0 ls1 i - -/- [loops::list_nth_mut_shared_loop_pair_merge] -/ -def list_nth_mut_shared_loop_pair_merge_loop_back - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : - (Result (list_t T)) - := - match h: ls0 with - | list_t.Cons x0 tl0 => - match h: ls1 with - | list_t.Cons x1 tl1 => - if h: i = (U32.ofInt 0 (by intlit)) - then Result.ret (list_t.Cons ret0 tl0) - else - do - let i0 ← i - (U32.ofInt 1 (by intlit)) - let tl00 ← - list_nth_mut_shared_loop_pair_merge_loop_back T tl0 tl1 i0 ret0 - Result.ret (list_t.Cons x0 tl00) - | list_t.Nil => Result.fail Error.panic - | list_t.Nil => Result.fail Error.panic -termination_by list_nth_mut_shared_loop_pair_merge_loop_back ls0 ls1 i ret0 => - list_nth_mut_shared_loop_pair_merge_loop_terminates T ls0 ls1 i -decreasing_by list_nth_mut_shared_loop_pair_merge_loop_decreases ls0 ls1 i - -/- [loops::list_nth_mut_shared_loop_pair_merge] -/ -def list_nth_mut_shared_loop_pair_merge_back - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : - Result (list_t T) - := - list_nth_mut_shared_loop_pair_merge_loop_back T ls0 ls1 i ret0 - -/- [loops::list_nth_shared_mut_loop_pair] -/ -def list_nth_shared_mut_loop_pair_loop_fwd - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : (Result (T × T)) := - match h: ls0 with - | list_t.Cons x0 tl0 => - match h: ls1 with - | list_t.Cons x1 tl1 => - if h: i = (U32.ofInt 0 (by intlit)) - then Result.ret (x0, x1) - else - do - let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_shared_mut_loop_pair_loop_fwd T tl0 tl1 i0 - | list_t.Nil => Result.fail Error.panic - | list_t.Nil => Result.fail Error.panic -termination_by list_nth_shared_mut_loop_pair_loop_fwd ls0 ls1 i => - list_nth_shared_mut_loop_pair_loop_terminates T ls0 ls1 i -decreasing_by list_nth_shared_mut_loop_pair_loop_decreases ls0 ls1 i - -/- [loops::list_nth_shared_mut_loop_pair] -/ -def list_nth_shared_mut_loop_pair_fwd - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := - list_nth_shared_mut_loop_pair_loop_fwd T ls0 ls1 i - -/- [loops::list_nth_shared_mut_loop_pair] -/ -def list_nth_shared_mut_loop_pair_loop_back - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : - (Result (list_t T)) - := - match h: ls0 with - | list_t.Cons x0 tl0 => - match h: ls1 with - | list_t.Cons x1 tl1 => - if h: i = (U32.ofInt 0 (by intlit)) - then Result.ret (list_t.Cons ret0 tl1) - else - do - let i0 ← i - (U32.ofInt 1 (by intlit)) - let tl10 ← - list_nth_shared_mut_loop_pair_loop_back T tl0 tl1 i0 ret0 - Result.ret (list_t.Cons x1 tl10) - | list_t.Nil => Result.fail Error.panic - | list_t.Nil => Result.fail Error.panic -termination_by list_nth_shared_mut_loop_pair_loop_back ls0 ls1 i ret0 => - list_nth_shared_mut_loop_pair_loop_terminates T ls0 ls1 i -decreasing_by list_nth_shared_mut_loop_pair_loop_decreases ls0 ls1 i - -/- [loops::list_nth_shared_mut_loop_pair] -/ -def list_nth_shared_mut_loop_pair_back - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : - Result (list_t T) - := - list_nth_shared_mut_loop_pair_loop_back T ls0 ls1 i ret0 - -/- [loops::list_nth_shared_mut_loop_pair_merge] -/ -def list_nth_shared_mut_loop_pair_merge_loop_fwd - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : (Result (T × T)) := - match h: ls0 with - | list_t.Cons x0 tl0 => - match h: ls1 with - | list_t.Cons x1 tl1 => - if h: i = (U32.ofInt 0 (by intlit)) - then Result.ret (x0, x1) - else - do - let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_shared_mut_loop_pair_merge_loop_fwd T tl0 tl1 i0 - | list_t.Nil => Result.fail Error.panic - | list_t.Nil => Result.fail Error.panic -termination_by list_nth_shared_mut_loop_pair_merge_loop_fwd ls0 ls1 i => - list_nth_shared_mut_loop_pair_merge_loop_terminates T ls0 ls1 i -decreasing_by list_nth_shared_mut_loop_pair_merge_loop_decreases ls0 ls1 i - -/- [loops::list_nth_shared_mut_loop_pair_merge] -/ -def list_nth_shared_mut_loop_pair_merge_fwd - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := - list_nth_shared_mut_loop_pair_merge_loop_fwd T ls0 ls1 i - -/- [loops::list_nth_shared_mut_loop_pair_merge] -/ -def list_nth_shared_mut_loop_pair_merge_loop_back - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : - (Result (list_t T)) - := - match h: ls0 with - | list_t.Cons x0 tl0 => - match h: ls1 with - | list_t.Cons x1 tl1 => - if h: i = (U32.ofInt 0 (by intlit)) - then Result.ret (list_t.Cons ret0 tl1) - else - do - let i0 ← i - (U32.ofInt 1 (by intlit)) - let tl10 ← - list_nth_shared_mut_loop_pair_merge_loop_back T tl0 tl1 i0 ret0 - Result.ret (list_t.Cons x1 tl10) - | list_t.Nil => Result.fail Error.panic - | list_t.Nil => Result.fail Error.panic -termination_by list_nth_shared_mut_loop_pair_merge_loop_back ls0 ls1 i ret0 => - list_nth_shared_mut_loop_pair_merge_loop_terminates T ls0 ls1 i -decreasing_by list_nth_shared_mut_loop_pair_merge_loop_decreases ls0 ls1 i - -/- [loops::list_nth_shared_mut_loop_pair_merge] -/ -def list_nth_shared_mut_loop_pair_merge_back - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : - Result (list_t T) - := - list_nth_shared_mut_loop_pair_merge_loop_back T ls0 ls1 i ret0 - diff --git a/tests/lean/misc-loops/Loops/Types.lean b/tests/lean/misc-loops/Loops/Types.lean deleted file mode 100644 index ca43f4c8..00000000 --- a/tests/lean/misc-loops/Loops/Types.lean +++ /dev/null @@ -1,9 +0,0 @@ --- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS --- [loops]: type definitions -import Base.Primitives - -/- [loops::List] -/ -inductive list_t (T : Type) := -| Cons : T -> list_t T -> list_t T -| Nil : list_t T - diff --git a/tests/lean/misc-loops/lake-manifest.json b/tests/lean/misc-loops/lake-manifest.json deleted file mode 100644 index 57b071ca..00000000 --- a/tests/lean/misc-loops/lake-manifest.json +++ /dev/null @@ -1,27 +0,0 @@ -{"version": 4, - "packagesDir": "./lake-packages", - "packages": - [{"git": - {"url": "https://github.com/leanprover-community/mathlib4.git", - "subDir?": null, - "rev": "4037792ead804d7bfa8868e2c4684d4223c15ece", - "name": "mathlib", - "inputRev?": null}}, - {"git": - {"url": "https://github.com/gebner/quote4", - "subDir?": null, - "rev": "2412c4fdf4a8b689f4467618e5e7b371ae5014aa", - "name": "Qq", - "inputRev?": "master"}}, - {"git": - {"url": "https://github.com/JLimperg/aesop", - "subDir?": null, - "rev": "7fe9ecd9339b0e1796e89d243b776849c305c690", - "name": "aesop", - "inputRev?": "master"}}, - {"git": - {"url": "https://github.com/leanprover/std4", - "subDir?": null, - "rev": "24897887905b3a1254b244369f5dd2cf6174b0ee", - "name": "std", - "inputRev?": "main"}}]} diff --git a/tests/lean/misc-loops/lakefile.lean b/tests/lean/misc-loops/lakefile.lean deleted file mode 100644 index 097c0a7d..00000000 --- a/tests/lean/misc-loops/lakefile.lean +++ /dev/null @@ -1,12 +0,0 @@ -import Lake -open Lake DSL - -require mathlib from git - "https://github.com/leanprover-community/mathlib4.git" - -package «loops» {} - -lean_lib «Base» {} - -@[default_target] -lean_lib «Loops» {} diff --git a/tests/lean/misc-loops/lean-toolchain b/tests/lean/misc-loops/lean-toolchain deleted file mode 100644 index bbf57f10..00000000 --- a/tests/lean/misc-loops/lean-toolchain +++ /dev/null @@ -1 +0,0 @@ -leanprover/lean4:nightly-2023-01-21 diff --git a/tests/lean/misc-no_nested_borrows/Base/Primitives.lean b/tests/lean/misc-no_nested_borrows/Base/Primitives.lean deleted file mode 100644 index 4a66a453..00000000 --- a/tests/lean/misc-no_nested_borrows/Base/Primitives.lean +++ /dev/null @@ -1,583 +0,0 @@ -import Lean -import Lean.Meta.Tactic.Simp -import Init.Data.List.Basic -import Mathlib.Tactic.RunCmd - --------------------- --- ASSERT COMMAND -- --------------------- - -open Lean Elab Command Term Meta - -syntax (name := assert) "#assert" term: command - -@[command_elab assert] -unsafe -def assertImpl : CommandElab := fun (_stx: Syntax) => do - runTermElabM (fun _ => do - let r ← evalTerm Bool (mkConst ``Bool) _stx[1] - if not r then - logInfo "Assertion failed for: " - logInfo _stx[1] - logError "Expression reduced to false" - pure ()) - -#eval 2 == 2 -#assert (2 == 2) - -------------- --- PRELUDE -- -------------- - --- Results & monadic combinators - -inductive Error where - | assertionFailure: Error - | integerOverflow: Error - | divisionByZero: Error - | arrayOutOfBounds: Error - | maximumSizeExceeded: Error - | panic: Error -deriving Repr, BEq - -open Error - -inductive Result (α : Type u) where - | ret (v: α): Result α - | fail (e: Error): Result α -deriving Repr, BEq - -open Result - -instance Result_Inhabited (α : Type u) : Inhabited (Result α) := - Inhabited.mk (fail panic) - -/- HELPERS -/ - -def ret? {α: Type} (r: Result α): Bool := - match r with - | Result.ret _ => true - | Result.fail _ => false - -def massert (b:Bool) : Result Unit := - if b then .ret () else fail assertionFailure - -def eval_global {α: Type} (x: Result α) (_: ret? x): α := - match x with - | Result.fail _ => by contradiction - | Result.ret x => x - -/- DO-DSL SUPPORT -/ - -def bind (x: Result α) (f: α -> Result β) : Result β := - match x with - | ret v => f v - | fail v => fail v - --- Allows using Result in do-blocks -instance : Bind Result where - bind := bind - --- Allows using return x in do-blocks -instance : Pure Result where - pure := fun x => ret x - -/- CUSTOM-DSL SUPPORT -/ - --- Let-binding the Result of a monadic operation is oftentimes not sufficient, --- because we may need a hypothesis for equational reasoning in the scope. We --- rely on subtype, and a custom let-binding operator, in effect recreating our --- own variant of the do-dsl - -def Result.attach {α: Type} (o : Result α): Result { x : α // o = ret x } := - match o with - | .ret x => .ret ⟨x, rfl⟩ - | .fail e => .fail e - -macro "let" e:term " ⟵ " f:term : doElem => - `(doElem| let ⟨$e, h⟩ ← Result.attach $f) - --- TODO: any way to factorize both definitions? -macro "let" e:term " <-- " f:term : doElem => - `(doElem| let ⟨$e, h⟩ ← Result.attach $f) - --- We call the hypothesis `h`, in effect making it unavailable to the user --- (because too much shadowing). But in practice, once can use the French single --- quote notation (input with f< and f>), where `‹ h ›` finds a suitable --- hypothesis in the context, this is equivalent to `have x: h := by assumption in x` -#eval do - let y <-- .ret (0: Nat) - let _: y = 0 := by cases ‹ ret 0 = ret y › ; decide - let r: { x: Nat // x = 0 } := ⟨ y, by assumption ⟩ - .ret r - ----------------------- --- MACHINE INTEGERS -- ----------------------- - --- We redefine our machine integers types. - --- For Isize/Usize, we reuse `getNumBits` from `USize`. You cannot reduce `getNumBits` --- using the simplifier, meaning that proofs do not depend on the compile-time value of --- USize.size. (Lean assumes 32 or 64-bit platforms, and Rust doesn't really support, at --- least officially, 16-bit microcontrollers, so this seems like a fine design decision --- for now.) - --- Note from Chris Bailey: "If there's more than one salient property of your --- definition then the subtyping strategy might get messy, and the property part --- of a subtype is less discoverable by the simplifier or tactics like --- library_search." So, we will not add refinements on the return values of the --- operations defined on Primitives, but will rather rely on custom lemmas to --- invert on possible return values of the primitive operations. - --- Machine integer constants, done via `ofNatCore`, which requires a proof that --- the `Nat` fits within the desired integer type. We provide a custom tactic. - -open System.Platform.getNumBits - --- TODO: is there a way of only importing System.Platform.getNumBits? --- -@[simp] def size_num_bits : Nat := (System.Platform.getNumBits ()).val - --- Remark: Lean seems to use < for the comparisons with the upper bounds by convention. --- We keep the F* convention for now. -@[simp] def Isize.min : Int := - (HPow.hPow 2 (size_num_bits - 1)) -@[simp] def Isize.max : Int := (HPow.hPow 2 (size_num_bits - 1)) - 1 -@[simp] def I8.min : Int := - (HPow.hPow 2 7) -@[simp] def I8.max : Int := HPow.hPow 2 7 - 1 -@[simp] def I16.min : Int := - (HPow.hPow 2 15) -@[simp] def I16.max : Int := HPow.hPow 2 15 - 1 -@[simp] def I32.min : Int := -(HPow.hPow 2 31) -@[simp] def I32.max : Int := HPow.hPow 2 31 - 1 -@[simp] def I64.min : Int := -(HPow.hPow 2 63) -@[simp] def I64.max : Int := HPow.hPow 2 63 - 1 -@[simp] def I128.min : Int := -(HPow.hPow 2 127) -@[simp] def I128.max : Int := HPow.hPow 2 127 - 1 -@[simp] def Usize.min : Int := 0 -@[simp] def Usize.max : Int := HPow.hPow 2 size_num_bits - 1 -@[simp] def U8.min : Int := 0 -@[simp] def U8.max : Int := HPow.hPow 2 8 - 1 -@[simp] def U16.min : Int := 0 -@[simp] def U16.max : Int := HPow.hPow 2 16 - 1 -@[simp] def U32.min : Int := 0 -@[simp] def U32.max : Int := HPow.hPow 2 32 - 1 -@[simp] def U64.min : Int := 0 -@[simp] def U64.max : Int := HPow.hPow 2 64 - 1 -@[simp] def U128.min : Int := 0 -@[simp] def U128.max : Int := HPow.hPow 2 128 - 1 - -#assert (I8.min == -128) -#assert (I8.max == 127) -#assert (I16.min == -32768) -#assert (I16.max == 32767) -#assert (I32.min == -2147483648) -#assert (I32.max == 2147483647) -#assert (I64.min == -9223372036854775808) -#assert (I64.max == 9223372036854775807) -#assert (I128.min == -170141183460469231731687303715884105728) -#assert (I128.max == 170141183460469231731687303715884105727) -#assert (U8.min == 0) -#assert (U8.max == 255) -#assert (U16.min == 0) -#assert (U16.max == 65535) -#assert (U32.min == 0) -#assert (U32.max == 4294967295) -#assert (U64.min == 0) -#assert (U64.max == 18446744073709551615) -#assert (U128.min == 0) -#assert (U128.max == 340282366920938463463374607431768211455) - -inductive ScalarTy := -| Isize -| I8 -| I16 -| I32 -| I64 -| I128 -| Usize -| U8 -| U16 -| U32 -| U64 -| U128 - -def Scalar.min (ty : ScalarTy) : Int := - match ty with - | .Isize => Isize.min - | .I8 => I8.min - | .I16 => I16.min - | .I32 => I32.min - | .I64 => I64.min - | .I128 => I128.min - | .Usize => Usize.min - | .U8 => U8.min - | .U16 => U16.min - | .U32 => U32.min - | .U64 => U64.min - | .U128 => U128.min - -def Scalar.max (ty : ScalarTy) : Int := - match ty with - | .Isize => Isize.max - | .I8 => I8.max - | .I16 => I16.max - | .I32 => I32.max - | .I64 => I64.max - | .I128 => I128.max - | .Usize => Usize.max - | .U8 => U8.max - | .U16 => U16.max - | .U32 => U32.max - | .U64 => U64.max - | .U128 => U128.max - --- "Conservative" bounds --- We use those because we can't compare to the isize bounds (which can't --- reduce at compile-time). Whenever we perform an arithmetic operation like --- addition we need to check that the result is in bounds: we first compare --- to the conservative bounds, which reduce, then compare to the real bounds. --- This is useful for the various #asserts that we want to reduce at --- type-checking time. -def Scalar.cMin (ty : ScalarTy) : Int := - match ty with - | .Isize => I32.min - | _ => Scalar.min ty - -def Scalar.cMax (ty : ScalarTy) : Int := - match ty with - | .Isize => I32.max - | .Usize => U32.max - | _ => Scalar.max ty - -theorem Scalar.cMin_bound ty : Scalar.min ty <= Scalar.cMin ty := by sorry -theorem Scalar.cMax_bound ty : Scalar.min ty <= Scalar.cMin ty := by sorry - -structure Scalar (ty : ScalarTy) where - val : Int - hmin : Scalar.min ty <= val - hmax : val <= Scalar.max ty - -theorem Scalar.bound_suffices (ty : ScalarTy) (x : Int) : - Scalar.cMin ty <= x && x <= Scalar.cMax ty -> - (decide (Scalar.min ty ≤ x) && decide (x ≤ Scalar.max ty)) = true - := by sorry - -def Scalar.ofIntCore {ty : ScalarTy} (x : Int) - (hmin : Scalar.min ty <= x) (hmax : x <= Scalar.max ty) : Scalar ty := - { val := x, hmin := hmin, hmax := hmax } - -def Scalar.ofInt {ty : ScalarTy} (x : Int) - (h : Scalar.min ty <= x && x <= Scalar.max ty) : Scalar ty := - let hmin: Scalar.min ty <= x := by sorry - let hmax: x <= Scalar.max ty := by sorry - Scalar.ofIntCore x hmin hmax - --- Further thoughts: look at what has been done here: --- https://github.com/leanprover-community/mathlib4/blob/master/Mathlib/Data/Fin/Basic.lean --- and --- https://github.com/leanprover-community/mathlib4/blob/master/Mathlib/Data/UInt.lean --- which both contain a fair amount of reasoning already! -def Scalar.tryMk (ty : ScalarTy) (x : Int) : Result (Scalar ty) := - -- TODO: write this with only one if then else - if hmin_cons: Scalar.cMin ty <= x || Scalar.min ty <= x then - if hmax_cons: x <= Scalar.cMax ty || x <= Scalar.max ty then - let hmin: Scalar.min ty <= x := by sorry - let hmax: x <= Scalar.max ty := by sorry - return Scalar.ofIntCore x hmin hmax - else fail integerOverflow - else fail integerOverflow - -def Scalar.neg {ty : ScalarTy} (x : Scalar ty) : Result (Scalar ty) := Scalar.tryMk ty (- x.val) - -def Scalar.div {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - if y.val != 0 then Scalar.tryMk ty (x.val / y.val) else fail divisionByZero - --- Checking that the % operation in Lean computes the same as the remainder operation in Rust -#assert 1 % 2 = (1:Int) -#assert (-1) % 2 = -1 -#assert 1 % (-2) = 1 -#assert (-1) % (-2) = -1 - -def Scalar.rem {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - if y.val != 0 then Scalar.tryMk ty (x.val % y.val) else fail divisionByZero - -def Scalar.add {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - Scalar.tryMk ty (x.val + y.val) - -def Scalar.sub {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - Scalar.tryMk ty (x.val - y.val) - -def Scalar.mul {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - Scalar.tryMk ty (x.val * y.val) - --- TODO: instances of +, -, * etc. for scalars - --- Cast an integer from a [src_ty] to a [tgt_ty] --- TODO: check the semantics of casts in Rust -def Scalar.cast {src_ty : ScalarTy} (tgt_ty : ScalarTy) (x : Scalar src_ty) : Result (Scalar tgt_ty) := - Scalar.tryMk tgt_ty x.val - --- The scalar types --- We declare the definitions as reducible so that Lean can unfold them (useful --- for type class resolution for instance). -@[reducible] def Isize := Scalar .Isize -@[reducible] def I8 := Scalar .I8 -@[reducible] def I16 := Scalar .I16 -@[reducible] def I32 := Scalar .I32 -@[reducible] def I64 := Scalar .I64 -@[reducible] def I128 := Scalar .I128 -@[reducible] def Usize := Scalar .Usize -@[reducible] def U8 := Scalar .U8 -@[reducible] def U16 := Scalar .U16 -@[reducible] def U32 := Scalar .U32 -@[reducible] def U64 := Scalar .U64 -@[reducible] def U128 := Scalar .U128 - --- TODO: below: not sure this is the best way. --- Should we rather overload operations like +, -, etc.? --- Also, it is possible to automate the generation of those definitions --- with macros (but would it be a good idea? It would be less easy to --- read the file, which is not supposed to change a lot) - --- Negation - -/-- -Remark: there is no heterogeneous negation in the Lean prelude: we thus introduce -one here. - -The notation typeclass for heterogeneous addition. -This enables the notation `- a : β` where `a : α`. --/ -class HNeg (α : Type u) (β : outParam (Type v)) where - /-- `- a` computes the negation of `a`. - The meaning of this notation is type-dependent. -/ - hNeg : α → β - -prefix:75 "-" => HNeg.hNeg - -instance : HNeg Isize (Result Isize) where hNeg x := Scalar.neg x -instance : HNeg I8 (Result I8) where hNeg x := Scalar.neg x -instance : HNeg I16 (Result I16) where hNeg x := Scalar.neg x -instance : HNeg I32 (Result I32) where hNeg x := Scalar.neg x -instance : HNeg I64 (Result I64) where hNeg x := Scalar.neg x -instance : HNeg I128 (Result I128) where hNeg x := Scalar.neg x - --- Addition -instance {ty} : HAdd (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hAdd x y := Scalar.add x y - --- Substraction -instance {ty} : HSub (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hSub x y := Scalar.sub x y - --- Multiplication -instance {ty} : HMul (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hMul x y := Scalar.mul x y - --- Division -instance {ty} : HDiv (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hDiv x y := Scalar.div x y - --- Remainder -instance {ty} : HMod (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hMod x y := Scalar.rem x y - --- ofIntCore --- TODO: typeclass? -def Isize.ofIntCore := @Scalar.ofIntCore .Isize -def I8.ofIntCore := @Scalar.ofIntCore .I8 -def I16.ofIntCore := @Scalar.ofIntCore .I16 -def I32.ofIntCore := @Scalar.ofIntCore .I32 -def I64.ofIntCore := @Scalar.ofIntCore .I64 -def I128.ofIntCore := @Scalar.ofIntCore .I128 -def Usize.ofIntCore := @Scalar.ofIntCore .Usize -def U8.ofIntCore := @Scalar.ofIntCore .U8 -def U16.ofIntCore := @Scalar.ofIntCore .U16 -def U32.ofIntCore := @Scalar.ofIntCore .U32 -def U64.ofIntCore := @Scalar.ofIntCore .U64 -def U128.ofIntCore := @Scalar.ofIntCore .U128 - --- ofInt --- TODO: typeclass? -def Isize.ofInt := @Scalar.ofInt .Isize -def I8.ofInt := @Scalar.ofInt .I8 -def I16.ofInt := @Scalar.ofInt .I16 -def I32.ofInt := @Scalar.ofInt .I32 -def I64.ofInt := @Scalar.ofInt .I64 -def I128.ofInt := @Scalar.ofInt .I128 -def Usize.ofInt := @Scalar.ofInt .Usize -def U8.ofInt := @Scalar.ofInt .U8 -def U16.ofInt := @Scalar.ofInt .U16 -def U32.ofInt := @Scalar.ofInt .U32 -def U64.ofInt := @Scalar.ofInt .U64 -def U128.ofInt := @Scalar.ofInt .U128 - --- Comparisons -instance {ty} : LT (Scalar ty) where - lt a b := LT.lt a.val b.val - -instance {ty} : LE (Scalar ty) where le a b := LE.le a.val b.val - -instance Scalar.decLt {ty} (a b : Scalar ty) : Decidable (LT.lt a b) := Int.decLt .. -instance Scalar.decLe {ty} (a b : Scalar ty) : Decidable (LE.le a b) := Int.decLe .. - -theorem Scalar.eq_of_val_eq {ty} : ∀ {i j : Scalar ty}, Eq i.val j.val → Eq i j - | ⟨_, _, _⟩, ⟨_, _, _⟩, rfl => rfl - -theorem Scalar.val_eq_of_eq {ty} {i j : Scalar ty} (h : Eq i j) : Eq i.val j.val := - h ▸ rfl - -theorem Scalar.ne_of_val_ne {ty} {i j : Scalar ty} (h : Not (Eq i.val j.val)) : Not (Eq i j) := - fun h' => absurd (val_eq_of_eq h') h - -instance (ty : ScalarTy) : DecidableEq (Scalar ty) := - fun i j => - match decEq i.val j.val with - | isTrue h => isTrue (Scalar.eq_of_val_eq h) - | isFalse h => isFalse (Scalar.ne_of_val_ne h) - -def Scalar.toInt {ty} (n : Scalar ty) : Int := n.val - --- Tactic to prove that integers are in bounds -syntax "intlit" : tactic - -macro_rules - | `(tactic| intlit) => `(tactic| apply Scalar.bound_suffices ; decide) - --- -- We now define a type class that subsumes the various machine integer types, so --- -- as to write a concise definition for scalar_cast, rather than exhaustively --- -- enumerating all of the possible pairs. We remark that Rust has sane semantics --- -- and fails if a cast operation would involve a truncation or modulo. - --- class MachineInteger (t: Type) where --- size: Nat --- val: t -> Fin size --- ofNatCore: (n:Nat) -> LT.lt n size -> t - --- set_option hygiene false in --- run_cmd --- for typeName in [`UInt8, `UInt16, `UInt32, `UInt64, `USize].map Lean.mkIdent do --- Lean.Elab.Command.elabCommand (← `( --- namespace $typeName --- instance: MachineInteger $typeName where --- size := size --- val := val --- ofNatCore := ofNatCore --- end $typeName --- )) - --- -- Aeneas only instantiates the destination type (`src` is implicit). We rely on --- -- Lean to infer `src`. - --- def scalar_cast { src: Type } (dst: Type) [ MachineInteger src ] [ MachineInteger dst ] (x: src): Result dst := --- if h: MachineInteger.val x < MachineInteger.size dst then --- .ret (MachineInteger.ofNatCore (MachineInteger.val x).val h) --- else --- .fail integerOverflow - -------------- --- VECTORS -- -------------- - -def Vec (α : Type u) := { l : List α // List.length l <= Usize.max } - -def vec_new (α : Type u): Vec α := ⟨ [], by sorry ⟩ - -def vec_len (α : Type u) (v : Vec α) : Usize := - let ⟨ v, l ⟩ := v - Usize.ofIntCore (List.length v) (by sorry) l - -def vec_push_fwd (α : Type u) (_ : Vec α) (_ : α) : Unit := () - -def vec_push_back (α : Type u) (v : Vec α) (x : α) : Result (Vec α) - := - if h : List.length v.val <= U32.max || List.length v.val <= Usize.max then - return ⟨ List.concat v.val x, by sorry ⟩ - else - fail maximumSizeExceeded - -def vec_insert_fwd (α : Type u) (v: Vec α) (i: Usize) (_: α): Result Unit := - if i.val < List.length v.val then - .ret () - else - .fail arrayOutOfBounds - -def vec_insert_back (α : Type u) (v: Vec α) (i: Usize) (x: α): Result (Vec α) := - if i.val < List.length v.val then - -- TODO: maybe we should redefine a list library which uses integers - -- (instead of natural numbers) - let i : Nat := - match i.val with - | .ofNat n => n - | .negSucc n => by sorry -- TODO: we can't get here - let isLt: i < USize.size := by sorry - let i : Fin USize.size := { val := i, isLt := isLt } - .ret ⟨ List.set v.val i.val x, by - have h: List.length v.val <= Usize.max := v.property - rewrite [ List.length_set v.val i.val x ] - assumption - ⟩ - else - .fail arrayOutOfBounds - -def vec_index_fwd (α : Type u) (v: Vec α) (i: Usize): Result α := - if i.val < List.length v.val then - let i : Nat := - match i.val with - | .ofNat n => n - | .negSucc n => by sorry -- TODO: we can't get here - let isLt: i < USize.size := by sorry - let i : Fin USize.size := { val := i, isLt := isLt } - let h: i < List.length v.val := by sorry - .ret (List.get v.val ⟨i.val, h⟩) - else - .fail arrayOutOfBounds - -def vec_index_back (α : Type u) (v: Vec α) (i: Usize) (_: α): Result Unit := - if i.val < List.length v.val then - .ret () - else - .fail arrayOutOfBounds - -def vec_index_mut_fwd (α : Type u) (v: Vec α) (i: Usize): Result α := - if i.val < List.length v.val then - let i : Nat := - match i.val with - | .ofNat n => n - | .negSucc n => by sorry -- TODO: we can't get here - let isLt: i < USize.size := by sorry - let i : Fin USize.size := { val := i, isLt := isLt } - let h: i < List.length v.val := by sorry - .ret (List.get v.val ⟨i.val, h⟩) - else - .fail arrayOutOfBounds - -def vec_index_mut_back (α : Type u) (v: Vec α) (i: Usize) (x: α): Result (Vec α) := - if i.val < List.length v.val then - let i : Nat := - match i.val with - | .ofNat n => n - | .negSucc n => by sorry -- TODO: we can't get here - let isLt: i < USize.size := by sorry - let i : Fin USize.size := { val := i, isLt := isLt } - .ret ⟨ List.set v.val i.val x, by - have h: List.length v.val <= Usize.max := v.property - rewrite [ List.length_set v.val i.val x ] - assumption - ⟩ - else - .fail arrayOutOfBounds - ----------- --- MISC -- ----------- - -def mem_replace_fwd (a : Type) (x : a) (_ : a) : a := - x - -def mem_replace_back (a : Type) (_ : a) (y : a) : a := - y - -/-- Aeneas-translated function -- useful to reduce non-recursive definitions. - Use with `simp [ aeneas ]` -/ -register_simp_attr aeneas diff --git a/tests/lean/misc-no_nested_borrows/NoNestedBorrows.lean b/tests/lean/misc-no_nested_borrows/NoNestedBorrows.lean deleted file mode 100644 index 12c7d8f7..00000000 --- a/tests/lean/misc-no_nested_borrows/NoNestedBorrows.lean +++ /dev/null @@ -1,538 +0,0 @@ --- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS --- [no_nested_borrows] -import Base.Primitives - -/- [no_nested_borrows::Pair] -/ -structure pair_t (T1 T2 : Type) where - pair_x : T1 - pair_y : T2 - -/- [no_nested_borrows::List] -/ -inductive list_t (T : Type) := -| Cons : T -> list_t T -> list_t T -| Nil : list_t T - -/- [no_nested_borrows::One] -/ -inductive one_t (T1 : Type) := -| One : T1 -> one_t T1 - -/- [no_nested_borrows::EmptyEnum] -/ -inductive empty_enum_t := -| Empty : empty_enum_t - -/- [no_nested_borrows::Enum] -/ -inductive enum_t := -| Variant1 : enum_t -| Variant2 : enum_t - -/- [no_nested_borrows::EmptyStruct] -/ -structure empty_struct_t where - -/- [no_nested_borrows::Sum] -/ -inductive sum_t (T1 T2 : Type) := -| Left : T1 -> sum_t T1 T2 -| Right : T2 -> sum_t T1 T2 - -/- [no_nested_borrows::neg_test] -/ -def neg_test_fwd (x : I32) : Result I32 := - - x - -/- [no_nested_borrows::add_test] -/ -def add_test_fwd (x : U32) (y : U32) : Result U32 := - x + y - -/- [no_nested_borrows::subs_test] -/ -def subs_test_fwd (x : U32) (y : U32) : Result U32 := - x - y - -/- [no_nested_borrows::div_test] -/ -def div_test_fwd (x : U32) (y : U32) : Result U32 := - x / y - -/- [no_nested_borrows::div_test1] -/ -def div_test1_fwd (x : U32) : Result U32 := - x / (U32.ofInt 2 (by intlit)) - -/- [no_nested_borrows::rem_test] -/ -def rem_test_fwd (x : U32) (y : U32) : Result U32 := - x % y - -/- [no_nested_borrows::cast_test] -/ -def cast_test_fwd (x : U32) : Result I32 := - Scalar.cast .I32 x - -/- [no_nested_borrows::test2] -/ -def test2_fwd : Result Unit := - do - let _ ← (U32.ofInt 23 (by intlit)) + (U32.ofInt 44 (by intlit)) - Result.ret () - -/- Unit test for [no_nested_borrows::test2] -/ -#assert (test2_fwd == .ret ()) - -/- [no_nested_borrows::get_max] -/ -def get_max_fwd (x : U32) (y : U32) : Result U32 := - if h: x >= y - then Result.ret x - else Result.ret y - -/- [no_nested_borrows::test3] -/ -def test3_fwd : Result Unit := - do - let x ← get_max_fwd (U32.ofInt 4 (by intlit)) (U32.ofInt 3 (by intlit)) - let y ← get_max_fwd (U32.ofInt 10 (by intlit)) (U32.ofInt 11 (by intlit)) - let z ← x + y - if h: not (z = (U32.ofInt 15 (by intlit))) - then Result.fail Error.panic - else Result.ret () - -/- Unit test for [no_nested_borrows::test3] -/ -#assert (test3_fwd == .ret ()) - -/- [no_nested_borrows::test_neg1] -/ -def test_neg1_fwd : Result Unit := - do - let y ← - (I32.ofInt 3 (by intlit)) - if h: not (y = (I32.ofInt (-(3:Int)) (by intlit))) - then Result.fail Error.panic - else Result.ret () - -/- Unit test for [no_nested_borrows::test_neg1] -/ -#assert (test_neg1_fwd == .ret ()) - -/- [no_nested_borrows::refs_test1] -/ -def refs_test1_fwd : Result Unit := - if h: not ((I32.ofInt 1 (by intlit)) = (I32.ofInt 1 (by intlit))) - then Result.fail Error.panic - else Result.ret () - -/- Unit test for [no_nested_borrows::refs_test1] -/ -#assert (refs_test1_fwd == .ret ()) - -/- [no_nested_borrows::refs_test2] -/ -def refs_test2_fwd : Result Unit := - if h: not ((I32.ofInt 2 (by intlit)) = (I32.ofInt 2 (by intlit))) - then Result.fail Error.panic - else - if h: not ((I32.ofInt 0 (by intlit)) = (I32.ofInt 0 (by intlit))) - then Result.fail Error.panic - else - if h: not ((I32.ofInt 2 (by intlit)) = (I32.ofInt 2 (by intlit))) - then Result.fail Error.panic - else - if h: not ((I32.ofInt 2 (by intlit)) = (I32.ofInt 2 (by intlit))) - then Result.fail Error.panic - else Result.ret () - -/- Unit test for [no_nested_borrows::refs_test2] -/ -#assert (refs_test2_fwd == .ret ()) - -/- [no_nested_borrows::test_list1] -/ -def test_list1_fwd : Result Unit := - Result.ret () - -/- Unit test for [no_nested_borrows::test_list1] -/ -#assert (test_list1_fwd == .ret ()) - -/- [no_nested_borrows::test_box1] -/ -def test_box1_fwd : Result Unit := - let b := (I32.ofInt 1 (by intlit)) - let x := b - if h: not (x = (I32.ofInt 1 (by intlit))) - then Result.fail Error.panic - else Result.ret () - -/- Unit test for [no_nested_borrows::test_box1] -/ -#assert (test_box1_fwd == .ret ()) - -/- [no_nested_borrows::copy_int] -/ -def copy_int_fwd (x : I32) : Result I32 := - Result.ret x - -/- [no_nested_borrows::test_unreachable] -/ -def test_unreachable_fwd (b : Bool) : Result Unit := - if h: b - then Result.fail Error.panic - else Result.ret () - -/- [no_nested_borrows::test_panic] -/ -def test_panic_fwd (b : Bool) : Result Unit := - if h: b - then Result.fail Error.panic - else Result.ret () - -/- [no_nested_borrows::test_copy_int] -/ -def test_copy_int_fwd : Result Unit := - do - let y ← copy_int_fwd (I32.ofInt 0 (by intlit)) - if h: not ((I32.ofInt 0 (by intlit)) = y) - then Result.fail Error.panic - else Result.ret () - -/- Unit test for [no_nested_borrows::test_copy_int] -/ -#assert (test_copy_int_fwd == .ret ()) - -/- [no_nested_borrows::is_cons] -/ -def is_cons_fwd (T : Type) (l : list_t T) : Result Bool := - match h: l with - | list_t.Cons t l0 => Result.ret true - | list_t.Nil => Result.ret false - -/- [no_nested_borrows::test_is_cons] -/ -def test_is_cons_fwd : Result Unit := - do - let l := list_t.Nil - let b ← is_cons_fwd I32 (list_t.Cons (I32.ofInt 0 (by intlit)) l) - if h: not b - then Result.fail Error.panic - else Result.ret () - -/- Unit test for [no_nested_borrows::test_is_cons] -/ -#assert (test_is_cons_fwd == .ret ()) - -/- [no_nested_borrows::split_list] -/ -def split_list_fwd (T : Type) (l : list_t T) : Result (T × (list_t T)) := - match h: l with - | list_t.Cons hd tl => Result.ret (hd, tl) - | list_t.Nil => Result.fail Error.panic - -/- [no_nested_borrows::test_split_list] -/ -def test_split_list_fwd : Result Unit := - do - let l := list_t.Nil - let p ← split_list_fwd I32 (list_t.Cons (I32.ofInt 0 (by intlit)) l) - let (hd, _) := p - if h: not (hd = (I32.ofInt 0 (by intlit))) - then Result.fail Error.panic - else Result.ret () - -/- Unit test for [no_nested_borrows::test_split_list] -/ -#assert (test_split_list_fwd == .ret ()) - -/- [no_nested_borrows::choose] -/ -def choose_fwd (T : Type) (b : Bool) (x : T) (y : T) : Result T := - if h: b - then Result.ret x - else Result.ret y - -/- [no_nested_borrows::choose] -/ -def choose_back - (T : Type) (b : Bool) (x : T) (y : T) (ret0 : T) : Result (T × T) := - if h: b - then Result.ret (ret0, y) - else Result.ret (x, ret0) - -/- [no_nested_borrows::choose_test] -/ -def choose_test_fwd : Result Unit := - do - let z ← - choose_fwd I32 true (I32.ofInt 0 (by intlit)) (I32.ofInt 0 (by intlit)) - let z0 ← z + (I32.ofInt 1 (by intlit)) - if h: not (z0 = (I32.ofInt 1 (by intlit))) - then Result.fail Error.panic - else - do - let (x, y) ← - choose_back I32 true (I32.ofInt 0 (by intlit)) - (I32.ofInt 0 (by intlit)) z0 - if h: not (x = (I32.ofInt 1 (by intlit))) - then Result.fail Error.panic - else - if h: not (y = (I32.ofInt 0 (by intlit))) - then Result.fail Error.panic - else Result.ret () - -/- Unit test for [no_nested_borrows::choose_test] -/ -#assert (choose_test_fwd == .ret ()) - -/- [no_nested_borrows::test_char] -/ -def test_char_fwd : Result Char := - Result.ret 'a' - -mutual - -/- [no_nested_borrows::NodeElem] -/ -inductive node_elem_t (T : Type) := -| Cons : tree_t T -> node_elem_t T -> node_elem_t T -| Nil : node_elem_t T - -/- [no_nested_borrows::Tree] -/ -inductive tree_t (T : Type) := -| Leaf : T -> tree_t T -| Node : T -> node_elem_t T -> tree_t T -> tree_t T - -end - -/- [no_nested_borrows::list_length] -/ -def list_length_fwd (T : Type) (l : list_t T) : Result U32 := - match h: l with - | list_t.Cons t l1 => - do - let i ← list_length_fwd T l1 - (U32.ofInt 1 (by intlit)) + i - | list_t.Nil => Result.ret (U32.ofInt 0 (by intlit)) - -/- [no_nested_borrows::list_nth_shared] -/ -def list_nth_shared_fwd (T : Type) (l : list_t T) (i : U32) : Result T := - match h: l with - | list_t.Cons x tl => - if h: i = (U32.ofInt 0 (by intlit)) - then Result.ret x - else - do - let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_shared_fwd T tl i0 - | list_t.Nil => Result.fail Error.panic - -/- [no_nested_borrows::list_nth_mut] -/ -def list_nth_mut_fwd (T : Type) (l : list_t T) (i : U32) : Result T := - match h: l with - | list_t.Cons x tl => - if h: i = (U32.ofInt 0 (by intlit)) - then Result.ret x - else do - let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_mut_fwd T tl i0 - | list_t.Nil => Result.fail Error.panic - -/- [no_nested_borrows::list_nth_mut] -/ -def list_nth_mut_back - (T : Type) (l : list_t T) (i : U32) (ret0 : T) : Result (list_t T) := - match h: l with - | list_t.Cons x tl => - if h: i = (U32.ofInt 0 (by intlit)) - then Result.ret (list_t.Cons ret0 tl) - else - do - let i0 ← i - (U32.ofInt 1 (by intlit)) - let tl0 ← list_nth_mut_back T tl i0 ret0 - Result.ret (list_t.Cons x tl0) - | list_t.Nil => Result.fail Error.panic - -/- [no_nested_borrows::list_rev_aux] -/ -def list_rev_aux_fwd - (T : Type) (li : list_t T) (lo : list_t T) : Result (list_t T) := - match h: li with - | list_t.Cons hd tl => list_rev_aux_fwd T tl (list_t.Cons hd lo) - | list_t.Nil => Result.ret lo - -/- [no_nested_borrows::list_rev] -/ -def list_rev_fwd_back (T : Type) (l : list_t T) : Result (list_t T) := - let li := mem_replace_fwd (list_t T) l list_t.Nil - list_rev_aux_fwd T li list_t.Nil - -/- [no_nested_borrows::test_list_functions] -/ -def test_list_functions_fwd : Result Unit := - do - let l := list_t.Nil - let l0 := list_t.Cons (I32.ofInt 2 (by intlit)) l - let l1 := list_t.Cons (I32.ofInt 1 (by intlit)) l0 - let i ← list_length_fwd I32 (list_t.Cons (I32.ofInt 0 (by intlit)) l1) - if h: not (i = (U32.ofInt 3 (by intlit))) - then Result.fail Error.panic - else - do - let i0 ← - list_nth_shared_fwd I32 (list_t.Cons (I32.ofInt 0 (by intlit)) l1) - (U32.ofInt 0 (by intlit)) - if h: not (i0 = (I32.ofInt 0 (by intlit))) - then Result.fail Error.panic - else - do - let i1 ← - list_nth_shared_fwd I32 (list_t.Cons (I32.ofInt 0 (by intlit)) - l1) (U32.ofInt 1 (by intlit)) - if h: not (i1 = (I32.ofInt 1 (by intlit))) - then Result.fail Error.panic - else - do - let i2 ← - list_nth_shared_fwd I32 (list_t.Cons - (I32.ofInt 0 (by intlit)) l1) (U32.ofInt 2 (by intlit)) - if h: not (i2 = (I32.ofInt 2 (by intlit))) - then Result.fail Error.panic - else - do - let ls ← - list_nth_mut_back I32 (list_t.Cons - (I32.ofInt 0 (by intlit)) l1) (U32.ofInt 1 (by intlit)) - (I32.ofInt 3 (by intlit)) - let i3 ← - list_nth_shared_fwd I32 ls (U32.ofInt 0 (by intlit)) - if h: not (i3 = (I32.ofInt 0 (by intlit))) - then Result.fail Error.panic - else - do - let i4 ← - list_nth_shared_fwd I32 ls (U32.ofInt 1 (by intlit)) - if h: not (i4 = (I32.ofInt 3 (by intlit))) - then Result.fail Error.panic - else - do - let i5 ← - list_nth_shared_fwd I32 ls - (U32.ofInt 2 (by intlit)) - if h: not (i5 = (I32.ofInt 2 (by intlit))) - then Result.fail Error.panic - else Result.ret () - -/- Unit test for [no_nested_borrows::test_list_functions] -/ -#assert (test_list_functions_fwd == .ret ()) - -/- [no_nested_borrows::id_mut_pair1] -/ -def id_mut_pair1_fwd (T1 T2 : Type) (x : T1) (y : T2) : Result (T1 × T2) := - Result.ret (x, y) - -/- [no_nested_borrows::id_mut_pair1] -/ -def id_mut_pair1_back - (T1 T2 : Type) (x : T1) (y : T2) (ret0 : (T1 × T2)) : Result (T1 × T2) := - let (t, t0) := ret0 - Result.ret (t, t0) - -/- [no_nested_borrows::id_mut_pair2] -/ -def id_mut_pair2_fwd (T1 T2 : Type) (p : (T1 × T2)) : Result (T1 × T2) := - let (t, t0) := p - Result.ret (t, t0) - -/- [no_nested_borrows::id_mut_pair2] -/ -def id_mut_pair2_back - (T1 T2 : Type) (p : (T1 × T2)) (ret0 : (T1 × T2)) : Result (T1 × T2) := - let (t, t0) := ret0 - Result.ret (t, t0) - -/- [no_nested_borrows::id_mut_pair3] -/ -def id_mut_pair3_fwd (T1 T2 : Type) (x : T1) (y : T2) : Result (T1 × T2) := - Result.ret (x, y) - -/- [no_nested_borrows::id_mut_pair3] -/ -def id_mut_pair3_back'a - (T1 T2 : Type) (x : T1) (y : T2) (ret0 : T1) : Result T1 := - Result.ret ret0 - -/- [no_nested_borrows::id_mut_pair3] -/ -def id_mut_pair3_back'b - (T1 T2 : Type) (x : T1) (y : T2) (ret0 : T2) : Result T2 := - Result.ret ret0 - -/- [no_nested_borrows::id_mut_pair4] -/ -def id_mut_pair4_fwd (T1 T2 : Type) (p : (T1 × T2)) : Result (T1 × T2) := - let (t, t0) := p - Result.ret (t, t0) - -/- [no_nested_borrows::id_mut_pair4] -/ -def id_mut_pair4_back'a - (T1 T2 : Type) (p : (T1 × T2)) (ret0 : T1) : Result T1 := - Result.ret ret0 - -/- [no_nested_borrows::id_mut_pair4] -/ -def id_mut_pair4_back'b - (T1 T2 : Type) (p : (T1 × T2)) (ret0 : T2) : Result T2 := - Result.ret ret0 - -/- [no_nested_borrows::StructWithTuple] -/ -structure struct_with_tuple_t (T1 T2 : Type) where - struct_with_tuple_p : (T1 × T2) - -/- [no_nested_borrows::new_tuple1] -/ -def new_tuple1_fwd : Result (struct_with_tuple_t U32 U32) := - Result.ret - { - struct_with_tuple_p := - ((U32.ofInt 1 (by intlit)), (U32.ofInt 2 (by intlit))) - } - -/- [no_nested_borrows::new_tuple2] -/ -def new_tuple2_fwd : Result (struct_with_tuple_t I16 I16) := - Result.ret - { - struct_with_tuple_p := - ((I16.ofInt 1 (by intlit)), (I16.ofInt 2 (by intlit))) - } - -/- [no_nested_borrows::new_tuple3] -/ -def new_tuple3_fwd : Result (struct_with_tuple_t U64 I64) := - Result.ret - { - struct_with_tuple_p := - ((U64.ofInt 1 (by intlit)), (I64.ofInt 2 (by intlit))) - } - -/- [no_nested_borrows::StructWithPair] -/ -structure struct_with_pair_t (T1 T2 : Type) where - struct_with_pair_p : pair_t T1 T2 - -/- [no_nested_borrows::new_pair1] -/ -def new_pair1_fwd : Result (struct_with_pair_t U32 U32) := - Result.ret - { - struct_with_pair_p := - { - pair_x := (U32.ofInt 1 (by intlit)), - pair_y := (U32.ofInt 2 (by intlit)) - } - } - -/- [no_nested_borrows::test_constants] -/ -def test_constants_fwd : Result Unit := - do - let swt ← new_tuple1_fwd - let (i, _) := swt.struct_with_tuple_p - if h: not (i = (U32.ofInt 1 (by intlit))) - then Result.fail Error.panic - else - do - let swt0 ← new_tuple2_fwd - let (i0, _) := swt0.struct_with_tuple_p - if h: not (i0 = (I16.ofInt 1 (by intlit))) - then Result.fail Error.panic - else - do - let swt1 ← new_tuple3_fwd - let (i1, _) := swt1.struct_with_tuple_p - if h: not (i1 = (U64.ofInt 1 (by intlit))) - then Result.fail Error.panic - else - do - let swp ← new_pair1_fwd - if h: not (swp.struct_with_pair_p.pair_x = - (U32.ofInt 1 (by intlit))) - then Result.fail Error.panic - else Result.ret () - -/- Unit test for [no_nested_borrows::test_constants] -/ -#assert (test_constants_fwd == .ret ()) - -/- [no_nested_borrows::test_weird_borrows1] -/ -def test_weird_borrows1_fwd : Result Unit := - Result.ret () - -/- Unit test for [no_nested_borrows::test_weird_borrows1] -/ -#assert (test_weird_borrows1_fwd == .ret ()) - -/- [no_nested_borrows::test_mem_replace] -/ -def test_mem_replace_fwd_back (px : U32) : Result U32 := - let y := mem_replace_fwd U32 px (U32.ofInt 1 (by intlit)) - if h: not (y = (U32.ofInt 0 (by intlit))) - then Result.fail Error.panic - else Result.ret (U32.ofInt 2 (by intlit)) - -/- [no_nested_borrows::test_shared_borrow_bool1] -/ -def test_shared_borrow_bool1_fwd (b : Bool) : Result U32 := - if h: b - then Result.ret (U32.ofInt 0 (by intlit)) - else Result.ret (U32.ofInt 1 (by intlit)) - -/- [no_nested_borrows::test_shared_borrow_bool2] -/ -def test_shared_borrow_bool2_fwd : Result U32 := - Result.ret (U32.ofInt 0 (by intlit)) - -/- [no_nested_borrows::test_shared_borrow_enum1] -/ -def test_shared_borrow_enum1_fwd (l : list_t U32) : Result U32 := - match h: l with - | list_t.Cons i l0 => Result.ret (U32.ofInt 1 (by intlit)) - | list_t.Nil => Result.ret (U32.ofInt 0 (by intlit)) - -/- [no_nested_borrows::test_shared_borrow_enum2] -/ -def test_shared_borrow_enum2_fwd : Result U32 := - Result.ret (U32.ofInt 0 (by intlit)) - diff --git a/tests/lean/misc-no_nested_borrows/lake-manifest.json b/tests/lean/misc-no_nested_borrows/lake-manifest.json deleted file mode 100644 index 57b071ca..00000000 --- a/tests/lean/misc-no_nested_borrows/lake-manifest.json +++ /dev/null @@ -1,27 +0,0 @@ -{"version": 4, - "packagesDir": "./lake-packages", - "packages": - [{"git": - {"url": "https://github.com/leanprover-community/mathlib4.git", - "subDir?": null, - "rev": "4037792ead804d7bfa8868e2c4684d4223c15ece", - "name": "mathlib", - "inputRev?": null}}, - {"git": - {"url": "https://github.com/gebner/quote4", - "subDir?": null, - "rev": "2412c4fdf4a8b689f4467618e5e7b371ae5014aa", - "name": "Qq", - "inputRev?": "master"}}, - {"git": - {"url": "https://github.com/JLimperg/aesop", - "subDir?": null, - "rev": "7fe9ecd9339b0e1796e89d243b776849c305c690", - "name": "aesop", - "inputRev?": "master"}}, - {"git": - {"url": "https://github.com/leanprover/std4", - "subDir?": null, - "rev": "24897887905b3a1254b244369f5dd2cf6174b0ee", - "name": "std", - "inputRev?": "main"}}]} diff --git a/tests/lean/misc-no_nested_borrows/lakefile.lean b/tests/lean/misc-no_nested_borrows/lakefile.lean deleted file mode 100644 index 58619110..00000000 --- a/tests/lean/misc-no_nested_borrows/lakefile.lean +++ /dev/null @@ -1,12 +0,0 @@ -import Lake -open Lake DSL - -require mathlib from git - "https://github.com/leanprover-community/mathlib4.git" - -package «no_nested_borrows» {} - -lean_lib «Base» {} - -@[default_target] -lean_lib «NoNestedBorrows» {} diff --git a/tests/lean/misc-no_nested_borrows/lean-toolchain b/tests/lean/misc-no_nested_borrows/lean-toolchain deleted file mode 100644 index bbf57f10..00000000 --- a/tests/lean/misc-no_nested_borrows/lean-toolchain +++ /dev/null @@ -1 +0,0 @@ -leanprover/lean4:nightly-2023-01-21 diff --git a/tests/lean/misc-paper/Base/Primitives.lean b/tests/lean/misc-paper/Base/Primitives.lean deleted file mode 100644 index 4a66a453..00000000 --- a/tests/lean/misc-paper/Base/Primitives.lean +++ /dev/null @@ -1,583 +0,0 @@ -import Lean -import Lean.Meta.Tactic.Simp -import Init.Data.List.Basic -import Mathlib.Tactic.RunCmd - --------------------- --- ASSERT COMMAND -- --------------------- - -open Lean Elab Command Term Meta - -syntax (name := assert) "#assert" term: command - -@[command_elab assert] -unsafe -def assertImpl : CommandElab := fun (_stx: Syntax) => do - runTermElabM (fun _ => do - let r ← evalTerm Bool (mkConst ``Bool) _stx[1] - if not r then - logInfo "Assertion failed for: " - logInfo _stx[1] - logError "Expression reduced to false" - pure ()) - -#eval 2 == 2 -#assert (2 == 2) - -------------- --- PRELUDE -- -------------- - --- Results & monadic combinators - -inductive Error where - | assertionFailure: Error - | integerOverflow: Error - | divisionByZero: Error - | arrayOutOfBounds: Error - | maximumSizeExceeded: Error - | panic: Error -deriving Repr, BEq - -open Error - -inductive Result (α : Type u) where - | ret (v: α): Result α - | fail (e: Error): Result α -deriving Repr, BEq - -open Result - -instance Result_Inhabited (α : Type u) : Inhabited (Result α) := - Inhabited.mk (fail panic) - -/- HELPERS -/ - -def ret? {α: Type} (r: Result α): Bool := - match r with - | Result.ret _ => true - | Result.fail _ => false - -def massert (b:Bool) : Result Unit := - if b then .ret () else fail assertionFailure - -def eval_global {α: Type} (x: Result α) (_: ret? x): α := - match x with - | Result.fail _ => by contradiction - | Result.ret x => x - -/- DO-DSL SUPPORT -/ - -def bind (x: Result α) (f: α -> Result β) : Result β := - match x with - | ret v => f v - | fail v => fail v - --- Allows using Result in do-blocks -instance : Bind Result where - bind := bind - --- Allows using return x in do-blocks -instance : Pure Result where - pure := fun x => ret x - -/- CUSTOM-DSL SUPPORT -/ - --- Let-binding the Result of a monadic operation is oftentimes not sufficient, --- because we may need a hypothesis for equational reasoning in the scope. We --- rely on subtype, and a custom let-binding operator, in effect recreating our --- own variant of the do-dsl - -def Result.attach {α: Type} (o : Result α): Result { x : α // o = ret x } := - match o with - | .ret x => .ret ⟨x, rfl⟩ - | .fail e => .fail e - -macro "let" e:term " ⟵ " f:term : doElem => - `(doElem| let ⟨$e, h⟩ ← Result.attach $f) - --- TODO: any way to factorize both definitions? -macro "let" e:term " <-- " f:term : doElem => - `(doElem| let ⟨$e, h⟩ ← Result.attach $f) - --- We call the hypothesis `h`, in effect making it unavailable to the user --- (because too much shadowing). But in practice, once can use the French single --- quote notation (input with f< and f>), where `‹ h ›` finds a suitable --- hypothesis in the context, this is equivalent to `have x: h := by assumption in x` -#eval do - let y <-- .ret (0: Nat) - let _: y = 0 := by cases ‹ ret 0 = ret y › ; decide - let r: { x: Nat // x = 0 } := ⟨ y, by assumption ⟩ - .ret r - ----------------------- --- MACHINE INTEGERS -- ----------------------- - --- We redefine our machine integers types. - --- For Isize/Usize, we reuse `getNumBits` from `USize`. You cannot reduce `getNumBits` --- using the simplifier, meaning that proofs do not depend on the compile-time value of --- USize.size. (Lean assumes 32 or 64-bit platforms, and Rust doesn't really support, at --- least officially, 16-bit microcontrollers, so this seems like a fine design decision --- for now.) - --- Note from Chris Bailey: "If there's more than one salient property of your --- definition then the subtyping strategy might get messy, and the property part --- of a subtype is less discoverable by the simplifier or tactics like --- library_search." So, we will not add refinements on the return values of the --- operations defined on Primitives, but will rather rely on custom lemmas to --- invert on possible return values of the primitive operations. - --- Machine integer constants, done via `ofNatCore`, which requires a proof that --- the `Nat` fits within the desired integer type. We provide a custom tactic. - -open System.Platform.getNumBits - --- TODO: is there a way of only importing System.Platform.getNumBits? --- -@[simp] def size_num_bits : Nat := (System.Platform.getNumBits ()).val - --- Remark: Lean seems to use < for the comparisons with the upper bounds by convention. --- We keep the F* convention for now. -@[simp] def Isize.min : Int := - (HPow.hPow 2 (size_num_bits - 1)) -@[simp] def Isize.max : Int := (HPow.hPow 2 (size_num_bits - 1)) - 1 -@[simp] def I8.min : Int := - (HPow.hPow 2 7) -@[simp] def I8.max : Int := HPow.hPow 2 7 - 1 -@[simp] def I16.min : Int := - (HPow.hPow 2 15) -@[simp] def I16.max : Int := HPow.hPow 2 15 - 1 -@[simp] def I32.min : Int := -(HPow.hPow 2 31) -@[simp] def I32.max : Int := HPow.hPow 2 31 - 1 -@[simp] def I64.min : Int := -(HPow.hPow 2 63) -@[simp] def I64.max : Int := HPow.hPow 2 63 - 1 -@[simp] def I128.min : Int := -(HPow.hPow 2 127) -@[simp] def I128.max : Int := HPow.hPow 2 127 - 1 -@[simp] def Usize.min : Int := 0 -@[simp] def Usize.max : Int := HPow.hPow 2 size_num_bits - 1 -@[simp] def U8.min : Int := 0 -@[simp] def U8.max : Int := HPow.hPow 2 8 - 1 -@[simp] def U16.min : Int := 0 -@[simp] def U16.max : Int := HPow.hPow 2 16 - 1 -@[simp] def U32.min : Int := 0 -@[simp] def U32.max : Int := HPow.hPow 2 32 - 1 -@[simp] def U64.min : Int := 0 -@[simp] def U64.max : Int := HPow.hPow 2 64 - 1 -@[simp] def U128.min : Int := 0 -@[simp] def U128.max : Int := HPow.hPow 2 128 - 1 - -#assert (I8.min == -128) -#assert (I8.max == 127) -#assert (I16.min == -32768) -#assert (I16.max == 32767) -#assert (I32.min == -2147483648) -#assert (I32.max == 2147483647) -#assert (I64.min == -9223372036854775808) -#assert (I64.max == 9223372036854775807) -#assert (I128.min == -170141183460469231731687303715884105728) -#assert (I128.max == 170141183460469231731687303715884105727) -#assert (U8.min == 0) -#assert (U8.max == 255) -#assert (U16.min == 0) -#assert (U16.max == 65535) -#assert (U32.min == 0) -#assert (U32.max == 4294967295) -#assert (U64.min == 0) -#assert (U64.max == 18446744073709551615) -#assert (U128.min == 0) -#assert (U128.max == 340282366920938463463374607431768211455) - -inductive ScalarTy := -| Isize -| I8 -| I16 -| I32 -| I64 -| I128 -| Usize -| U8 -| U16 -| U32 -| U64 -| U128 - -def Scalar.min (ty : ScalarTy) : Int := - match ty with - | .Isize => Isize.min - | .I8 => I8.min - | .I16 => I16.min - | .I32 => I32.min - | .I64 => I64.min - | .I128 => I128.min - | .Usize => Usize.min - | .U8 => U8.min - | .U16 => U16.min - | .U32 => U32.min - | .U64 => U64.min - | .U128 => U128.min - -def Scalar.max (ty : ScalarTy) : Int := - match ty with - | .Isize => Isize.max - | .I8 => I8.max - | .I16 => I16.max - | .I32 => I32.max - | .I64 => I64.max - | .I128 => I128.max - | .Usize => Usize.max - | .U8 => U8.max - | .U16 => U16.max - | .U32 => U32.max - | .U64 => U64.max - | .U128 => U128.max - --- "Conservative" bounds --- We use those because we can't compare to the isize bounds (which can't --- reduce at compile-time). Whenever we perform an arithmetic operation like --- addition we need to check that the result is in bounds: we first compare --- to the conservative bounds, which reduce, then compare to the real bounds. --- This is useful for the various #asserts that we want to reduce at --- type-checking time. -def Scalar.cMin (ty : ScalarTy) : Int := - match ty with - | .Isize => I32.min - | _ => Scalar.min ty - -def Scalar.cMax (ty : ScalarTy) : Int := - match ty with - | .Isize => I32.max - | .Usize => U32.max - | _ => Scalar.max ty - -theorem Scalar.cMin_bound ty : Scalar.min ty <= Scalar.cMin ty := by sorry -theorem Scalar.cMax_bound ty : Scalar.min ty <= Scalar.cMin ty := by sorry - -structure Scalar (ty : ScalarTy) where - val : Int - hmin : Scalar.min ty <= val - hmax : val <= Scalar.max ty - -theorem Scalar.bound_suffices (ty : ScalarTy) (x : Int) : - Scalar.cMin ty <= x && x <= Scalar.cMax ty -> - (decide (Scalar.min ty ≤ x) && decide (x ≤ Scalar.max ty)) = true - := by sorry - -def Scalar.ofIntCore {ty : ScalarTy} (x : Int) - (hmin : Scalar.min ty <= x) (hmax : x <= Scalar.max ty) : Scalar ty := - { val := x, hmin := hmin, hmax := hmax } - -def Scalar.ofInt {ty : ScalarTy} (x : Int) - (h : Scalar.min ty <= x && x <= Scalar.max ty) : Scalar ty := - let hmin: Scalar.min ty <= x := by sorry - let hmax: x <= Scalar.max ty := by sorry - Scalar.ofIntCore x hmin hmax - --- Further thoughts: look at what has been done here: --- https://github.com/leanprover-community/mathlib4/blob/master/Mathlib/Data/Fin/Basic.lean --- and --- https://github.com/leanprover-community/mathlib4/blob/master/Mathlib/Data/UInt.lean --- which both contain a fair amount of reasoning already! -def Scalar.tryMk (ty : ScalarTy) (x : Int) : Result (Scalar ty) := - -- TODO: write this with only one if then else - if hmin_cons: Scalar.cMin ty <= x || Scalar.min ty <= x then - if hmax_cons: x <= Scalar.cMax ty || x <= Scalar.max ty then - let hmin: Scalar.min ty <= x := by sorry - let hmax: x <= Scalar.max ty := by sorry - return Scalar.ofIntCore x hmin hmax - else fail integerOverflow - else fail integerOverflow - -def Scalar.neg {ty : ScalarTy} (x : Scalar ty) : Result (Scalar ty) := Scalar.tryMk ty (- x.val) - -def Scalar.div {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - if y.val != 0 then Scalar.tryMk ty (x.val / y.val) else fail divisionByZero - --- Checking that the % operation in Lean computes the same as the remainder operation in Rust -#assert 1 % 2 = (1:Int) -#assert (-1) % 2 = -1 -#assert 1 % (-2) = 1 -#assert (-1) % (-2) = -1 - -def Scalar.rem {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - if y.val != 0 then Scalar.tryMk ty (x.val % y.val) else fail divisionByZero - -def Scalar.add {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - Scalar.tryMk ty (x.val + y.val) - -def Scalar.sub {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - Scalar.tryMk ty (x.val - y.val) - -def Scalar.mul {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - Scalar.tryMk ty (x.val * y.val) - --- TODO: instances of +, -, * etc. for scalars - --- Cast an integer from a [src_ty] to a [tgt_ty] --- TODO: check the semantics of casts in Rust -def Scalar.cast {src_ty : ScalarTy} (tgt_ty : ScalarTy) (x : Scalar src_ty) : Result (Scalar tgt_ty) := - Scalar.tryMk tgt_ty x.val - --- The scalar types --- We declare the definitions as reducible so that Lean can unfold them (useful --- for type class resolution for instance). -@[reducible] def Isize := Scalar .Isize -@[reducible] def I8 := Scalar .I8 -@[reducible] def I16 := Scalar .I16 -@[reducible] def I32 := Scalar .I32 -@[reducible] def I64 := Scalar .I64 -@[reducible] def I128 := Scalar .I128 -@[reducible] def Usize := Scalar .Usize -@[reducible] def U8 := Scalar .U8 -@[reducible] def U16 := Scalar .U16 -@[reducible] def U32 := Scalar .U32 -@[reducible] def U64 := Scalar .U64 -@[reducible] def U128 := Scalar .U128 - --- TODO: below: not sure this is the best way. --- Should we rather overload operations like +, -, etc.? --- Also, it is possible to automate the generation of those definitions --- with macros (but would it be a good idea? It would be less easy to --- read the file, which is not supposed to change a lot) - --- Negation - -/-- -Remark: there is no heterogeneous negation in the Lean prelude: we thus introduce -one here. - -The notation typeclass for heterogeneous addition. -This enables the notation `- a : β` where `a : α`. --/ -class HNeg (α : Type u) (β : outParam (Type v)) where - /-- `- a` computes the negation of `a`. - The meaning of this notation is type-dependent. -/ - hNeg : α → β - -prefix:75 "-" => HNeg.hNeg - -instance : HNeg Isize (Result Isize) where hNeg x := Scalar.neg x -instance : HNeg I8 (Result I8) where hNeg x := Scalar.neg x -instance : HNeg I16 (Result I16) where hNeg x := Scalar.neg x -instance : HNeg I32 (Result I32) where hNeg x := Scalar.neg x -instance : HNeg I64 (Result I64) where hNeg x := Scalar.neg x -instance : HNeg I128 (Result I128) where hNeg x := Scalar.neg x - --- Addition -instance {ty} : HAdd (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hAdd x y := Scalar.add x y - --- Substraction -instance {ty} : HSub (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hSub x y := Scalar.sub x y - --- Multiplication -instance {ty} : HMul (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hMul x y := Scalar.mul x y - --- Division -instance {ty} : HDiv (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hDiv x y := Scalar.div x y - --- Remainder -instance {ty} : HMod (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hMod x y := Scalar.rem x y - --- ofIntCore --- TODO: typeclass? -def Isize.ofIntCore := @Scalar.ofIntCore .Isize -def I8.ofIntCore := @Scalar.ofIntCore .I8 -def I16.ofIntCore := @Scalar.ofIntCore .I16 -def I32.ofIntCore := @Scalar.ofIntCore .I32 -def I64.ofIntCore := @Scalar.ofIntCore .I64 -def I128.ofIntCore := @Scalar.ofIntCore .I128 -def Usize.ofIntCore := @Scalar.ofIntCore .Usize -def U8.ofIntCore := @Scalar.ofIntCore .U8 -def U16.ofIntCore := @Scalar.ofIntCore .U16 -def U32.ofIntCore := @Scalar.ofIntCore .U32 -def U64.ofIntCore := @Scalar.ofIntCore .U64 -def U128.ofIntCore := @Scalar.ofIntCore .U128 - --- ofInt --- TODO: typeclass? -def Isize.ofInt := @Scalar.ofInt .Isize -def I8.ofInt := @Scalar.ofInt .I8 -def I16.ofInt := @Scalar.ofInt .I16 -def I32.ofInt := @Scalar.ofInt .I32 -def I64.ofInt := @Scalar.ofInt .I64 -def I128.ofInt := @Scalar.ofInt .I128 -def Usize.ofInt := @Scalar.ofInt .Usize -def U8.ofInt := @Scalar.ofInt .U8 -def U16.ofInt := @Scalar.ofInt .U16 -def U32.ofInt := @Scalar.ofInt .U32 -def U64.ofInt := @Scalar.ofInt .U64 -def U128.ofInt := @Scalar.ofInt .U128 - --- Comparisons -instance {ty} : LT (Scalar ty) where - lt a b := LT.lt a.val b.val - -instance {ty} : LE (Scalar ty) where le a b := LE.le a.val b.val - -instance Scalar.decLt {ty} (a b : Scalar ty) : Decidable (LT.lt a b) := Int.decLt .. -instance Scalar.decLe {ty} (a b : Scalar ty) : Decidable (LE.le a b) := Int.decLe .. - -theorem Scalar.eq_of_val_eq {ty} : ∀ {i j : Scalar ty}, Eq i.val j.val → Eq i j - | ⟨_, _, _⟩, ⟨_, _, _⟩, rfl => rfl - -theorem Scalar.val_eq_of_eq {ty} {i j : Scalar ty} (h : Eq i j) : Eq i.val j.val := - h ▸ rfl - -theorem Scalar.ne_of_val_ne {ty} {i j : Scalar ty} (h : Not (Eq i.val j.val)) : Not (Eq i j) := - fun h' => absurd (val_eq_of_eq h') h - -instance (ty : ScalarTy) : DecidableEq (Scalar ty) := - fun i j => - match decEq i.val j.val with - | isTrue h => isTrue (Scalar.eq_of_val_eq h) - | isFalse h => isFalse (Scalar.ne_of_val_ne h) - -def Scalar.toInt {ty} (n : Scalar ty) : Int := n.val - --- Tactic to prove that integers are in bounds -syntax "intlit" : tactic - -macro_rules - | `(tactic| intlit) => `(tactic| apply Scalar.bound_suffices ; decide) - --- -- We now define a type class that subsumes the various machine integer types, so --- -- as to write a concise definition for scalar_cast, rather than exhaustively --- -- enumerating all of the possible pairs. We remark that Rust has sane semantics --- -- and fails if a cast operation would involve a truncation or modulo. - --- class MachineInteger (t: Type) where --- size: Nat --- val: t -> Fin size --- ofNatCore: (n:Nat) -> LT.lt n size -> t - --- set_option hygiene false in --- run_cmd --- for typeName in [`UInt8, `UInt16, `UInt32, `UInt64, `USize].map Lean.mkIdent do --- Lean.Elab.Command.elabCommand (← `( --- namespace $typeName --- instance: MachineInteger $typeName where --- size := size --- val := val --- ofNatCore := ofNatCore --- end $typeName --- )) - --- -- Aeneas only instantiates the destination type (`src` is implicit). We rely on --- -- Lean to infer `src`. - --- def scalar_cast { src: Type } (dst: Type) [ MachineInteger src ] [ MachineInteger dst ] (x: src): Result dst := --- if h: MachineInteger.val x < MachineInteger.size dst then --- .ret (MachineInteger.ofNatCore (MachineInteger.val x).val h) --- else --- .fail integerOverflow - -------------- --- VECTORS -- -------------- - -def Vec (α : Type u) := { l : List α // List.length l <= Usize.max } - -def vec_new (α : Type u): Vec α := ⟨ [], by sorry ⟩ - -def vec_len (α : Type u) (v : Vec α) : Usize := - let ⟨ v, l ⟩ := v - Usize.ofIntCore (List.length v) (by sorry) l - -def vec_push_fwd (α : Type u) (_ : Vec α) (_ : α) : Unit := () - -def vec_push_back (α : Type u) (v : Vec α) (x : α) : Result (Vec α) - := - if h : List.length v.val <= U32.max || List.length v.val <= Usize.max then - return ⟨ List.concat v.val x, by sorry ⟩ - else - fail maximumSizeExceeded - -def vec_insert_fwd (α : Type u) (v: Vec α) (i: Usize) (_: α): Result Unit := - if i.val < List.length v.val then - .ret () - else - .fail arrayOutOfBounds - -def vec_insert_back (α : Type u) (v: Vec α) (i: Usize) (x: α): Result (Vec α) := - if i.val < List.length v.val then - -- TODO: maybe we should redefine a list library which uses integers - -- (instead of natural numbers) - let i : Nat := - match i.val with - | .ofNat n => n - | .negSucc n => by sorry -- TODO: we can't get here - let isLt: i < USize.size := by sorry - let i : Fin USize.size := { val := i, isLt := isLt } - .ret ⟨ List.set v.val i.val x, by - have h: List.length v.val <= Usize.max := v.property - rewrite [ List.length_set v.val i.val x ] - assumption - ⟩ - else - .fail arrayOutOfBounds - -def vec_index_fwd (α : Type u) (v: Vec α) (i: Usize): Result α := - if i.val < List.length v.val then - let i : Nat := - match i.val with - | .ofNat n => n - | .negSucc n => by sorry -- TODO: we can't get here - let isLt: i < USize.size := by sorry - let i : Fin USize.size := { val := i, isLt := isLt } - let h: i < List.length v.val := by sorry - .ret (List.get v.val ⟨i.val, h⟩) - else - .fail arrayOutOfBounds - -def vec_index_back (α : Type u) (v: Vec α) (i: Usize) (_: α): Result Unit := - if i.val < List.length v.val then - .ret () - else - .fail arrayOutOfBounds - -def vec_index_mut_fwd (α : Type u) (v: Vec α) (i: Usize): Result α := - if i.val < List.length v.val then - let i : Nat := - match i.val with - | .ofNat n => n - | .negSucc n => by sorry -- TODO: we can't get here - let isLt: i < USize.size := by sorry - let i : Fin USize.size := { val := i, isLt := isLt } - let h: i < List.length v.val := by sorry - .ret (List.get v.val ⟨i.val, h⟩) - else - .fail arrayOutOfBounds - -def vec_index_mut_back (α : Type u) (v: Vec α) (i: Usize) (x: α): Result (Vec α) := - if i.val < List.length v.val then - let i : Nat := - match i.val with - | .ofNat n => n - | .negSucc n => by sorry -- TODO: we can't get here - let isLt: i < USize.size := by sorry - let i : Fin USize.size := { val := i, isLt := isLt } - .ret ⟨ List.set v.val i.val x, by - have h: List.length v.val <= Usize.max := v.property - rewrite [ List.length_set v.val i.val x ] - assumption - ⟩ - else - .fail arrayOutOfBounds - ----------- --- MISC -- ----------- - -def mem_replace_fwd (a : Type) (x : a) (_ : a) : a := - x - -def mem_replace_back (a : Type) (_ : a) (y : a) : a := - y - -/-- Aeneas-translated function -- useful to reduce non-recursive definitions. - Use with `simp [ aeneas ]` -/ -register_simp_attr aeneas diff --git a/tests/lean/misc-paper/Paper.lean b/tests/lean/misc-paper/Paper.lean deleted file mode 100644 index 0b16fb8e..00000000 --- a/tests/lean/misc-paper/Paper.lean +++ /dev/null @@ -1,123 +0,0 @@ --- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS --- [paper] -import Base.Primitives - -/- [paper::ref_incr] -/ -def ref_incr_fwd_back (x : I32) : Result I32 := - x + (I32.ofInt 1 (by intlit)) - -/- [paper::test_incr] -/ -def test_incr_fwd : Result Unit := - do - let x ← ref_incr_fwd_back (I32.ofInt 0 (by intlit)) - if h: not (x = (I32.ofInt 1 (by intlit))) - then Result.fail Error.panic - else Result.ret () - -/- Unit test for [paper::test_incr] -/ -#assert (test_incr_fwd == .ret ()) - -/- [paper::choose] -/ -def choose_fwd (T : Type) (b : Bool) (x : T) (y : T) : Result T := - if h: b - then Result.ret x - else Result.ret y - -/- [paper::choose] -/ -def choose_back - (T : Type) (b : Bool) (x : T) (y : T) (ret0 : T) : Result (T × T) := - if h: b - then Result.ret (ret0, y) - else Result.ret (x, ret0) - -/- [paper::test_choose] -/ -def test_choose_fwd : Result Unit := - do - let z ← - choose_fwd I32 true (I32.ofInt 0 (by intlit)) (I32.ofInt 0 (by intlit)) - let z0 ← z + (I32.ofInt 1 (by intlit)) - if h: not (z0 = (I32.ofInt 1 (by intlit))) - then Result.fail Error.panic - else - do - let (x, y) ← - choose_back I32 true (I32.ofInt 0 (by intlit)) - (I32.ofInt 0 (by intlit)) z0 - if h: not (x = (I32.ofInt 1 (by intlit))) - then Result.fail Error.panic - else - if h: not (y = (I32.ofInt 0 (by intlit))) - then Result.fail Error.panic - else Result.ret () - -/- Unit test for [paper::test_choose] -/ -#assert (test_choose_fwd == .ret ()) - -/- [paper::List] -/ -inductive list_t (T : Type) := -| Cons : T -> list_t T -> list_t T -| Nil : list_t T - -/- [paper::list_nth_mut] -/ -def list_nth_mut_fwd (T : Type) (l : list_t T) (i : U32) : Result T := - match h: l with - | list_t.Cons x tl => - if h: i = (U32.ofInt 0 (by intlit)) - then Result.ret x - else do - let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_mut_fwd T tl i0 - | list_t.Nil => Result.fail Error.panic - -/- [paper::list_nth_mut] -/ -def list_nth_mut_back - (T : Type) (l : list_t T) (i : U32) (ret0 : T) : Result (list_t T) := - match h: l with - | list_t.Cons x tl => - if h: i = (U32.ofInt 0 (by intlit)) - then Result.ret (list_t.Cons ret0 tl) - else - do - let i0 ← i - (U32.ofInt 1 (by intlit)) - let tl0 ← list_nth_mut_back T tl i0 ret0 - Result.ret (list_t.Cons x tl0) - | list_t.Nil => Result.fail Error.panic - -/- [paper::sum] -/ -def sum_fwd (l : list_t I32) : Result I32 := - match h: l with - | list_t.Cons x tl => do - let i ← sum_fwd tl - x + i - | list_t.Nil => Result.ret (I32.ofInt 0 (by intlit)) - -/- [paper::test_nth] -/ -def test_nth_fwd : Result Unit := - do - let l := list_t.Nil - let l0 := list_t.Cons (I32.ofInt 3 (by intlit)) l - let l1 := list_t.Cons (I32.ofInt 2 (by intlit)) l0 - let x ← - list_nth_mut_fwd I32 (list_t.Cons (I32.ofInt 1 (by intlit)) l1) - (U32.ofInt 2 (by intlit)) - let x0 ← x + (I32.ofInt 1 (by intlit)) - let l2 ← - list_nth_mut_back I32 (list_t.Cons (I32.ofInt 1 (by intlit)) l1) - (U32.ofInt 2 (by intlit)) x0 - let i ← sum_fwd l2 - if h: not (i = (I32.ofInt 7 (by intlit))) - then Result.fail Error.panic - else Result.ret () - -/- Unit test for [paper::test_nth] -/ -#assert (test_nth_fwd == .ret ()) - -/- [paper::call_choose] -/ -def call_choose_fwd (p : (U32 × U32)) : Result U32 := - do - let (px, py) := p - let pz ← choose_fwd U32 true px py - let pz0 ← pz + (U32.ofInt 1 (by intlit)) - let (px0, _) ← choose_back U32 true px py pz0 - Result.ret px0 - diff --git a/tests/lean/misc-paper/lake-manifest.json b/tests/lean/misc-paper/lake-manifest.json deleted file mode 100644 index 57b071ca..00000000 --- a/tests/lean/misc-paper/lake-manifest.json +++ /dev/null @@ -1,27 +0,0 @@ -{"version": 4, - "packagesDir": "./lake-packages", - "packages": - [{"git": - {"url": "https://github.com/leanprover-community/mathlib4.git", - "subDir?": null, - "rev": "4037792ead804d7bfa8868e2c4684d4223c15ece", - "name": "mathlib", - "inputRev?": null}}, - {"git": - {"url": "https://github.com/gebner/quote4", - "subDir?": null, - "rev": "2412c4fdf4a8b689f4467618e5e7b371ae5014aa", - "name": "Qq", - "inputRev?": "master"}}, - {"git": - {"url": "https://github.com/JLimperg/aesop", - "subDir?": null, - "rev": "7fe9ecd9339b0e1796e89d243b776849c305c690", - "name": "aesop", - "inputRev?": "master"}}, - {"git": - {"url": "https://github.com/leanprover/std4", - "subDir?": null, - "rev": "24897887905b3a1254b244369f5dd2cf6174b0ee", - "name": "std", - "inputRev?": "main"}}]} diff --git a/tests/lean/misc-paper/lakefile.lean b/tests/lean/misc-paper/lakefile.lean deleted file mode 100644 index 75d7208e..00000000 --- a/tests/lean/misc-paper/lakefile.lean +++ /dev/null @@ -1,12 +0,0 @@ -import Lake -open Lake DSL - -require mathlib from git - "https://github.com/leanprover-community/mathlib4.git" - -package «paper» {} - -lean_lib «Base» {} - -@[default_target] -lean_lib «Paper» {} diff --git a/tests/lean/misc-paper/lean-toolchain b/tests/lean/misc-paper/lean-toolchain deleted file mode 100644 index bbf57f10..00000000 --- a/tests/lean/misc-paper/lean-toolchain +++ /dev/null @@ -1 +0,0 @@ -leanprover/lean4:nightly-2023-01-21 diff --git a/tests/lean/misc-polonius_list/Base/Primitives.lean b/tests/lean/misc-polonius_list/Base/Primitives.lean deleted file mode 100644 index 4a66a453..00000000 --- a/tests/lean/misc-polonius_list/Base/Primitives.lean +++ /dev/null @@ -1,583 +0,0 @@ -import Lean -import Lean.Meta.Tactic.Simp -import Init.Data.List.Basic -import Mathlib.Tactic.RunCmd - --------------------- --- ASSERT COMMAND -- --------------------- - -open Lean Elab Command Term Meta - -syntax (name := assert) "#assert" term: command - -@[command_elab assert] -unsafe -def assertImpl : CommandElab := fun (_stx: Syntax) => do - runTermElabM (fun _ => do - let r ← evalTerm Bool (mkConst ``Bool) _stx[1] - if not r then - logInfo "Assertion failed for: " - logInfo _stx[1] - logError "Expression reduced to false" - pure ()) - -#eval 2 == 2 -#assert (2 == 2) - -------------- --- PRELUDE -- -------------- - --- Results & monadic combinators - -inductive Error where - | assertionFailure: Error - | integerOverflow: Error - | divisionByZero: Error - | arrayOutOfBounds: Error - | maximumSizeExceeded: Error - | panic: Error -deriving Repr, BEq - -open Error - -inductive Result (α : Type u) where - | ret (v: α): Result α - | fail (e: Error): Result α -deriving Repr, BEq - -open Result - -instance Result_Inhabited (α : Type u) : Inhabited (Result α) := - Inhabited.mk (fail panic) - -/- HELPERS -/ - -def ret? {α: Type} (r: Result α): Bool := - match r with - | Result.ret _ => true - | Result.fail _ => false - -def massert (b:Bool) : Result Unit := - if b then .ret () else fail assertionFailure - -def eval_global {α: Type} (x: Result α) (_: ret? x): α := - match x with - | Result.fail _ => by contradiction - | Result.ret x => x - -/- DO-DSL SUPPORT -/ - -def bind (x: Result α) (f: α -> Result β) : Result β := - match x with - | ret v => f v - | fail v => fail v - --- Allows using Result in do-blocks -instance : Bind Result where - bind := bind - --- Allows using return x in do-blocks -instance : Pure Result where - pure := fun x => ret x - -/- CUSTOM-DSL SUPPORT -/ - --- Let-binding the Result of a monadic operation is oftentimes not sufficient, --- because we may need a hypothesis for equational reasoning in the scope. We --- rely on subtype, and a custom let-binding operator, in effect recreating our --- own variant of the do-dsl - -def Result.attach {α: Type} (o : Result α): Result { x : α // o = ret x } := - match o with - | .ret x => .ret ⟨x, rfl⟩ - | .fail e => .fail e - -macro "let" e:term " ⟵ " f:term : doElem => - `(doElem| let ⟨$e, h⟩ ← Result.attach $f) - --- TODO: any way to factorize both definitions? -macro "let" e:term " <-- " f:term : doElem => - `(doElem| let ⟨$e, h⟩ ← Result.attach $f) - --- We call the hypothesis `h`, in effect making it unavailable to the user --- (because too much shadowing). But in practice, once can use the French single --- quote notation (input with f< and f>), where `‹ h ›` finds a suitable --- hypothesis in the context, this is equivalent to `have x: h := by assumption in x` -#eval do - let y <-- .ret (0: Nat) - let _: y = 0 := by cases ‹ ret 0 = ret y › ; decide - let r: { x: Nat // x = 0 } := ⟨ y, by assumption ⟩ - .ret r - ----------------------- --- MACHINE INTEGERS -- ----------------------- - --- We redefine our machine integers types. - --- For Isize/Usize, we reuse `getNumBits` from `USize`. You cannot reduce `getNumBits` --- using the simplifier, meaning that proofs do not depend on the compile-time value of --- USize.size. (Lean assumes 32 or 64-bit platforms, and Rust doesn't really support, at --- least officially, 16-bit microcontrollers, so this seems like a fine design decision --- for now.) - --- Note from Chris Bailey: "If there's more than one salient property of your --- definition then the subtyping strategy might get messy, and the property part --- of a subtype is less discoverable by the simplifier or tactics like --- library_search." So, we will not add refinements on the return values of the --- operations defined on Primitives, but will rather rely on custom lemmas to --- invert on possible return values of the primitive operations. - --- Machine integer constants, done via `ofNatCore`, which requires a proof that --- the `Nat` fits within the desired integer type. We provide a custom tactic. - -open System.Platform.getNumBits - --- TODO: is there a way of only importing System.Platform.getNumBits? --- -@[simp] def size_num_bits : Nat := (System.Platform.getNumBits ()).val - --- Remark: Lean seems to use < for the comparisons with the upper bounds by convention. --- We keep the F* convention for now. -@[simp] def Isize.min : Int := - (HPow.hPow 2 (size_num_bits - 1)) -@[simp] def Isize.max : Int := (HPow.hPow 2 (size_num_bits - 1)) - 1 -@[simp] def I8.min : Int := - (HPow.hPow 2 7) -@[simp] def I8.max : Int := HPow.hPow 2 7 - 1 -@[simp] def I16.min : Int := - (HPow.hPow 2 15) -@[simp] def I16.max : Int := HPow.hPow 2 15 - 1 -@[simp] def I32.min : Int := -(HPow.hPow 2 31) -@[simp] def I32.max : Int := HPow.hPow 2 31 - 1 -@[simp] def I64.min : Int := -(HPow.hPow 2 63) -@[simp] def I64.max : Int := HPow.hPow 2 63 - 1 -@[simp] def I128.min : Int := -(HPow.hPow 2 127) -@[simp] def I128.max : Int := HPow.hPow 2 127 - 1 -@[simp] def Usize.min : Int := 0 -@[simp] def Usize.max : Int := HPow.hPow 2 size_num_bits - 1 -@[simp] def U8.min : Int := 0 -@[simp] def U8.max : Int := HPow.hPow 2 8 - 1 -@[simp] def U16.min : Int := 0 -@[simp] def U16.max : Int := HPow.hPow 2 16 - 1 -@[simp] def U32.min : Int := 0 -@[simp] def U32.max : Int := HPow.hPow 2 32 - 1 -@[simp] def U64.min : Int := 0 -@[simp] def U64.max : Int := HPow.hPow 2 64 - 1 -@[simp] def U128.min : Int := 0 -@[simp] def U128.max : Int := HPow.hPow 2 128 - 1 - -#assert (I8.min == -128) -#assert (I8.max == 127) -#assert (I16.min == -32768) -#assert (I16.max == 32767) -#assert (I32.min == -2147483648) -#assert (I32.max == 2147483647) -#assert (I64.min == -9223372036854775808) -#assert (I64.max == 9223372036854775807) -#assert (I128.min == -170141183460469231731687303715884105728) -#assert (I128.max == 170141183460469231731687303715884105727) -#assert (U8.min == 0) -#assert (U8.max == 255) -#assert (U16.min == 0) -#assert (U16.max == 65535) -#assert (U32.min == 0) -#assert (U32.max == 4294967295) -#assert (U64.min == 0) -#assert (U64.max == 18446744073709551615) -#assert (U128.min == 0) -#assert (U128.max == 340282366920938463463374607431768211455) - -inductive ScalarTy := -| Isize -| I8 -| I16 -| I32 -| I64 -| I128 -| Usize -| U8 -| U16 -| U32 -| U64 -| U128 - -def Scalar.min (ty : ScalarTy) : Int := - match ty with - | .Isize => Isize.min - | .I8 => I8.min - | .I16 => I16.min - | .I32 => I32.min - | .I64 => I64.min - | .I128 => I128.min - | .Usize => Usize.min - | .U8 => U8.min - | .U16 => U16.min - | .U32 => U32.min - | .U64 => U64.min - | .U128 => U128.min - -def Scalar.max (ty : ScalarTy) : Int := - match ty with - | .Isize => Isize.max - | .I8 => I8.max - | .I16 => I16.max - | .I32 => I32.max - | .I64 => I64.max - | .I128 => I128.max - | .Usize => Usize.max - | .U8 => U8.max - | .U16 => U16.max - | .U32 => U32.max - | .U64 => U64.max - | .U128 => U128.max - --- "Conservative" bounds --- We use those because we can't compare to the isize bounds (which can't --- reduce at compile-time). Whenever we perform an arithmetic operation like --- addition we need to check that the result is in bounds: we first compare --- to the conservative bounds, which reduce, then compare to the real bounds. --- This is useful for the various #asserts that we want to reduce at --- type-checking time. -def Scalar.cMin (ty : ScalarTy) : Int := - match ty with - | .Isize => I32.min - | _ => Scalar.min ty - -def Scalar.cMax (ty : ScalarTy) : Int := - match ty with - | .Isize => I32.max - | .Usize => U32.max - | _ => Scalar.max ty - -theorem Scalar.cMin_bound ty : Scalar.min ty <= Scalar.cMin ty := by sorry -theorem Scalar.cMax_bound ty : Scalar.min ty <= Scalar.cMin ty := by sorry - -structure Scalar (ty : ScalarTy) where - val : Int - hmin : Scalar.min ty <= val - hmax : val <= Scalar.max ty - -theorem Scalar.bound_suffices (ty : ScalarTy) (x : Int) : - Scalar.cMin ty <= x && x <= Scalar.cMax ty -> - (decide (Scalar.min ty ≤ x) && decide (x ≤ Scalar.max ty)) = true - := by sorry - -def Scalar.ofIntCore {ty : ScalarTy} (x : Int) - (hmin : Scalar.min ty <= x) (hmax : x <= Scalar.max ty) : Scalar ty := - { val := x, hmin := hmin, hmax := hmax } - -def Scalar.ofInt {ty : ScalarTy} (x : Int) - (h : Scalar.min ty <= x && x <= Scalar.max ty) : Scalar ty := - let hmin: Scalar.min ty <= x := by sorry - let hmax: x <= Scalar.max ty := by sorry - Scalar.ofIntCore x hmin hmax - --- Further thoughts: look at what has been done here: --- https://github.com/leanprover-community/mathlib4/blob/master/Mathlib/Data/Fin/Basic.lean --- and --- https://github.com/leanprover-community/mathlib4/blob/master/Mathlib/Data/UInt.lean --- which both contain a fair amount of reasoning already! -def Scalar.tryMk (ty : ScalarTy) (x : Int) : Result (Scalar ty) := - -- TODO: write this with only one if then else - if hmin_cons: Scalar.cMin ty <= x || Scalar.min ty <= x then - if hmax_cons: x <= Scalar.cMax ty || x <= Scalar.max ty then - let hmin: Scalar.min ty <= x := by sorry - let hmax: x <= Scalar.max ty := by sorry - return Scalar.ofIntCore x hmin hmax - else fail integerOverflow - else fail integerOverflow - -def Scalar.neg {ty : ScalarTy} (x : Scalar ty) : Result (Scalar ty) := Scalar.tryMk ty (- x.val) - -def Scalar.div {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - if y.val != 0 then Scalar.tryMk ty (x.val / y.val) else fail divisionByZero - --- Checking that the % operation in Lean computes the same as the remainder operation in Rust -#assert 1 % 2 = (1:Int) -#assert (-1) % 2 = -1 -#assert 1 % (-2) = 1 -#assert (-1) % (-2) = -1 - -def Scalar.rem {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - if y.val != 0 then Scalar.tryMk ty (x.val % y.val) else fail divisionByZero - -def Scalar.add {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - Scalar.tryMk ty (x.val + y.val) - -def Scalar.sub {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - Scalar.tryMk ty (x.val - y.val) - -def Scalar.mul {ty : ScalarTy} (x : Scalar ty) (y : Scalar ty) : Result (Scalar ty) := - Scalar.tryMk ty (x.val * y.val) - --- TODO: instances of +, -, * etc. for scalars - --- Cast an integer from a [src_ty] to a [tgt_ty] --- TODO: check the semantics of casts in Rust -def Scalar.cast {src_ty : ScalarTy} (tgt_ty : ScalarTy) (x : Scalar src_ty) : Result (Scalar tgt_ty) := - Scalar.tryMk tgt_ty x.val - --- The scalar types --- We declare the definitions as reducible so that Lean can unfold them (useful --- for type class resolution for instance). -@[reducible] def Isize := Scalar .Isize -@[reducible] def I8 := Scalar .I8 -@[reducible] def I16 := Scalar .I16 -@[reducible] def I32 := Scalar .I32 -@[reducible] def I64 := Scalar .I64 -@[reducible] def I128 := Scalar .I128 -@[reducible] def Usize := Scalar .Usize -@[reducible] def U8 := Scalar .U8 -@[reducible] def U16 := Scalar .U16 -@[reducible] def U32 := Scalar .U32 -@[reducible] def U64 := Scalar .U64 -@[reducible] def U128 := Scalar .U128 - --- TODO: below: not sure this is the best way. --- Should we rather overload operations like +, -, etc.? --- Also, it is possible to automate the generation of those definitions --- with macros (but would it be a good idea? It would be less easy to --- read the file, which is not supposed to change a lot) - --- Negation - -/-- -Remark: there is no heterogeneous negation in the Lean prelude: we thus introduce -one here. - -The notation typeclass for heterogeneous addition. -This enables the notation `- a : β` where `a : α`. --/ -class HNeg (α : Type u) (β : outParam (Type v)) where - /-- `- a` computes the negation of `a`. - The meaning of this notation is type-dependent. -/ - hNeg : α → β - -prefix:75 "-" => HNeg.hNeg - -instance : HNeg Isize (Result Isize) where hNeg x := Scalar.neg x -instance : HNeg I8 (Result I8) where hNeg x := Scalar.neg x -instance : HNeg I16 (Result I16) where hNeg x := Scalar.neg x -instance : HNeg I32 (Result I32) where hNeg x := Scalar.neg x -instance : HNeg I64 (Result I64) where hNeg x := Scalar.neg x -instance : HNeg I128 (Result I128) where hNeg x := Scalar.neg x - --- Addition -instance {ty} : HAdd (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hAdd x y := Scalar.add x y - --- Substraction -instance {ty} : HSub (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hSub x y := Scalar.sub x y - --- Multiplication -instance {ty} : HMul (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hMul x y := Scalar.mul x y - --- Division -instance {ty} : HDiv (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hDiv x y := Scalar.div x y - --- Remainder -instance {ty} : HMod (Scalar ty) (Scalar ty) (Result (Scalar ty)) where - hMod x y := Scalar.rem x y - --- ofIntCore --- TODO: typeclass? -def Isize.ofIntCore := @Scalar.ofIntCore .Isize -def I8.ofIntCore := @Scalar.ofIntCore .I8 -def I16.ofIntCore := @Scalar.ofIntCore .I16 -def I32.ofIntCore := @Scalar.ofIntCore .I32 -def I64.ofIntCore := @Scalar.ofIntCore .I64 -def I128.ofIntCore := @Scalar.ofIntCore .I128 -def Usize.ofIntCore := @Scalar.ofIntCore .Usize -def U8.ofIntCore := @Scalar.ofIntCore .U8 -def U16.ofIntCore := @Scalar.ofIntCore .U16 -def U32.ofIntCore := @Scalar.ofIntCore .U32 -def U64.ofIntCore := @Scalar.ofIntCore .U64 -def U128.ofIntCore := @Scalar.ofIntCore .U128 - --- ofInt --- TODO: typeclass? -def Isize.ofInt := @Scalar.ofInt .Isize -def I8.ofInt := @Scalar.ofInt .I8 -def I16.ofInt := @Scalar.ofInt .I16 -def I32.ofInt := @Scalar.ofInt .I32 -def I64.ofInt := @Scalar.ofInt .I64 -def I128.ofInt := @Scalar.ofInt .I128 -def Usize.ofInt := @Scalar.ofInt .Usize -def U8.ofInt := @Scalar.ofInt .U8 -def U16.ofInt := @Scalar.ofInt .U16 -def U32.ofInt := @Scalar.ofInt .U32 -def U64.ofInt := @Scalar.ofInt .U64 -def U128.ofInt := @Scalar.ofInt .U128 - --- Comparisons -instance {ty} : LT (Scalar ty) where - lt a b := LT.lt a.val b.val - -instance {ty} : LE (Scalar ty) where le a b := LE.le a.val b.val - -instance Scalar.decLt {ty} (a b : Scalar ty) : Decidable (LT.lt a b) := Int.decLt .. -instance Scalar.decLe {ty} (a b : Scalar ty) : Decidable (LE.le a b) := Int.decLe .. - -theorem Scalar.eq_of_val_eq {ty} : ∀ {i j : Scalar ty}, Eq i.val j.val → Eq i j - | ⟨_, _, _⟩, ⟨_, _, _⟩, rfl => rfl - -theorem Scalar.val_eq_of_eq {ty} {i j : Scalar ty} (h : Eq i j) : Eq i.val j.val := - h ▸ rfl - -theorem Scalar.ne_of_val_ne {ty} {i j : Scalar ty} (h : Not (Eq i.val j.val)) : Not (Eq i j) := - fun h' => absurd (val_eq_of_eq h') h - -instance (ty : ScalarTy) : DecidableEq (Scalar ty) := - fun i j => - match decEq i.val j.val with - | isTrue h => isTrue (Scalar.eq_of_val_eq h) - | isFalse h => isFalse (Scalar.ne_of_val_ne h) - -def Scalar.toInt {ty} (n : Scalar ty) : Int := n.val - --- Tactic to prove that integers are in bounds -syntax "intlit" : tactic - -macro_rules - | `(tactic| intlit) => `(tactic| apply Scalar.bound_suffices ; decide) - --- -- We now define a type class that subsumes the various machine integer types, so --- -- as to write a concise definition for scalar_cast, rather than exhaustively --- -- enumerating all of the possible pairs. We remark that Rust has sane semantics --- -- and fails if a cast operation would involve a truncation or modulo. - --- class MachineInteger (t: Type) where --- size: Nat --- val: t -> Fin size --- ofNatCore: (n:Nat) -> LT.lt n size -> t - --- set_option hygiene false in --- run_cmd --- for typeName in [`UInt8, `UInt16, `UInt32, `UInt64, `USize].map Lean.mkIdent do --- Lean.Elab.Command.elabCommand (← `( --- namespace $typeName --- instance: MachineInteger $typeName where --- size := size --- val := val --- ofNatCore := ofNatCore --- end $typeName --- )) - --- -- Aeneas only instantiates the destination type (`src` is implicit). We rely on --- -- Lean to infer `src`. - --- def scalar_cast { src: Type } (dst: Type) [ MachineInteger src ] [ MachineInteger dst ] (x: src): Result dst := --- if h: MachineInteger.val x < MachineInteger.size dst then --- .ret (MachineInteger.ofNatCore (MachineInteger.val x).val h) --- else --- .fail integerOverflow - -------------- --- VECTORS -- -------------- - -def Vec (α : Type u) := { l : List α // List.length l <= Usize.max } - -def vec_new (α : Type u): Vec α := ⟨ [], by sorry ⟩ - -def vec_len (α : Type u) (v : Vec α) : Usize := - let ⟨ v, l ⟩ := v - Usize.ofIntCore (List.length v) (by sorry) l - -def vec_push_fwd (α : Type u) (_ : Vec α) (_ : α) : Unit := () - -def vec_push_back (α : Type u) (v : Vec α) (x : α) : Result (Vec α) - := - if h : List.length v.val <= U32.max || List.length v.val <= Usize.max then - return ⟨ List.concat v.val x, by sorry ⟩ - else - fail maximumSizeExceeded - -def vec_insert_fwd (α : Type u) (v: Vec α) (i: Usize) (_: α): Result Unit := - if i.val < List.length v.val then - .ret () - else - .fail arrayOutOfBounds - -def vec_insert_back (α : Type u) (v: Vec α) (i: Usize) (x: α): Result (Vec α) := - if i.val < List.length v.val then - -- TODO: maybe we should redefine a list library which uses integers - -- (instead of natural numbers) - let i : Nat := - match i.val with - | .ofNat n => n - | .negSucc n => by sorry -- TODO: we can't get here - let isLt: i < USize.size := by sorry - let i : Fin USize.size := { val := i, isLt := isLt } - .ret ⟨ List.set v.val i.val x, by - have h: List.length v.val <= Usize.max := v.property - rewrite [ List.length_set v.val i.val x ] - assumption - ⟩ - else - .fail arrayOutOfBounds - -def vec_index_fwd (α : Type u) (v: Vec α) (i: Usize): Result α := - if i.val < List.length v.val then - let i : Nat := - match i.val with - | .ofNat n => n - | .negSucc n => by sorry -- TODO: we can't get here - let isLt: i < USize.size := by sorry - let i : Fin USize.size := { val := i, isLt := isLt } - let h: i < List.length v.val := by sorry - .ret (List.get v.val ⟨i.val, h⟩) - else - .fail arrayOutOfBounds - -def vec_index_back (α : Type u) (v: Vec α) (i: Usize) (_: α): Result Unit := - if i.val < List.length v.val then - .ret () - else - .fail arrayOutOfBounds - -def vec_index_mut_fwd (α : Type u) (v: Vec α) (i: Usize): Result α := - if i.val < List.length v.val then - let i : Nat := - match i.val with - | .ofNat n => n - | .negSucc n => by sorry -- TODO: we can't get here - let isLt: i < USize.size := by sorry - let i : Fin USize.size := { val := i, isLt := isLt } - let h: i < List.length v.val := by sorry - .ret (List.get v.val ⟨i.val, h⟩) - else - .fail arrayOutOfBounds - -def vec_index_mut_back (α : Type u) (v: Vec α) (i: Usize) (x: α): Result (Vec α) := - if i.val < List.length v.val then - let i : Nat := - match i.val with - | .ofNat n => n - | .negSucc n => by sorry -- TODO: we can't get here - let isLt: i < USize.size := by sorry - let i : Fin USize.size := { val := i, isLt := isLt } - .ret ⟨ List.set v.val i.val x, by - have h: List.length v.val <= Usize.max := v.property - rewrite [ List.length_set v.val i.val x ] - assumption - ⟩ - else - .fail arrayOutOfBounds - ----------- --- MISC -- ----------- - -def mem_replace_fwd (a : Type) (x : a) (_ : a) : a := - x - -def mem_replace_back (a : Type) (_ : a) (y : a) : a := - y - -/-- Aeneas-translated function -- useful to reduce non-recursive definitions. - Use with `simp [ aeneas ]` -/ -register_simp_attr aeneas diff --git a/tests/lean/misc-polonius_list/PoloniusList.lean b/tests/lean/misc-polonius_list/PoloniusList.lean deleted file mode 100644 index 79696996..00000000 --- a/tests/lean/misc-polonius_list/PoloniusList.lean +++ /dev/null @@ -1,31 +0,0 @@ --- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS --- [polonius_list] -import Base.Primitives - -/- [polonius_list::List] -/ -inductive list_t (T : Type) := -| Cons : T -> list_t T -> list_t T -| Nil : list_t T - -/- [polonius_list::get_list_at_x] -/ -def get_list_at_x_fwd (ls : list_t U32) (x : U32) : Result (list_t U32) := - match h: ls with - | list_t.Cons hd tl => - if h: hd = x - then Result.ret (list_t.Cons hd tl) - else get_list_at_x_fwd tl x - | list_t.Nil => Result.ret list_t.Nil - -/- [polonius_list::get_list_at_x] -/ -def get_list_at_x_back - (ls : list_t U32) (x : U32) (ret0 : list_t U32) : Result (list_t U32) := - match h: ls with - | list_t.Cons hd tl => - if h: hd = x - then Result.ret ret0 - else - do - let tl0 ← get_list_at_x_back tl x ret0 - Result.ret (list_t.Cons hd tl0) - | list_t.Nil => Result.ret ret0 - diff --git a/tests/lean/misc-polonius_list/lake-manifest.json b/tests/lean/misc-polonius_list/lake-manifest.json deleted file mode 100644 index 57b071ca..00000000 --- a/tests/lean/misc-polonius_list/lake-manifest.json +++ /dev/null @@ -1,27 +0,0 @@ -{"version": 4, - "packagesDir": "./lake-packages", - "packages": - [{"git": - {"url": "https://github.com/leanprover-community/mathlib4.git", - "subDir?": null, - "rev": "4037792ead804d7bfa8868e2c4684d4223c15ece", - "name": "mathlib", - "inputRev?": null}}, - {"git": - {"url": "https://github.com/gebner/quote4", - "subDir?": null, - "rev": "2412c4fdf4a8b689f4467618e5e7b371ae5014aa", - "name": "Qq", - "inputRev?": "master"}}, - {"git": - {"url": "https://github.com/JLimperg/aesop", - "subDir?": null, - "rev": "7fe9ecd9339b0e1796e89d243b776849c305c690", - "name": "aesop", - "inputRev?": "master"}}, - {"git": - {"url": "https://github.com/leanprover/std4", - "subDir?": null, - "rev": "24897887905b3a1254b244369f5dd2cf6174b0ee", - "name": "std", - "inputRev?": "main"}}]} diff --git a/tests/lean/misc-polonius_list/lakefile.lean b/tests/lean/misc-polonius_list/lakefile.lean deleted file mode 100644 index e89d4259..00000000 --- a/tests/lean/misc-polonius_list/lakefile.lean +++ /dev/null @@ -1,12 +0,0 @@ -import Lake -open Lake DSL - -require mathlib from git - "https://github.com/leanprover-community/mathlib4.git" - -package «polonius_list» {} - -lean_lib «Base» {} - -@[default_target] -lean_lib «PoloniusList» {} diff --git a/tests/lean/misc-polonius_list/lean-toolchain b/tests/lean/misc-polonius_list/lean-toolchain deleted file mode 100644 index bbf57f10..00000000 --- a/tests/lean/misc-polonius_list/lean-toolchain +++ /dev/null @@ -1 +0,0 @@ -leanprover/lean4:nightly-2023-01-21 -- cgit v1.2.3 From b643bd00747e75d69b6066c55a1798b61277c4b6 Mon Sep 17 00:00:00 2001 From: Son Ho Date: Tue, 4 Jul 2023 18:09:36 +0200 Subject: Regenerate the Lean test files --- tests/lean/BetreeMain/ExternalFuns.lean | 9 ++ tests/lean/BetreeMain/Funs.lean | 147 ++++++++++++++++--------------- tests/lean/BetreeMain/Opaque.lean | 16 ++-- tests/lean/BetreeMain/Types.lean | 24 +++-- tests/lean/Constants.lean | 3 + tests/lean/External/ExternalFuns.lean | 9 ++ tests/lean/External/Funs.lean | 3 + tests/lean/External/Opaque.lean | 14 +-- tests/lean/External/Types.lean | 3 + tests/lean/Hashmap/Funs.lean | 31 ++++--- tests/lean/Hashmap/Types.lean | 5 +- tests/lean/HashmapMain/ExternalFuns.lean | 9 ++ tests/lean/HashmapMain/Funs.lean | 31 ++++--- tests/lean/HashmapMain/Opaque.lean | 7 +- tests/lean/HashmapMain/Types.lean | 5 +- tests/lean/Loops/Funs.lean | 83 ++++++++--------- tests/lean/Loops/Types.lean | 5 +- tests/lean/NoNestedBorrows.lean | 33 +++---- tests/lean/Paper.lean | 11 ++- tests/lean/PoloniusList.lean | 9 +- tests/lean/lake-manifest.json | 2 +- tests/lean/lakefile.lean | 8 ++ 22 files changed, 275 insertions(+), 192 deletions(-) create mode 100644 tests/lean/BetreeMain/ExternalFuns.lean create mode 100644 tests/lean/External/ExternalFuns.lean create mode 100644 tests/lean/HashmapMain/ExternalFuns.lean (limited to 'tests/lean') diff --git a/tests/lean/BetreeMain/ExternalFuns.lean b/tests/lean/BetreeMain/ExternalFuns.lean new file mode 100644 index 00000000..59beb514 --- /dev/null +++ b/tests/lean/BetreeMain/ExternalFuns.lean @@ -0,0 +1,9 @@ +import Base +import BetreeMain.Types +import BetreeMain.Opaque + +namespace BetreeMain + +def opaque_defs : OpaqueDefs := by sorry + +end BetreeMain diff --git a/tests/lean/BetreeMain/Funs.lean b/tests/lean/BetreeMain/Funs.lean index fb48b3a6..2eb7fa1f 100644 --- a/tests/lean/BetreeMain/Funs.lean +++ b/tests/lean/BetreeMain/Funs.lean @@ -5,6 +5,8 @@ import BetreeMain.Types import BetreeMain.ExternalFuns open Primitives +namespace BetreeMain + /- [betree_main::betree::load_internal_node] -/ def betree_load_internal_node_fwd (id : U64) (st : State) : @@ -75,13 +77,13 @@ def core_num_u64_max_c : U64 := eval_global core_num_u64_max_body (by simp) /- [betree_main::betree::upsert_update] -/ def betree_upsert_update_fwd (prev : Option U64) (st : betree_upsert_fun_state_t) : Result U64 := - match h: prev with + match prev with | Option.none => - match h: st with + match st with | betree_upsert_fun_state_t.Add v => Result.ret v | betree_upsert_fun_state_t.Sub i => Result.ret (U64.ofInt 0 (by intlit)) | Option.some prev0 => - match h: st with + match st with | betree_upsert_fun_state_t.Add v => do let margin ← core_num_u64_max_c - prev0 @@ -96,7 +98,7 @@ def betree_upsert_update_fwd /- [betree_main::betree::List::{1}::len] -/ divergent def betree_list_len_fwd (T : Type) (self : betree_list_t T) : Result U64 := - match h: self with + match self with | betree_list_t.Cons t tl => do let i ← betree_list_len_fwd T tl @@ -111,7 +113,7 @@ divergent def betree_list_split_at_fwd if n = (U64.ofInt 0 (by intlit)) then Result.ret (betree_list_t.Nil, self) else - match h: self with + match self with | betree_list_t.Cons hd tl => do let i ← n - (U64.ofInt 1 (by intlit)) @@ -131,7 +133,7 @@ def betree_list_push_front_fwd_back /- [betree_main::betree::List::{1}::pop_front] -/ def betree_list_pop_front_fwd (T : Type) (self : betree_list_t T) : Result T := let ls := mem_replace_fwd (betree_list_t T) self betree_list_t.Nil - match h: ls with + match ls with | betree_list_t.Cons x tl => Result.ret x | betree_list_t.Nil => Result.fail Error.panic @@ -139,20 +141,20 @@ def betree_list_pop_front_fwd (T : Type) (self : betree_list_t T) : Result T := def betree_list_pop_front_back (T : Type) (self : betree_list_t T) : Result (betree_list_t T) := let ls := mem_replace_fwd (betree_list_t T) self betree_list_t.Nil - match h: ls with + match ls with | betree_list_t.Cons x tl => Result.ret tl | betree_list_t.Nil => Result.fail Error.panic /- [betree_main::betree::List::{1}::hd] -/ def betree_list_hd_fwd (T : Type) (self : betree_list_t T) : Result T := - match h: self with + match self with | betree_list_t.Cons hd l => Result.ret hd | betree_list_t.Nil => Result.fail Error.panic /- [betree_main::betree::List::{2}::head_has_key] -/ def betree_list_head_has_key_fwd (T : Type) (self : betree_list_t (U64 × T)) (key : U64) : Result Bool := - match h: self with + match self with | betree_list_t.Cons hd l => let (i, _) := hd Result.ret (i = key) | betree_list_t.Nil => Result.ret false @@ -162,7 +164,7 @@ divergent def betree_list_partition_at_pivot_fwd (T : Type) (self : betree_list_t (U64 × T)) (pivot : U64) : Result ((betree_list_t (U64 × T)) × (betree_list_t (U64 × T))) := - match h: self with + match self with | betree_list_t.Cons hd tl => let (i, t) := hd if i >= pivot @@ -204,7 +206,7 @@ def betree_leaf_split_fwd betree_leaf_id := id1, betree_leaf_size := params.betree_params_split_size } - Result.ret (st1, mkbetree_internal_t self.betree_leaf_id pivot n n0) + Result.ret (st1, betree_internal_t.mk self.betree_leaf_id pivot n n0) /- [betree_main::betree::Leaf::{3}::split] -/ def betree_leaf_split_back @@ -229,7 +231,7 @@ def betree_leaf_split_back /- [betree_main::betree::Node::{5}::lookup_in_bindings] -/ divergent def betree_node_lookup_in_bindings_fwd (key : U64) (bindings : betree_list_t (U64 × U64)) : Result (Option U64) := - match h: bindings with + match bindings with | betree_list_t.Cons hd tl => let (i, i0) := hd if i = key @@ -245,7 +247,7 @@ divergent def betree_node_lookup_first_message_for_key_fwd (key : U64) (msgs : betree_list_t (U64 × betree_message_t)) : Result (betree_list_t (U64 × betree_message_t)) := - match h: msgs with + match msgs with | betree_list_t.Cons x next_msgs => let (i, m) := x if i >= key @@ -259,7 +261,7 @@ divergent def betree_node_lookup_first_message_for_key_back (ret0 : betree_list_t (U64 × betree_message_t)) : Result (betree_list_t (U64 × betree_message_t)) := - match h: msgs with + match msgs with | betree_list_t.Cons x next_msgs => let (i, m) := x if i >= key @@ -284,7 +286,7 @@ divergent def betree_node_apply_upserts_fwd do let msg ← betree_list_pop_front_fwd (U64 × betree_message_t) msgs let (_, m) := msg - match h: m with + match m with | betree_message_t.Insert i => Result.fail Error.panic | betree_message_t.Delete => Result.fail Error.panic | betree_message_t.Upsert s => @@ -314,7 +316,7 @@ divergent def betree_node_apply_upserts_back do let msg ← betree_list_pop_front_fwd (U64 × betree_message_t) msgs let (_, m) := msg - match h: m with + match m with | betree_message_t.Insert i => Result.fail Error.panic | betree_message_t.Delete => Result.fail Error.panic | betree_message_t.Upsert s => @@ -334,27 +336,27 @@ mutual divergent def betree_node_lookup_fwd (self : betree_node_t) (key : U64) (st : State) : Result (State × (Option U64)) := - match h: self with + match self with | betree_node_t.Internal node => do - let (mkbetree_internal_t i i0 n n0) := node + let ⟨ i, i0, n, n0 ⟩ := node let (st0, msgs) ← betree_load_internal_node_fwd i st let pending ← betree_node_lookup_first_message_for_key_fwd key msgs - match h: pending with + match pending with | betree_list_t.Cons p l => let (k, msg) := p if k != key then do let (st1, opt) ← - betree_internal_lookup_in_children_fwd (mkbetree_internal_t i i0 + betree_internal_lookup_in_children_fwd (betree_internal_t.mk i i0 n n0) key st0 let _ ← betree_node_lookup_first_message_for_key_back key msgs (betree_list_t.Cons (k, msg) l) Result.ret (st1, opt) else - match h: msg with + match msg with | betree_message_t.Insert v => do let _ ← @@ -370,15 +372,15 @@ mutual divergent def betree_node_lookup_fwd | betree_message_t.Upsert ufs => do let (st1, v) ← - betree_internal_lookup_in_children_fwd (mkbetree_internal_t i + betree_internal_lookup_in_children_fwd (betree_internal_t.mk i i0 n n0) key st0 let (st2, v0) ← betree_node_apply_upserts_fwd (betree_list_t.Cons (k, betree_message_t.Upsert ufs) l) v key st1 let node0 ← - betree_internal_lookup_in_children_back (mkbetree_internal_t i + betree_internal_lookup_in_children_back (betree_internal_t.mk i i0 n n0) key st0 - let (mkbetree_internal_t i1 _ _ _) := node0 + let ⟨ i1, _, _, _ ⟩ := node0 let pending0 ← betree_node_apply_upserts_back (betree_list_t.Cons (k, betree_message_t.Upsert ufs) l) v key st1 @@ -389,7 +391,7 @@ mutual divergent def betree_node_lookup_fwd | betree_list_t.Nil => do let (st1, opt) ← - betree_internal_lookup_in_children_fwd (mkbetree_internal_t i i0 n + betree_internal_lookup_in_children_fwd (betree_internal_t.mk i i0 n n0) key st0 let _ ← betree_node_lookup_first_message_for_key_back key msgs @@ -404,13 +406,13 @@ mutual divergent def betree_node_lookup_fwd /- [betree_main::betree::Node::{5}::lookup] -/ divergent def betree_node_lookup_back (self : betree_node_t) (key : U64) (st : State) : Result betree_node_t := - match h: self with + match self with | betree_node_t.Internal node => do - let (mkbetree_internal_t i i0 n n0) := node + let ⟨ i, i0, n, n0 ⟩ := node let (st0, msgs) ← betree_load_internal_node_fwd i st let pending ← betree_node_lookup_first_message_for_key_fwd key msgs - match h: pending with + match pending with | betree_list_t.Cons p l => let (k, msg) := p if k != key @@ -420,44 +422,44 @@ divergent def betree_node_lookup_back betree_node_lookup_first_message_for_key_back key msgs (betree_list_t.Cons (k, msg) l) let node0 ← - betree_internal_lookup_in_children_back (mkbetree_internal_t i i0 - n n0) key st0 + betree_internal_lookup_in_children_back (betree_internal_t.mk i + i0 n n0) key st0 Result.ret (betree_node_t.Internal node0) else - match h: msg with + match msg with | betree_message_t.Insert v => do let _ ← betree_node_lookup_first_message_for_key_back key msgs (betree_list_t.Cons (k, betree_message_t.Insert v) l) - Result.ret (betree_node_t.Internal (mkbetree_internal_t i i0 n + Result.ret (betree_node_t.Internal (betree_internal_t.mk i i0 n n0)) | betree_message_t.Delete => do let _ ← betree_node_lookup_first_message_for_key_back key msgs (betree_list_t.Cons (k, betree_message_t.Delete) l) - Result.ret (betree_node_t.Internal (mkbetree_internal_t i i0 n + Result.ret (betree_node_t.Internal (betree_internal_t.mk i i0 n n0)) | betree_message_t.Upsert ufs => do let (st1, v) ← - betree_internal_lookup_in_children_fwd (mkbetree_internal_t i + betree_internal_lookup_in_children_fwd (betree_internal_t.mk i i0 n n0) key st0 let (st2, _) ← betree_node_apply_upserts_fwd (betree_list_t.Cons (k, betree_message_t.Upsert ufs) l) v key st1 let node0 ← - betree_internal_lookup_in_children_back (mkbetree_internal_t i + betree_internal_lookup_in_children_back (betree_internal_t.mk i i0 n n0) key st0 - let (mkbetree_internal_t i1 i2 n1 n2) := node0 + let ⟨ i1, i2, n1, n2 ⟩ := node0 let pending0 ← betree_node_apply_upserts_back (betree_list_t.Cons (k, betree_message_t.Upsert ufs) l) v key st1 let msgs0 ← betree_node_lookup_first_message_for_key_back key msgs pending0 let _ ← betree_store_internal_node_fwd i1 msgs0 st2 - Result.ret (betree_node_t.Internal (mkbetree_internal_t i1 i2 n1 + Result.ret (betree_node_t.Internal (betree_internal_t.mk i1 i2 n1 n2)) | betree_list_t.Nil => do @@ -465,8 +467,8 @@ divergent def betree_node_lookup_back betree_node_lookup_first_message_for_key_back key msgs betree_list_t.Nil let node0 ← - betree_internal_lookup_in_children_back (mkbetree_internal_t i i0 n - n0) key st0 + betree_internal_lookup_in_children_back (betree_internal_t.mk i i0 + n n0) key st0 Result.ret (betree_node_t.Internal node0) | betree_node_t.Leaf node => do @@ -479,7 +481,7 @@ divergent def betree_internal_lookup_in_children_fwd (self : betree_internal_t) (key : U64) (st : State) : Result (State × (Option U64)) := - let (mkbetree_internal_t _ i n n0) := self + let ⟨ _, i, n, n0 ⟩ := self if key < i then betree_node_lookup_fwd n key st else betree_node_lookup_fwd n0 key st @@ -489,16 +491,16 @@ divergent def betree_internal_lookup_in_children_back (self : betree_internal_t) (key : U64) (st : State) : Result betree_internal_t := - let (mkbetree_internal_t i i0 n n0) := self + let ⟨ i, i0, n, n0 ⟩ := self if key < i0 then do let n1 ← betree_node_lookup_back n key st - Result.ret (mkbetree_internal_t i i0 n1 n0) + Result.ret (betree_internal_t.mk i i0 n1 n0) else do let n1 ← betree_node_lookup_back n0 key st - Result.ret (mkbetree_internal_t i i0 n n1) + Result.ret (betree_internal_t.mk i i0 n n1) end @@ -507,7 +509,7 @@ divergent def betree_node_lookup_mut_in_bindings_fwd (key : U64) (bindings : betree_list_t (U64 × U64)) : Result (betree_list_t (U64 × U64)) := - match h: bindings with + match bindings with | betree_list_t.Cons hd tl => let (i, i0) := hd if i >= key @@ -521,7 +523,7 @@ divergent def betree_node_lookup_mut_in_bindings_back (ret0 : betree_list_t (U64 × U64)) : Result (betree_list_t (U64 × U64)) := - match h: bindings with + match bindings with | betree_list_t.Cons hd tl => let (i, i0) := hd if i >= key @@ -545,7 +547,7 @@ def betree_node_apply_to_leaf_fwd_back then do let hd ← betree_list_pop_front_fwd (U64 × U64) bindings0 - match h: new_msg with + match new_msg with | betree_message_t.Insert v => do let bindings1 ← betree_list_pop_front_back (U64 × U64) bindings0 @@ -565,7 +567,7 @@ def betree_node_apply_to_leaf_fwd_back betree_list_push_front_fwd_back (U64 × U64) bindings1 (key, v) betree_node_lookup_mut_in_bindings_back key bindings bindings2 else - match h: new_msg with + match new_msg with | betree_message_t.Insert v => do let bindings1 ← @@ -586,7 +588,7 @@ divergent def betree_node_apply_messages_to_leaf_fwd_back (new_msgs : betree_list_t (U64 × betree_message_t)) : Result (betree_list_t (U64 × U64)) := - match h: new_msgs with + match new_msgs with | betree_list_t.Cons new_msg new_msgs_tl => do let (i, m) := new_msg @@ -599,7 +601,7 @@ divergent def betree_node_filter_messages_for_key_fwd_back (key : U64) (msgs : betree_list_t (U64 × betree_message_t)) : Result (betree_list_t (U64 × betree_message_t)) := - match h: msgs with + match msgs with | betree_list_t.Cons p l => let (k, m) := p if k = key @@ -617,7 +619,7 @@ divergent def betree_node_lookup_first_message_after_key_fwd (key : U64) (msgs : betree_list_t (U64 × betree_message_t)) : Result (betree_list_t (U64 × betree_message_t)) := - match h: msgs with + match msgs with | betree_list_t.Cons p next_msgs => let (k, m) := p if k = key @@ -631,7 +633,7 @@ divergent def betree_node_lookup_first_message_after_key_back (ret0 : betree_list_t (U64 × betree_message_t)) : Result (betree_list_t (U64 × betree_message_t)) := - match h: msgs with + match msgs with | betree_list_t.Cons p next_msgs => let (k, m) := p if k = key @@ -654,7 +656,7 @@ def betree_node_apply_to_internal_fwd_back let b ← betree_list_head_has_key_fwd betree_message_t msgs0 key if b then - match h: new_msg with + match new_msg with | betree_message_t.Insert i => do let msgs1 ← betree_node_filter_messages_for_key_fwd_back key msgs0 @@ -673,7 +675,7 @@ def betree_node_apply_to_internal_fwd_back do let p ← betree_list_hd_fwd (U64 × betree_message_t) msgs0 let (_, m) := p - match h: m with + match m with | betree_message_t.Insert prev => do let v ← betree_upsert_update_fwd (Option.some prev) s @@ -715,7 +717,7 @@ divergent def betree_node_apply_messages_to_internal_fwd_back (new_msgs : betree_list_t (U64 × betree_message_t)) : Result (betree_list_t (U64 × betree_message_t)) := - match h: new_msgs with + match new_msgs with | betree_list_t.Cons new_msg new_msgs_tl => do let (i, m) := new_msg @@ -730,10 +732,10 @@ mutual divergent def betree_node_apply_messages_fwd (msgs : betree_list_t (U64 × betree_message_t)) (st : State) : Result (State × Unit) := - match h: self with + match self with | betree_node_t.Internal node => do - let (mkbetree_internal_t i i0 n n0) := node + let ⟨ i, i0, n, n0 ⟩ := node let (st0, content) ← betree_load_internal_node_fwd i st let content0 ← betree_node_apply_messages_to_internal_fwd_back content msgs @@ -742,12 +744,12 @@ mutual divergent def betree_node_apply_messages_fwd then do let (st1, content1) ← - betree_internal_flush_fwd (mkbetree_internal_t i i0 n n0) params + betree_internal_flush_fwd (betree_internal_t.mk i i0 n n0) params node_id_cnt content0 st0 let (node0, _) ← - betree_internal_flush_back (mkbetree_internal_t i i0 n n0) params + betree_internal_flush_back (betree_internal_t.mk i i0 n n0) params node_id_cnt content0 st0 - let (mkbetree_internal_t i1 _ _ _) := node0 + let ⟨ i1, _, _, _ ⟩ := node0 let (st2, _) ← betree_store_internal_node_fwd i1 content1 st1 Result.ret (st2, ()) else @@ -782,10 +784,10 @@ divergent def betree_node_apply_messages_back (msgs : betree_list_t (U64 × betree_message_t)) (st : State) : Result (betree_node_t × betree_node_id_counter_t) := - match h: self with + match self with | betree_node_t.Internal node => do - let (mkbetree_internal_t i i0 n n0) := node + let ⟨ i, i0, n, n0 ⟩ := node let (st0, content) ← betree_load_internal_node_fwd i st let content0 ← betree_node_apply_messages_to_internal_fwd_back content msgs @@ -794,19 +796,19 @@ divergent def betree_node_apply_messages_back then do let (st1, content1) ← - betree_internal_flush_fwd (mkbetree_internal_t i i0 n n0) params + betree_internal_flush_fwd (betree_internal_t.mk i i0 n n0) params node_id_cnt content0 st0 let (node0, node_id_cnt0) ← - betree_internal_flush_back (mkbetree_internal_t i i0 n n0) params + betree_internal_flush_back (betree_internal_t.mk i i0 n n0) params node_id_cnt content0 st0 - let (mkbetree_internal_t i1 i2 n1 n2) := node0 + let ⟨ i1, i2, n1, n2 ⟩ := node0 let _ ← betree_store_internal_node_fwd i1 content1 st1 - Result.ret (betree_node_t.Internal (mkbetree_internal_t i1 i2 n1 n2), - node_id_cnt0) + Result.ret (betree_node_t.Internal (betree_internal_t.mk i1 i2 n1 + n2), node_id_cnt0) else do let _ ← betree_store_internal_node_fwd i content0 st0 - Result.ret (betree_node_t.Internal (mkbetree_internal_t i i0 n n0), + Result.ret (betree_node_t.Internal (betree_internal_t.mk i i0 n n0), node_id_cnt) | betree_node_t.Leaf node => do @@ -839,7 +841,7 @@ divergent def betree_internal_flush_fwd Result (State × (betree_list_t (U64 × betree_message_t))) := do - let (mkbetree_internal_t _ i n n0) := self + let ⟨ _, i, n, n0 ⟩ := self let p ← betree_list_partition_at_pivot_fwd betree_message_t content i let (msgs_left, msgs_right) := p let len_left ← betree_list_len_fwd (U64 × betree_message_t) msgs_left @@ -879,7 +881,7 @@ divergent def betree_internal_flush_back Result (betree_internal_t × betree_node_id_counter_t) := do - let (mkbetree_internal_t i i0 n n0) := self + let ⟨ i, i0, n, n0 ⟩ := self let p ← betree_list_partition_at_pivot_fwd betree_message_t content i0 let (msgs_left, msgs_right) := p let len_left ← betree_list_len_fwd (U64 × betree_message_t) msgs_left @@ -898,13 +900,13 @@ divergent def betree_internal_flush_back let (n2, node_id_cnt1) ← betree_node_apply_messages_back n0 params node_id_cnt0 msgs_right st0 - Result.ret (mkbetree_internal_t i i0 n1 n2, node_id_cnt1) - else Result.ret (mkbetree_internal_t i i0 n1 n0, node_id_cnt0) + Result.ret (betree_internal_t.mk i i0 n1 n2, node_id_cnt1) + else Result.ret (betree_internal_t.mk i i0 n1 n0, node_id_cnt0) else do let (n1, node_id_cnt0) ← betree_node_apply_messages_back n0 params node_id_cnt msgs_right st - Result.ret (mkbetree_internal_t i i0 n n1, node_id_cnt0) + Result.ret (betree_internal_t.mk i i0 n n1, node_id_cnt0) end @@ -1069,3 +1071,4 @@ def main_fwd : Result Unit := /- Unit test for [betree_main::main] -/ #assert (main_fwd == .ret ()) +end BetreeMain diff --git a/tests/lean/BetreeMain/Opaque.lean b/tests/lean/BetreeMain/Opaque.lean index c8226d4e..d043186b 100644 --- a/tests/lean/BetreeMain/Opaque.lean +++ b/tests/lean/BetreeMain/Opaque.lean @@ -4,28 +4,32 @@ import Base import BetreeMain.Types open Primitives +namespace BetreeMain + structure OpaqueDefs where /- [betree_main::betree_utils::load_internal_node] -/ betree_utils_load_internal_node_fwd : - U64 -> State -> Result (State × (betree_list_t (U64 × betree_message_t))) + U64 → State → Result (State × (betree_list_t (U64 × + betree_message_t))) /- [betree_main::betree_utils::store_internal_node] -/ betree_utils_store_internal_node_fwd : - U64 -> betree_list_t (U64 × betree_message_t) -> State -> Result (State × - Unit) + U64 → betree_list_t (U64 × betree_message_t) → State → Result (State + × Unit) /- [betree_main::betree_utils::load_leaf_node] -/ betree_utils_load_leaf_node_fwd - : U64 -> State -> Result (State × (betree_list_t (U64 × U64))) + : U64 → State → Result (State × (betree_list_t (U64 × U64))) /- [betree_main::betree_utils::store_leaf_node] -/ betree_utils_store_leaf_node_fwd - : U64 -> betree_list_t (U64 × U64) -> State -> Result (State × Unit) + : U64 → betree_list_t (U64 × U64) → State → Result (State × Unit) /- [core::option::Option::{0}::unwrap] -/ core_option_option_unwrap_fwd - (T : Type) : Option T -> State -> Result (State × T) + (T : Type) : Option T → State → Result (State × T) +end BetreeMain diff --git a/tests/lean/BetreeMain/Types.lean b/tests/lean/BetreeMain/Types.lean index 4875a8ba..cfed6a28 100644 --- a/tests/lean/BetreeMain/Types.lean +++ b/tests/lean/BetreeMain/Types.lean @@ -3,21 +3,23 @@ import Base open Primitives +namespace BetreeMain + /- [betree_main::betree::List] -/ inductive betree_list_t (T : Type) := -| Cons : T -> betree_list_t T -> betree_list_t T +| Cons : T → betree_list_t T → betree_list_t T | Nil : betree_list_t T /- [betree_main::betree::UpsertFunState] -/ inductive betree_upsert_fun_state_t := -| Add : U64 -> betree_upsert_fun_state_t -| Sub : U64 -> betree_upsert_fun_state_t +| Add : U64 → betree_upsert_fun_state_t +| Sub : U64 → betree_upsert_fun_state_t /- [betree_main::betree::Message] -/ inductive betree_message_t := -| Insert : U64 -> betree_message_t +| Insert : U64 → betree_message_t | Delete : betree_message_t -| Upsert : betree_upsert_fun_state_t -> betree_message_t +| Upsert : betree_upsert_fun_state_t → betree_message_t /- [betree_main::betree::Leaf] -/ structure betree_leaf_t where @@ -28,17 +30,12 @@ mutual /- [betree_main::betree::Node] -/ inductive betree_node_t := -| Internal : betree_internal_t -> betree_node_t -| Leaf : betree_leaf_t -> betree_node_t +| Internal : betree_internal_t → betree_node_t +| Leaf : betree_leaf_t → betree_node_t /- [betree_main::betree::Internal] -/ inductive betree_internal_t := -| mkbetree_internal_t : - U64 -> - U64 -> - betree_node_t -> - betree_node_t -> - betree_internal_t +| mk : U64 → U64 → betree_node_t → betree_node_t → betree_internal_t end @@ -60,3 +57,4 @@ structure betree_be_tree_t where /- The state type used in the state-error monad -/ axiom State : Type +end BetreeMain diff --git a/tests/lean/Constants.lean b/tests/lean/Constants.lean index cd2f88f5..9f6a47de 100644 --- a/tests/lean/Constants.lean +++ b/tests/lean/Constants.lean @@ -3,6 +3,8 @@ import Base open Primitives +namespace Constants + /- [constants::X0] -/ def x0_body : Result U32 := Result.ret (U32.ofInt 0 (by intlit)) def x0_c : U32 := eval_global x0_body (by simp) @@ -130,3 +132,4 @@ def s4_body : Result (pair_t U32 U32) := mk_pair1_fwd (U32.ofInt 7 (by intlit)) (U32.ofInt 8 (by intlit)) def s4_c : pair_t U32 U32 := eval_global s4_body (by simp) +end Constants diff --git a/tests/lean/External/ExternalFuns.lean b/tests/lean/External/ExternalFuns.lean new file mode 100644 index 00000000..d63db2ac --- /dev/null +++ b/tests/lean/External/ExternalFuns.lean @@ -0,0 +1,9 @@ +import Base +import External.Types +import External.Opaque + +namespace External + +def opaque_defs : OpaqueDefs := sorry + +end External diff --git a/tests/lean/External/Funs.lean b/tests/lean/External/Funs.lean index 73e45938..e36987e0 100644 --- a/tests/lean/External/Funs.lean +++ b/tests/lean/External/Funs.lean @@ -5,6 +5,8 @@ import External.Types import External.ExternalFuns open Primitives +namespace External + /- [external::swap] -/ def swap_fwd (T : Type) (x : T) (y : T) (st : State) : Result (State × Unit) := @@ -86,3 +88,4 @@ def test_swap_non_zero_fwd (x : U32) (st : State) : Result (State × U32) := then Result.fail Error.panic else Result.ret (st1, x0) +end External diff --git a/tests/lean/External/Opaque.lean b/tests/lean/External/Opaque.lean index 5483c3a9..1c0db095 100644 --- a/tests/lean/External/Opaque.lean +++ b/tests/lean/External/Opaque.lean @@ -4,25 +4,29 @@ import Base import External.Types open Primitives +namespace External + structure OpaqueDefs where /- [core::mem::swap] -/ - core_mem_swap_fwd (T : Type) : T -> T -> State -> Result (State × Unit) + core_mem_swap_fwd (T : Type) : T → T → State → Result (State × Unit) /- [core::mem::swap] -/ core_mem_swap_back0 - (T : Type) : T -> T -> State -> State -> Result (State × T) + (T : Type) : T → T → State → State → Result (State × T) /- [core::mem::swap] -/ core_mem_swap_back1 - (T : Type) : T -> T -> State -> State -> Result (State × T) + (T : Type) : T → T → State → State → Result (State × T) /- [core::num::nonzero::NonZeroU32::{14}::new] -/ core_num_nonzero_non_zero_u32_new_fwd : - U32 -> State -> Result (State × (Option core_num_nonzero_non_zero_u32_t)) + U32 → State → Result (State × (Option + core_num_nonzero_non_zero_u32_t)) /- [core::option::Option::{0}::unwrap] -/ core_option_option_unwrap_fwd - (T : Type) : Option T -> State -> Result (State × T) + (T : Type) : Option T → State → Result (State × T) +end External diff --git a/tests/lean/External/Types.lean b/tests/lean/External/Types.lean index 25907da2..fda0670e 100644 --- a/tests/lean/External/Types.lean +++ b/tests/lean/External/Types.lean @@ -3,9 +3,12 @@ import Base open Primitives +namespace External + /- [core::num::nonzero::NonZeroU32] -/ axiom core_num_nonzero_non_zero_u32_t : Type /- The state type used in the state-error monad -/ axiom State : Type +end External diff --git a/tests/lean/Hashmap/Funs.lean b/tests/lean/Hashmap/Funs.lean index 26742d5d..b4254726 100644 --- a/tests/lean/Hashmap/Funs.lean +++ b/tests/lean/Hashmap/Funs.lean @@ -4,6 +4,8 @@ import Base import Hashmap.Types open Primitives +namespace Hashmap + /- [hashmap::hash_key] -/ def hash_key_fwd (k : Usize) : Result Usize := Result.ret k @@ -82,7 +84,7 @@ def hash_map_len_fwd (T : Type) (self : hash_map_t T) : Result Usize := /- [hashmap::HashMap::{0}::insert_in_list] -/ divergent def hash_map_insert_in_list_loop_fwd (T : Type) (key : Usize) (value : T) (ls : list_t T) : Result Bool := - match h: ls with + match ls with | list_t.Cons ckey cvalue tl => if ckey = key then Result.ret false @@ -97,7 +99,7 @@ def hash_map_insert_in_list_fwd /- [hashmap::HashMap::{0}::insert_in_list] -/ divergent def hash_map_insert_in_list_loop_back (T : Type) (key : Usize) (value : T) (ls : list_t T) : Result (list_t T) := - match h: ls with + match ls with | list_t.Cons ckey cvalue tl => if ckey = key then Result.ret (list_t.Cons ckey value tl) @@ -146,7 +148,7 @@ def core_num_u32_max_c : U32 := eval_global core_num_u32_max_body (by simp) /- [hashmap::HashMap::{0}::move_elements_from_list] -/ divergent def hash_map_move_elements_from_list_loop_fwd_back (T : Type) (ntable : hash_map_t T) (ls : list_t T) : Result (hash_map_t T) := - match h: ls with + match ls with | list_t.Cons k v tl => do let ntable0 ← hash_map_insert_no_resize_fwd_back T ntable k v @@ -224,7 +226,7 @@ def hash_map_insert_fwd_back /- [hashmap::HashMap::{0}::contains_key_in_list] -/ divergent def hash_map_contains_key_in_list_loop_fwd (T : Type) (key : Usize) (ls : list_t T) : Result Bool := - match h: ls with + match ls with | list_t.Cons ckey t tl => if ckey = key then Result.ret true @@ -249,7 +251,7 @@ def hash_map_contains_key_fwd /- [hashmap::HashMap::{0}::get_in_list] -/ divergent def hash_map_get_in_list_loop_fwd (T : Type) (key : Usize) (ls : list_t T) : Result T := - match h: ls with + match ls with | list_t.Cons ckey cvalue tl => if ckey = key then Result.ret cvalue @@ -274,7 +276,7 @@ def hash_map_get_fwd /- [hashmap::HashMap::{0}::get_mut_in_list] -/ divergent def hash_map_get_mut_in_list_loop_fwd (T : Type) (ls : list_t T) (key : Usize) : Result T := - match h: ls with + match ls with | list_t.Cons ckey cvalue tl => if ckey = key then Result.ret cvalue @@ -289,7 +291,7 @@ def hash_map_get_mut_in_list_fwd /- [hashmap::HashMap::{0}::get_mut_in_list] -/ divergent def hash_map_get_mut_in_list_loop_back (T : Type) (ls : list_t T) (key : Usize) (ret0 : T) : Result (list_t T) := - match h: ls with + match ls with | list_t.Cons ckey cvalue tl => if ckey = key then Result.ret (list_t.Cons ckey ret0 tl) @@ -331,13 +333,13 @@ def hash_map_get_mut_back /- [hashmap::HashMap::{0}::remove_from_list] -/ divergent def hash_map_remove_from_list_loop_fwd (T : Type) (key : Usize) (ls : list_t T) : Result (Option T) := - match h: ls with + match ls with | list_t.Cons ckey t tl => if ckey = key then let mv_ls := mem_replace_fwd (list_t T) (list_t.Cons ckey t tl) list_t.Nil - match h: mv_ls with + match mv_ls with | list_t.Cons i cvalue tl0 => Result.ret (Option.some cvalue) | list_t.Nil => Result.fail Error.panic else hash_map_remove_from_list_loop_fwd T key tl @@ -351,13 +353,13 @@ def hash_map_remove_from_list_fwd /- [hashmap::HashMap::{0}::remove_from_list] -/ divergent def hash_map_remove_from_list_loop_back (T : Type) (key : Usize) (ls : list_t T) : Result (list_t T) := - match h: ls with + match ls with | list_t.Cons ckey t tl => if ckey = key then let mv_ls := mem_replace_fwd (list_t T) (list_t.Cons ckey t tl) list_t.Nil - match h: mv_ls with + match mv_ls with | list_t.Cons i cvalue tl0 => Result.ret tl0 | list_t.Nil => Result.fail Error.panic else @@ -380,7 +382,7 @@ def hash_map_remove_fwd let hash_mod ← hash % i let l ← vec_index_mut_fwd (list_t T) self.hash_map_slots hash_mod let x ← hash_map_remove_from_list_fwd T key l - match h: x with + match x with | Option.none => Result.ret Option.none | Option.some x0 => do @@ -396,7 +398,7 @@ def hash_map_remove_back let hash_mod ← hash % i let l ← vec_index_mut_fwd (list_t T) self.hash_map_slots hash_mod let x ← hash_map_remove_from_list_fwd T key l - match h: x with + match x with | Option.none => do let l0 ← hash_map_remove_from_list_back T key l @@ -441,7 +443,7 @@ def test1_fwd : Result Unit := do let x ← hash_map_remove_fwd U64 hm4 (Usize.ofInt 1024 (by intlit)) - match h: x with + match x with | Option.none => Result.fail Error.panic | Option.some x0 => if not (x0 = (U64.ofInt 56 (by intlit))) @@ -472,3 +474,4 @@ def test1_fwd : Result Unit := /- Unit test for [hashmap::test1] -/ #assert (test1_fwd == .ret ()) +end Hashmap diff --git a/tests/lean/Hashmap/Types.lean b/tests/lean/Hashmap/Types.lean index af26f363..0aec6acf 100644 --- a/tests/lean/Hashmap/Types.lean +++ b/tests/lean/Hashmap/Types.lean @@ -3,9 +3,11 @@ import Base open Primitives +namespace Hashmap + /- [hashmap::List] -/ inductive list_t (T : Type) := -| Cons : Usize -> T -> list_t T -> list_t T +| Cons : Usize → T → list_t T → list_t T | Nil : list_t T /- [hashmap::HashMap] -/ @@ -15,3 +17,4 @@ structure hash_map_t (T : Type) where hash_map_max_load : Usize hash_map_slots : Vec (list_t T) +end Hashmap diff --git a/tests/lean/HashmapMain/ExternalFuns.lean b/tests/lean/HashmapMain/ExternalFuns.lean new file mode 100644 index 00000000..bc831158 --- /dev/null +++ b/tests/lean/HashmapMain/ExternalFuns.lean @@ -0,0 +1,9 @@ +import Base +import HashmapMain.Types +import HashmapMain.Opaque + +namespace HashmapMain + +def opaque_defs : OpaqueDefs := by sorry + +end HashmapMain diff --git a/tests/lean/HashmapMain/Funs.lean b/tests/lean/HashmapMain/Funs.lean index a59a9f26..34a0eca1 100644 --- a/tests/lean/HashmapMain/Funs.lean +++ b/tests/lean/HashmapMain/Funs.lean @@ -5,6 +5,8 @@ import HashmapMain.Types import HashmapMain.ExternalFuns open Primitives +namespace HashmapMain + /- [hashmap_main::hashmap::hash_key] -/ def hashmap_hash_key_fwd (k : Usize) : Result Usize := Result.ret k @@ -92,7 +94,7 @@ def hashmap_hash_map_len_fwd /- [hashmap_main::hashmap::HashMap::{0}::insert_in_list] -/ divergent def hashmap_hash_map_insert_in_list_loop_fwd (T : Type) (key : Usize) (value : T) (ls : hashmap_list_t T) : Result Bool := - match h: ls with + match ls with | hashmap_list_t.Cons ckey cvalue tl => if ckey = key then Result.ret false @@ -109,7 +111,7 @@ divergent def hashmap_hash_map_insert_in_list_loop_back (T : Type) (key : Usize) (value : T) (ls : hashmap_list_t T) : Result (hashmap_list_t T) := - match h: ls with + match ls with | hashmap_list_t.Cons ckey cvalue tl => if ckey = key then Result.ret (hashmap_list_t.Cons ckey value tl) @@ -173,7 +175,7 @@ divergent def hashmap_hash_map_move_elements_from_list_loop_fwd_back (T : Type) (ntable : hashmap_hash_map_t T) (ls : hashmap_list_t T) : Result (hashmap_hash_map_t T) := - match h: ls with + match ls with | hashmap_list_t.Cons k v tl => do let ntable0 ← hashmap_hash_map_insert_no_resize_fwd_back T ntable k v @@ -256,7 +258,7 @@ def hashmap_hash_map_insert_fwd_back /- [hashmap_main::hashmap::HashMap::{0}::contains_key_in_list] -/ divergent def hashmap_hash_map_contains_key_in_list_loop_fwd (T : Type) (key : Usize) (ls : hashmap_list_t T) : Result Bool := - match h: ls with + match ls with | hashmap_list_t.Cons ckey t tl => if ckey = key then Result.ret true @@ -282,7 +284,7 @@ def hashmap_hash_map_contains_key_fwd /- [hashmap_main::hashmap::HashMap::{0}::get_in_list] -/ divergent def hashmap_hash_map_get_in_list_loop_fwd (T : Type) (key : Usize) (ls : hashmap_list_t T) : Result T := - match h: ls with + match ls with | hashmap_list_t.Cons ckey cvalue tl => if ckey = key then Result.ret cvalue @@ -308,7 +310,7 @@ def hashmap_hash_map_get_fwd /- [hashmap_main::hashmap::HashMap::{0}::get_mut_in_list] -/ divergent def hashmap_hash_map_get_mut_in_list_loop_fwd (T : Type) (ls : hashmap_list_t T) (key : Usize) : Result T := - match h: ls with + match ls with | hashmap_list_t.Cons ckey cvalue tl => if ckey = key then Result.ret cvalue @@ -325,7 +327,7 @@ divergent def hashmap_hash_map_get_mut_in_list_loop_back (T : Type) (ls : hashmap_list_t T) (key : Usize) (ret0 : T) : Result (hashmap_list_t T) := - match h: ls with + match ls with | hashmap_list_t.Cons ckey cvalue tl => if ckey = key then Result.ret (hashmap_list_t.Cons ckey ret0 tl) @@ -373,14 +375,14 @@ def hashmap_hash_map_get_mut_back /- [hashmap_main::hashmap::HashMap::{0}::remove_from_list] -/ divergent def hashmap_hash_map_remove_from_list_loop_fwd (T : Type) (key : Usize) (ls : hashmap_list_t T) : Result (Option T) := - match h: ls with + match ls with | hashmap_list_t.Cons ckey t tl => if ckey = key then let mv_ls := mem_replace_fwd (hashmap_list_t T) (hashmap_list_t.Cons ckey t tl) hashmap_list_t.Nil - match h: mv_ls with + match mv_ls with | hashmap_list_t.Cons i cvalue tl0 => Result.ret (Option.some cvalue) | hashmap_list_t.Nil => Result.fail Error.panic else hashmap_hash_map_remove_from_list_loop_fwd T key tl @@ -396,14 +398,14 @@ divergent def hashmap_hash_map_remove_from_list_loop_back (T : Type) (key : Usize) (ls : hashmap_list_t T) : Result (hashmap_list_t T) := - match h: ls with + match ls with | hashmap_list_t.Cons ckey t tl => if ckey = key then let mv_ls := mem_replace_fwd (hashmap_list_t T) (hashmap_list_t.Cons ckey t tl) hashmap_list_t.Nil - match h: mv_ls with + match mv_ls with | hashmap_list_t.Cons i cvalue tl0 => Result.ret tl0 | hashmap_list_t.Nil => Result.fail Error.panic else @@ -429,7 +431,7 @@ def hashmap_hash_map_remove_fwd let l ← vec_index_mut_fwd (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod let x ← hashmap_hash_map_remove_from_list_fwd T key l - match h: x with + match x with | Option.none => Result.ret Option.none | Option.some x0 => do @@ -449,7 +451,7 @@ def hashmap_hash_map_remove_back let l ← vec_index_mut_fwd (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod let x ← hashmap_hash_map_remove_from_list_fwd T key l - match h: x with + match x with | Option.none => do let l0 ← hashmap_hash_map_remove_from_list_back T key l @@ -505,7 +507,7 @@ def hashmap_test1_fwd : Result Unit := let x ← hashmap_hash_map_remove_fwd U64 hm4 (Usize.ofInt 1024 (by intlit)) - match h: x with + match x with | Option.none => Result.fail Error.panic | Option.some x0 => if not (x0 = (U64.ofInt 56 (by intlit))) @@ -555,3 +557,4 @@ def main_fwd : Result Unit := /- Unit test for [hashmap_main::main] -/ #assert (main_fwd == .ret ()) +end HashmapMain diff --git a/tests/lean/HashmapMain/Opaque.lean b/tests/lean/HashmapMain/Opaque.lean index bef4f3fb..10e4d8bd 100644 --- a/tests/lean/HashmapMain/Opaque.lean +++ b/tests/lean/HashmapMain/Opaque.lean @@ -4,13 +4,16 @@ import Base import HashmapMain.Types open Primitives +namespace HashmapMain + structure OpaqueDefs where /- [hashmap_main::hashmap_utils::deserialize] -/ hashmap_utils_deserialize_fwd - : State -> Result (State × (hashmap_hash_map_t U64)) + : State → Result (State × (hashmap_hash_map_t U64)) /- [hashmap_main::hashmap_utils::serialize] -/ hashmap_utils_serialize_fwd - : hashmap_hash_map_t U64 -> State -> Result (State × Unit) + : hashmap_hash_map_t U64 → State → Result (State × Unit) +end HashmapMain diff --git a/tests/lean/HashmapMain/Types.lean b/tests/lean/HashmapMain/Types.lean index 858e1c51..b91ff3a7 100644 --- a/tests/lean/HashmapMain/Types.lean +++ b/tests/lean/HashmapMain/Types.lean @@ -3,9 +3,11 @@ import Base open Primitives +namespace HashmapMain + /- [hashmap_main::hashmap::List] -/ inductive hashmap_list_t (T : Type) := -| Cons : Usize -> T -> hashmap_list_t T -> hashmap_list_t T +| Cons : Usize → T → hashmap_list_t T → hashmap_list_t T | Nil : hashmap_list_t T /- [hashmap_main::hashmap::HashMap] -/ @@ -18,3 +20,4 @@ structure hashmap_hash_map_t (T : Type) where /- The state type used in the state-error monad -/ axiom State : Type +end HashmapMain diff --git a/tests/lean/Loops/Funs.lean b/tests/lean/Loops/Funs.lean index 7d5f7595..9e084327 100644 --- a/tests/lean/Loops/Funs.lean +++ b/tests/lean/Loops/Funs.lean @@ -4,6 +4,8 @@ import Base import Loops.Types open Primitives +namespace Loops + /- [loops::sum] -/ divergent def sum_loop_fwd (max : U32) (i : U32) (s : U32) : Result U32 := if i < max @@ -68,7 +70,7 @@ def clear_fwd_back (v : Vec U32) : Result (Vec U32) := /- [loops::list_mem] -/ divergent def list_mem_loop_fwd (x : U32) (ls : list_t U32) : Result Bool := - match h: ls with + match ls with | list_t.Cons y tl => if y = x then Result.ret true @@ -82,7 +84,7 @@ def list_mem_fwd (x : U32) (ls : list_t U32) : Result Bool := /- [loops::list_nth_mut_loop] -/ divergent def list_nth_mut_loop_loop_fwd (T : Type) (ls : list_t T) (i : U32) : Result T := - match h: ls with + match ls with | list_t.Cons x tl => if i = (U32.ofInt 0 (by intlit)) then Result.ret x @@ -99,7 +101,7 @@ def list_nth_mut_loop_fwd (T : Type) (ls : list_t T) (i : U32) : Result T := /- [loops::list_nth_mut_loop] -/ divergent def list_nth_mut_loop_loop_back (T : Type) (ls : list_t T) (i : U32) (ret0 : T) : Result (list_t T) := - match h: ls with + match ls with | list_t.Cons x tl => if i = (U32.ofInt 0 (by intlit)) then Result.ret (list_t.Cons ret0 tl) @@ -118,7 +120,7 @@ def list_nth_mut_loop_back /- [loops::list_nth_shared_loop] -/ divergent def list_nth_shared_loop_loop_fwd (T : Type) (ls : list_t T) (i : U32) : Result T := - match h: ls with + match ls with | list_t.Cons x tl => if i = (U32.ofInt 0 (by intlit)) then Result.ret x @@ -135,7 +137,7 @@ def list_nth_shared_loop_fwd (T : Type) (ls : list_t T) (i : U32) : Result T := /- [loops::get_elem_mut] -/ divergent def get_elem_mut_loop_fwd (x : Usize) (ls : list_t Usize) : Result Usize := - match h: ls with + match ls with | list_t.Cons y tl => if y = x then Result.ret y @@ -152,7 +154,7 @@ def get_elem_mut_fwd (slots : Vec (list_t Usize)) (x : Usize) : Result Usize := /- [loops::get_elem_mut] -/ divergent def get_elem_mut_loop_back (x : Usize) (ls : list_t Usize) (ret0 : Usize) : Result (list_t Usize) := - match h: ls with + match ls with | list_t.Cons y tl => if y = x then Result.ret (list_t.Cons ret0 tl) @@ -176,7 +178,7 @@ def get_elem_mut_back /- [loops::get_elem_shared] -/ divergent def get_elem_shared_loop_fwd (x : Usize) (ls : list_t Usize) : Result Usize := - match h: ls with + match ls with | list_t.Cons y tl => if y = x then Result.ret y @@ -206,7 +208,7 @@ def id_shared_fwd (T : Type) (ls : list_t T) : Result (list_t T) := /- [loops::list_nth_mut_loop_with_id] -/ divergent def list_nth_mut_loop_with_id_loop_fwd (T : Type) (i : U32) (ls : list_t T) : Result T := - match h: ls with + match ls with | list_t.Cons x tl => if i = (U32.ofInt 0 (by intlit)) then Result.ret x @@ -226,7 +228,7 @@ def list_nth_mut_loop_with_id_fwd /- [loops::list_nth_mut_loop_with_id] -/ divergent def list_nth_mut_loop_with_id_loop_back (T : Type) (i : U32) (ls : list_t T) (ret0 : T) : Result (list_t T) := - match h: ls with + match ls with | list_t.Cons x tl => if i = (U32.ofInt 0 (by intlit)) then Result.ret (list_t.Cons ret0 tl) @@ -248,7 +250,7 @@ def list_nth_mut_loop_with_id_back /- [loops::list_nth_shared_loop_with_id] -/ divergent def list_nth_shared_loop_with_id_loop_fwd (T : Type) (i : U32) (ls : list_t T) : Result T := - match h: ls with + match ls with | list_t.Cons x tl => if i = (U32.ofInt 0 (by intlit)) then Result.ret x @@ -268,9 +270,9 @@ def list_nth_shared_loop_with_id_fwd /- [loops::list_nth_mut_loop_pair] -/ divergent def list_nth_mut_loop_pair_loop_fwd (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := - match h: ls0 with + match ls0 with | list_t.Cons x0 tl0 => - match h: ls1 with + match ls1 with | list_t.Cons x1 tl1 => if i = (U32.ofInt 0 (by intlit)) then Result.ret (x0, x1) @@ -291,9 +293,9 @@ divergent def list_nth_mut_loop_pair_loop_back'a (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : Result (list_t T) := - match h: ls0 with + match ls0 with | list_t.Cons x0 tl0 => - match h: ls1 with + match ls1 with | list_t.Cons x1 tl1 => if i = (U32.ofInt 0 (by intlit)) then Result.ret (list_t.Cons ret0 tl0) @@ -317,9 +319,9 @@ divergent def list_nth_mut_loop_pair_loop_back'b (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : Result (list_t T) := - match h: ls0 with + match ls0 with | list_t.Cons x0 tl0 => - match h: ls1 with + match ls1 with | list_t.Cons x1 tl1 => if i = (U32.ofInt 0 (by intlit)) then Result.ret (list_t.Cons ret0 tl1) @@ -341,9 +343,9 @@ def list_nth_mut_loop_pair_back'b /- [loops::list_nth_shared_loop_pair] -/ divergent def list_nth_shared_loop_pair_loop_fwd (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := - match h: ls0 with + match ls0 with | list_t.Cons x0 tl0 => - match h: ls1 with + match ls1 with | list_t.Cons x1 tl1 => if i = (U32.ofInt 0 (by intlit)) then Result.ret (x0, x1) @@ -362,9 +364,9 @@ def list_nth_shared_loop_pair_fwd /- [loops::list_nth_mut_loop_pair_merge] -/ divergent def list_nth_mut_loop_pair_merge_loop_fwd (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := - match h: ls0 with + match ls0 with | list_t.Cons x0 tl0 => - match h: ls1 with + match ls1 with | list_t.Cons x1 tl1 => if i = (U32.ofInt 0 (by intlit)) then Result.ret (x0, x1) @@ -385,9 +387,9 @@ divergent def list_nth_mut_loop_pair_merge_loop_back (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : (T × T)) : Result ((list_t T) × (list_t T)) := - match h: ls0 with + match ls0 with | list_t.Cons x0 tl0 => - match h: ls1 with + match ls1 with | list_t.Cons x1 tl1 => if i = (U32.ofInt 0 (by intlit)) then @@ -412,9 +414,9 @@ def list_nth_mut_loop_pair_merge_back /- [loops::list_nth_shared_loop_pair_merge] -/ divergent def list_nth_shared_loop_pair_merge_loop_fwd (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := - match h: ls0 with + match ls0 with | list_t.Cons x0 tl0 => - match h: ls1 with + match ls1 with | list_t.Cons x1 tl1 => if i = (U32.ofInt 0 (by intlit)) then Result.ret (x0, x1) @@ -433,9 +435,9 @@ def list_nth_shared_loop_pair_merge_fwd /- [loops::list_nth_mut_shared_loop_pair] -/ divergent def list_nth_mut_shared_loop_pair_loop_fwd (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := - match h: ls0 with + match ls0 with | list_t.Cons x0 tl0 => - match h: ls1 with + match ls1 with | list_t.Cons x1 tl1 => if i = (U32.ofInt 0 (by intlit)) then Result.ret (x0, x1) @@ -456,9 +458,9 @@ divergent def list_nth_mut_shared_loop_pair_loop_back (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : Result (list_t T) := - match h: ls0 with + match ls0 with | list_t.Cons x0 tl0 => - match h: ls1 with + match ls1 with | list_t.Cons x1 tl1 => if i = (U32.ofInt 0 (by intlit)) then Result.ret (list_t.Cons ret0 tl0) @@ -481,9 +483,9 @@ def list_nth_mut_shared_loop_pair_back /- [loops::list_nth_mut_shared_loop_pair_merge] -/ divergent def list_nth_mut_shared_loop_pair_merge_loop_fwd (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := - match h: ls0 with + match ls0 with | list_t.Cons x0 tl0 => - match h: ls1 with + match ls1 with | list_t.Cons x1 tl1 => if i = (U32.ofInt 0 (by intlit)) then Result.ret (x0, x1) @@ -504,9 +506,9 @@ divergent def list_nth_mut_shared_loop_pair_merge_loop_back (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : Result (list_t T) := - match h: ls0 with + match ls0 with | list_t.Cons x0 tl0 => - match h: ls1 with + match ls1 with | list_t.Cons x1 tl1 => if i = (U32.ofInt 0 (by intlit)) then Result.ret (list_t.Cons ret0 tl0) @@ -529,9 +531,9 @@ def list_nth_mut_shared_loop_pair_merge_back /- [loops::list_nth_shared_mut_loop_pair] -/ divergent def list_nth_shared_mut_loop_pair_loop_fwd (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := - match h: ls0 with + match ls0 with | list_t.Cons x0 tl0 => - match h: ls1 with + match ls1 with | list_t.Cons x1 tl1 => if i = (U32.ofInt 0 (by intlit)) then Result.ret (x0, x1) @@ -552,9 +554,9 @@ divergent def list_nth_shared_mut_loop_pair_loop_back (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : Result (list_t T) := - match h: ls0 with + match ls0 with | list_t.Cons x0 tl0 => - match h: ls1 with + match ls1 with | list_t.Cons x1 tl1 => if i = (U32.ofInt 0 (by intlit)) then Result.ret (list_t.Cons ret0 tl1) @@ -577,9 +579,9 @@ def list_nth_shared_mut_loop_pair_back /- [loops::list_nth_shared_mut_loop_pair_merge] -/ divergent def list_nth_shared_mut_loop_pair_merge_loop_fwd (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := - match h: ls0 with + match ls0 with | list_t.Cons x0 tl0 => - match h: ls1 with + match ls1 with | list_t.Cons x1 tl1 => if i = (U32.ofInt 0 (by intlit)) then Result.ret (x0, x1) @@ -600,9 +602,9 @@ divergent def list_nth_shared_mut_loop_pair_merge_loop_back (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : Result (list_t T) := - match h: ls0 with + match ls0 with | list_t.Cons x0 tl0 => - match h: ls1 with + match ls1 with | list_t.Cons x1 tl1 => if i = (U32.ofInt 0 (by intlit)) then Result.ret (list_t.Cons ret0 tl1) @@ -622,3 +624,4 @@ def list_nth_shared_mut_loop_pair_merge_back := list_nth_shared_mut_loop_pair_merge_loop_back T ls0 ls1 i ret0 +end Loops diff --git a/tests/lean/Loops/Types.lean b/tests/lean/Loops/Types.lean index e14f9766..ca0403e9 100644 --- a/tests/lean/Loops/Types.lean +++ b/tests/lean/Loops/Types.lean @@ -3,8 +3,11 @@ import Base open Primitives +namespace Loops + /- [loops::List] -/ inductive list_t (T : Type) := -| Cons : T -> list_t T -> list_t T +| Cons : T → list_t T → list_t T | Nil : list_t T +end Loops diff --git a/tests/lean/NoNestedBorrows.lean b/tests/lean/NoNestedBorrows.lean index 67ef4b20..769bb311 100644 --- a/tests/lean/NoNestedBorrows.lean +++ b/tests/lean/NoNestedBorrows.lean @@ -3,6 +3,8 @@ import Base open Primitives +namespace NoNestedBorrows + /- [no_nested_borrows::Pair] -/ structure pair_t (T1 T2 : Type) where pair_x : T1 @@ -10,12 +12,12 @@ structure pair_t (T1 T2 : Type) where /- [no_nested_borrows::List] -/ inductive list_t (T : Type) := -| Cons : T -> list_t T -> list_t T +| Cons : T → list_t T → list_t T | Nil : list_t T /- [no_nested_borrows::One] -/ inductive one_t (T1 : Type) := -| One : T1 -> one_t T1 +| One : T1 → one_t T1 /- [no_nested_borrows::EmptyEnum] -/ inductive empty_enum_t := @@ -31,8 +33,8 @@ structure empty_struct_t where /- [no_nested_borrows::Sum] -/ inductive sum_t (T1 T2 : Type) := -| Left : T1 -> sum_t T1 T2 -| Right : T2 -> sum_t T1 T2 +| Left : T1 → sum_t T1 T2 +| Right : T2 → sum_t T1 T2 /- [no_nested_borrows::neg_test] -/ def neg_test_fwd (x : I32) : Result I32 := @@ -175,7 +177,7 @@ def test_copy_int_fwd : Result Unit := /- [no_nested_borrows::is_cons] -/ def is_cons_fwd (T : Type) (l : list_t T) : Result Bool := - match h: l with + match l with | list_t.Cons t l0 => Result.ret true | list_t.Nil => Result.ret false @@ -193,7 +195,7 @@ def test_is_cons_fwd : Result Unit := /- [no_nested_borrows::split_list] -/ def split_list_fwd (T : Type) (l : list_t T) : Result (T × (list_t T)) := - match h: l with + match l with | list_t.Cons hd tl => Result.ret (hd, tl) | list_t.Nil => Result.fail Error.panic @@ -254,19 +256,19 @@ mutual /- [no_nested_borrows::NodeElem] -/ inductive node_elem_t (T : Type) := -| Cons : tree_t T -> node_elem_t T -> node_elem_t T +| Cons : tree_t T → node_elem_t T → node_elem_t T | Nil : node_elem_t T /- [no_nested_borrows::Tree] -/ inductive tree_t (T : Type) := -| Leaf : T -> tree_t T -| Node : T -> node_elem_t T -> tree_t T -> tree_t T +| Leaf : T → tree_t T +| Node : T → node_elem_t T → tree_t T → tree_t T end /- [no_nested_borrows::list_length] -/ divergent def list_length_fwd (T : Type) (l : list_t T) : Result U32 := - match h: l with + match l with | list_t.Cons t l1 => do let i ← list_length_fwd T l1 @@ -276,7 +278,7 @@ divergent def list_length_fwd (T : Type) (l : list_t T) : Result U32 := /- [no_nested_borrows::list_nth_shared] -/ divergent def list_nth_shared_fwd (T : Type) (l : list_t T) (i : U32) : Result T := - match h: l with + match l with | list_t.Cons x tl => if i = (U32.ofInt 0 (by intlit)) then Result.ret x @@ -289,7 +291,7 @@ divergent def list_nth_shared_fwd /- [no_nested_borrows::list_nth_mut] -/ divergent def list_nth_mut_fwd (T : Type) (l : list_t T) (i : U32) : Result T := - match h: l with + match l with | list_t.Cons x tl => if i = (U32.ofInt 0 (by intlit)) then Result.ret x @@ -301,7 +303,7 @@ divergent def list_nth_mut_fwd /- [no_nested_borrows::list_nth_mut] -/ divergent def list_nth_mut_back (T : Type) (l : list_t T) (i : U32) (ret0 : T) : Result (list_t T) := - match h: l with + match l with | list_t.Cons x tl => if i = (U32.ofInt 0 (by intlit)) then Result.ret (list_t.Cons ret0 tl) @@ -315,7 +317,7 @@ divergent def list_nth_mut_back /- [no_nested_borrows::list_rev_aux] -/ divergent def list_rev_aux_fwd (T : Type) (li : list_t T) (lo : list_t T) : Result (list_t T) := - match h: li with + match li with | list_t.Cons hd tl => list_rev_aux_fwd T tl (list_t.Cons hd lo) | list_t.Nil => Result.ret lo @@ -531,7 +533,7 @@ def test_shared_borrow_bool2_fwd : Result U32 := /- [no_nested_borrows::test_shared_borrow_enum1] -/ def test_shared_borrow_enum1_fwd (l : list_t U32) : Result U32 := - match h: l with + match l with | list_t.Cons i l0 => Result.ret (U32.ofInt 1 (by intlit)) | list_t.Nil => Result.ret (U32.ofInt 0 (by intlit)) @@ -539,3 +541,4 @@ def test_shared_borrow_enum1_fwd (l : list_t U32) : Result U32 := def test_shared_borrow_enum2_fwd : Result U32 := Result.ret (U32.ofInt 0 (by intlit)) +end NoNestedBorrows diff --git a/tests/lean/Paper.lean b/tests/lean/Paper.lean index 9019b694..edcb5c1b 100644 --- a/tests/lean/Paper.lean +++ b/tests/lean/Paper.lean @@ -3,6 +3,8 @@ import Base open Primitives +namespace Paper + /- [paper::ref_incr] -/ def ref_incr_fwd_back (x : I32) : Result I32 := x + (I32.ofInt 1 (by intlit)) @@ -56,13 +58,13 @@ def test_choose_fwd : Result Unit := /- [paper::List] -/ inductive list_t (T : Type) := -| Cons : T -> list_t T -> list_t T +| Cons : T → list_t T → list_t T | Nil : list_t T /- [paper::list_nth_mut] -/ divergent def list_nth_mut_fwd (T : Type) (l : list_t T) (i : U32) : Result T := - match h: l with + match l with | list_t.Cons x tl => if i = (U32.ofInt 0 (by intlit)) then Result.ret x @@ -74,7 +76,7 @@ divergent def list_nth_mut_fwd /- [paper::list_nth_mut] -/ divergent def list_nth_mut_back (T : Type) (l : list_t T) (i : U32) (ret0 : T) : Result (list_t T) := - match h: l with + match l with | list_t.Cons x tl => if i = (U32.ofInt 0 (by intlit)) then Result.ret (list_t.Cons ret0 tl) @@ -87,7 +89,7 @@ divergent def list_nth_mut_back /- [paper::sum] -/ divergent def sum_fwd (l : list_t I32) : Result I32 := - match h: l with + match l with | list_t.Cons x tl => do let i ← sum_fwd tl x + i @@ -123,3 +125,4 @@ def call_choose_fwd (p : (U32 × U32)) : Result U32 := let (px0, _) ← choose_back U32 true px py pz0 Result.ret px0 +end Paper diff --git a/tests/lean/PoloniusList.lean b/tests/lean/PoloniusList.lean index 671f54ea..0f2a05e3 100644 --- a/tests/lean/PoloniusList.lean +++ b/tests/lean/PoloniusList.lean @@ -3,15 +3,17 @@ import Base open Primitives +namespace PoloniusList + /- [polonius_list::List] -/ inductive list_t (T : Type) := -| Cons : T -> list_t T -> list_t T +| Cons : T → list_t T → list_t T | Nil : list_t T /- [polonius_list::get_list_at_x] -/ divergent def get_list_at_x_fwd (ls : list_t U32) (x : U32) : Result (list_t U32) := - match h: ls with + match ls with | list_t.Cons hd tl => if hd = x then Result.ret (list_t.Cons hd tl) @@ -21,7 +23,7 @@ divergent def get_list_at_x_fwd /- [polonius_list::get_list_at_x] -/ divergent def get_list_at_x_back (ls : list_t U32) (x : U32) (ret0 : list_t U32) : Result (list_t U32) := - match h: ls with + match ls with | list_t.Cons hd tl => if hd = x then Result.ret ret0 @@ -31,3 +33,4 @@ divergent def get_list_at_x_back Result.ret (list_t.Cons hd tl0) | list_t.Nil => Result.ret ret0 +end PoloniusList diff --git a/tests/lean/lake-manifest.json b/tests/lean/lake-manifest.json index 1397c6f0..ccd6d61a 100644 --- a/tests/lean/lake-manifest.json +++ b/tests/lean/lake-manifest.json @@ -11,7 +11,7 @@ {"git": {"url": "https://github.com/leanprover-community/mathlib4.git", "subDir?": null, - "rev": "cb02d09e1d5611d22efc2b406e7893f246b2f51e", + "rev": "409bee4eabf8072c4569950c3c2f310afd203abf", "name": "mathlib", "inputRev?": null}}, {"git": diff --git a/tests/lean/lakefile.lean b/tests/lean/lakefile.lean index da4293dd..217e533f 100644 --- a/tests/lean/lakefile.lean +++ b/tests/lean/lakefile.lean @@ -11,4 +11,12 @@ package «tests» {} @[default_target] lean_lib «Tests» {} +lean_lib betreeMain +lean_lib constants +lean_lib external lean_lib hashmap +lean_lib hashmapMain +lean_lib loops +lean_lib noNestedBorrows +lean_lib paper +lean_lib poloniusList -- cgit v1.2.3 From 442caaf62e4a217b9a10116c4e529c49f83c4efd Mon Sep 17 00:00:00 2001 From: Son Ho Date: Tue, 4 Jul 2023 22:45:02 +0200 Subject: Fix an issue with mkSigmasVal --- tests/lean/lake-manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/lean') diff --git a/tests/lean/lake-manifest.json b/tests/lean/lake-manifest.json index ccd6d61a..637bda23 100644 --- a/tests/lean/lake-manifest.json +++ b/tests/lean/lake-manifest.json @@ -11,7 +11,7 @@ {"git": {"url": "https://github.com/leanprover-community/mathlib4.git", "subDir?": null, - "rev": "409bee4eabf8072c4569950c3c2f310afd203abf", + "rev": "cc5d11f24e1b92db65ec3389bb5142f4b2d7670e", "name": "mathlib", "inputRev?": null}}, {"git": -- cgit v1.2.3 From 0a0445c72e005c328b4764f5fb0f8f38e7a55d60 Mon Sep 17 00:00:00 2001 From: Son Ho Date: Wed, 5 Jul 2023 14:52:23 +0200 Subject: Start using namespaces in the Lean backend --- tests/lean/BetreeMain/ExternalFuns.lean | 9 - tests/lean/BetreeMain/Funs.lean | 498 +++++++++++----------- tests/lean/BetreeMain/FunsExternal.lean | 35 ++ tests/lean/BetreeMain/FunsExternal_Template.lean | 31 ++ tests/lean/BetreeMain/Opaque.lean | 35 -- tests/lean/BetreeMain/Types.lean | 5 +- tests/lean/Constants.lean | 9 +- tests/lean/External/ExternalFuns.lean | 9 - tests/lean/External/Funs.lean | 36 +- tests/lean/External/FunsExternal.lean | 33 ++ tests/lean/External/FunsExternal_Template.lean | 29 ++ tests/lean/External/Opaque.lean | 14 +- tests/lean/External/Types.lean | 5 +- tests/lean/Hashmap/Funs.lean | 192 +++++---- tests/lean/Hashmap/Types.lean | 5 +- tests/lean/HashmapMain/ExternalFuns.lean | 9 - tests/lean/HashmapMain/Funs.lean | 221 +++++----- tests/lean/HashmapMain/FunsExternal.lean | 17 + tests/lean/HashmapMain/FunsExternal_Template.lean | 16 + tests/lean/HashmapMain/Opaque.lean | 8 +- tests/lean/HashmapMain/Types.lean | 5 +- tests/lean/Loops/Funs.lean | 5 +- tests/lean/Loops/Types.lean | 5 +- tests/lean/NoNestedBorrows.lean | 5 +- tests/lean/Paper.lean | 5 +- tests/lean/PoloniusList.lean | 5 +- tests/lean/Tests.lean | 9 - tests/lean/lakefile.lean | 21 +- 28 files changed, 672 insertions(+), 604 deletions(-) delete mode 100644 tests/lean/BetreeMain/ExternalFuns.lean create mode 100644 tests/lean/BetreeMain/FunsExternal.lean create mode 100644 tests/lean/BetreeMain/FunsExternal_Template.lean delete mode 100644 tests/lean/BetreeMain/Opaque.lean delete mode 100644 tests/lean/External/ExternalFuns.lean create mode 100644 tests/lean/External/FunsExternal.lean create mode 100644 tests/lean/External/FunsExternal_Template.lean delete mode 100644 tests/lean/HashmapMain/ExternalFuns.lean create mode 100644 tests/lean/HashmapMain/FunsExternal.lean create mode 100644 tests/lean/HashmapMain/FunsExternal_Template.lean delete mode 100644 tests/lean/Tests.lean (limited to 'tests/lean') diff --git a/tests/lean/BetreeMain/ExternalFuns.lean b/tests/lean/BetreeMain/ExternalFuns.lean deleted file mode 100644 index 59beb514..00000000 --- a/tests/lean/BetreeMain/ExternalFuns.lean +++ /dev/null @@ -1,9 +0,0 @@ -import Base -import BetreeMain.Types -import BetreeMain.Opaque - -namespace BetreeMain - -def opaque_defs : OpaqueDefs := by sorry - -end BetreeMain diff --git a/tests/lean/BetreeMain/Funs.lean b/tests/lean/BetreeMain/Funs.lean index 2eb7fa1f..78e14146 100644 --- a/tests/lean/BetreeMain/Funs.lean +++ b/tests/lean/BetreeMain/Funs.lean @@ -2,59 +2,57 @@ -- [betree_main]: function definitions import Base import BetreeMain.Types -import BetreeMain.ExternalFuns +import BetreeMain.FunsExternal open Primitives - -namespace BetreeMain +namespace betree_main /- [betree_main::betree::load_internal_node] -/ -def betree_load_internal_node_fwd +def betree.load_internal_node_fwd (id : U64) (st : State) : Result (State × (betree_list_t (U64 × betree_message_t))) := - opaque_defs.betree_utils_load_internal_node_fwd id st + betree_utils.load_internal_node_fwd id st /- [betree_main::betree::store_internal_node] -/ -def betree_store_internal_node_fwd +def betree.store_internal_node_fwd (id : U64) (content : betree_list_t (U64 × betree_message_t)) (st : State) : Result (State × Unit) := do - let (st0, _) ← - opaque_defs.betree_utils_store_internal_node_fwd id content st + let (st0, _) ← betree_utils.store_internal_node_fwd id content st Result.ret (st0, ()) /- [betree_main::betree::load_leaf_node] -/ -def betree_load_leaf_node_fwd +def betree.load_leaf_node_fwd (id : U64) (st : State) : Result (State × (betree_list_t (U64 × U64))) := - opaque_defs.betree_utils_load_leaf_node_fwd id st + betree_utils.load_leaf_node_fwd id st /- [betree_main::betree::store_leaf_node] -/ -def betree_store_leaf_node_fwd +def betree.store_leaf_node_fwd (id : U64) (content : betree_list_t (U64 × U64)) (st : State) : Result (State × Unit) := do - let (st0, _) ← opaque_defs.betree_utils_store_leaf_node_fwd id content st + let (st0, _) ← betree_utils.store_leaf_node_fwd id content st Result.ret (st0, ()) /- [betree_main::betree::fresh_node_id] -/ -def betree_fresh_node_id_fwd (counter : U64) : Result U64 := +def betree.fresh_node_id_fwd (counter : U64) : Result U64 := do let _ ← counter + (U64.ofInt 1 (by intlit)) Result.ret counter /- [betree_main::betree::fresh_node_id] -/ -def betree_fresh_node_id_back (counter : U64) : Result U64 := +def betree.fresh_node_id_back (counter : U64) : Result U64 := counter + (U64.ofInt 1 (by intlit)) /- [betree_main::betree::NodeIdCounter::{0}::new] -/ -def betree_node_id_counter_new_fwd : Result betree_node_id_counter_t := +def betree.NodeIdCounter.new_fwd : Result betree_node_id_counter_t := Result.ret { betree_node_id_counter_next_node_id := (U64.ofInt 0 (by intlit)) } /- [betree_main::betree::NodeIdCounter::{0}::fresh_id] -/ -def betree_node_id_counter_fresh_id_fwd +def betree.NodeIdCounter.fresh_id_fwd (self : betree_node_id_counter_t) : Result U64 := do let _ ← self.betree_node_id_counter_next_node_id + @@ -62,7 +60,7 @@ def betree_node_id_counter_fresh_id_fwd Result.ret self.betree_node_id_counter_next_node_id /- [betree_main::betree::NodeIdCounter::{0}::fresh_id] -/ -def betree_node_id_counter_fresh_id_back +def betree.NodeIdCounter.fresh_id_back (self : betree_node_id_counter_t) : Result betree_node_id_counter_t := do let i ← self.betree_node_id_counter_next_node_id + @@ -75,7 +73,7 @@ def core_num_u64_max_body : Result U64 := def core_num_u64_max_c : U64 := eval_global core_num_u64_max_body (by simp) /- [betree_main::betree::upsert_update] -/ -def betree_upsert_update_fwd +def betree.upsert_update_fwd (prev : Option U64) (st : betree_upsert_fun_state_t) : Result U64 := match prev with | Option.none => @@ -96,17 +94,17 @@ def betree_upsert_update_fwd else Result.ret (U64.ofInt 0 (by intlit)) /- [betree_main::betree::List::{1}::len] -/ -divergent def betree_list_len_fwd +divergent def betree.List.len_fwd (T : Type) (self : betree_list_t T) : Result U64 := match self with | betree_list_t.Cons t tl => do - let i ← betree_list_len_fwd T tl + let i ← betree.List.len_fwd T tl (U64.ofInt 1 (by intlit)) + i | betree_list_t.Nil => Result.ret (U64.ofInt 0 (by intlit)) /- [betree_main::betree::List::{1}::split_at] -/ -divergent def betree_list_split_at_fwd +divergent def betree.List.split_at_fwd (T : Type) (self : betree_list_t T) (n : U64) : Result ((betree_list_t T) × (betree_list_t T)) := @@ -117,28 +115,28 @@ divergent def betree_list_split_at_fwd | betree_list_t.Cons hd tl => do let i ← n - (U64.ofInt 1 (by intlit)) - let p ← betree_list_split_at_fwd T tl i + let p ← betree.List.split_at_fwd T tl i let (ls0, ls1) := p let l := ls0 Result.ret (betree_list_t.Cons hd l, ls1) | betree_list_t.Nil => Result.fail Error.panic /- [betree_main::betree::List::{1}::push_front] -/ -def betree_list_push_front_fwd_back +def betree.List.push_front_fwd_back (T : Type) (self : betree_list_t T) (x : T) : Result (betree_list_t T) := let tl := mem_replace_fwd (betree_list_t T) self betree_list_t.Nil let l := tl Result.ret (betree_list_t.Cons x l) /- [betree_main::betree::List::{1}::pop_front] -/ -def betree_list_pop_front_fwd (T : Type) (self : betree_list_t T) : Result T := +def betree.List.pop_front_fwd (T : Type) (self : betree_list_t T) : Result T := let ls := mem_replace_fwd (betree_list_t T) self betree_list_t.Nil match ls with | betree_list_t.Cons x tl => Result.ret x | betree_list_t.Nil => Result.fail Error.panic /- [betree_main::betree::List::{1}::pop_front] -/ -def betree_list_pop_front_back +def betree.List.pop_front_back (T : Type) (self : betree_list_t T) : Result (betree_list_t T) := let ls := mem_replace_fwd (betree_list_t T) self betree_list_t.Nil match ls with @@ -146,13 +144,13 @@ def betree_list_pop_front_back | betree_list_t.Nil => Result.fail Error.panic /- [betree_main::betree::List::{1}::hd] -/ -def betree_list_hd_fwd (T : Type) (self : betree_list_t T) : Result T := +def betree.List.hd_fwd (T : Type) (self : betree_list_t T) : Result T := match self with | betree_list_t.Cons hd l => Result.ret hd | betree_list_t.Nil => Result.fail Error.panic /- [betree_main::betree::List::{2}::head_has_key] -/ -def betree_list_head_has_key_fwd +def betree.List.head_has_key_fwd (T : Type) (self : betree_list_t (U64 × T)) (key : U64) : Result Bool := match self with | betree_list_t.Cons hd l => let (i, _) := hd @@ -160,7 +158,7 @@ def betree_list_head_has_key_fwd | betree_list_t.Nil => Result.ret false /- [betree_main::betree::List::{2}::partition_at_pivot] -/ -divergent def betree_list_partition_at_pivot_fwd +divergent def betree.List.partition_at_pivot_fwd (T : Type) (self : betree_list_t (U64 × T)) (pivot : U64) : Result ((betree_list_t (U64 × T)) × (betree_list_t (U64 × T))) := @@ -171,14 +169,14 @@ divergent def betree_list_partition_at_pivot_fwd then Result.ret (betree_list_t.Nil, betree_list_t.Cons (i, t) tl) else do - let p ← betree_list_partition_at_pivot_fwd T tl pivot + let p ← betree.List.partition_at_pivot_fwd T tl pivot let (ls0, ls1) := p let l := ls0 Result.ret (betree_list_t.Cons (i, t) l, ls1) | betree_list_t.Nil => Result.ret (betree_list_t.Nil, betree_list_t.Nil) /- [betree_main::betree::Leaf::{3}::split] -/ -def betree_leaf_split_fwd +def betree.Leaf.split_fwd (self : betree_leaf_t) (content : betree_list_t (U64 × U64)) (params : betree_params_t) (node_id_cnt : betree_node_id_counter_t) (st : State) : @@ -186,16 +184,16 @@ def betree_leaf_split_fwd := do let p ← - betree_list_split_at_fwd (U64 × U64) content + betree.List.split_at_fwd (U64 × U64) content params.betree_params_split_size let (content0, content1) := p - let p0 ← betree_list_hd_fwd (U64 × U64) content1 + let p0 ← betree.List.hd_fwd (U64 × U64) content1 let (pivot, _) := p0 - let id0 ← betree_node_id_counter_fresh_id_fwd node_id_cnt - let node_id_cnt0 ← betree_node_id_counter_fresh_id_back node_id_cnt - let id1 ← betree_node_id_counter_fresh_id_fwd node_id_cnt0 - let (st0, _) ← betree_store_leaf_node_fwd id0 content0 st - let (st1, _) ← betree_store_leaf_node_fwd id1 content1 st0 + let id0 ← betree.NodeIdCounter.fresh_id_fwd node_id_cnt + let node_id_cnt0 ← betree.NodeIdCounter.fresh_id_back node_id_cnt + let id1 ← betree.NodeIdCounter.fresh_id_fwd node_id_cnt0 + let (st0, _) ← betree.store_leaf_node_fwd id0 content0 st + let (st1, _) ← betree.store_leaf_node_fwd id1 content1 st0 let n := betree_node_t.Leaf { betree_leaf_id := id0, @@ -209,7 +207,7 @@ def betree_leaf_split_fwd Result.ret (st1, betree_internal_t.mk self.betree_leaf_id pivot n n0) /- [betree_main::betree::Leaf::{3}::split] -/ -def betree_leaf_split_back +def betree.Leaf.split_back (self : betree_leaf_t) (content : betree_list_t (U64 × U64)) (params : betree_params_t) (node_id_cnt : betree_node_id_counter_t) (st : State) : @@ -217,19 +215,19 @@ def betree_leaf_split_back := do let p ← - betree_list_split_at_fwd (U64 × U64) content + betree.List.split_at_fwd (U64 × U64) content params.betree_params_split_size let (content0, content1) := p - let _ ← betree_list_hd_fwd (U64 × U64) content1 - let id0 ← betree_node_id_counter_fresh_id_fwd node_id_cnt - let node_id_cnt0 ← betree_node_id_counter_fresh_id_back node_id_cnt - let id1 ← betree_node_id_counter_fresh_id_fwd node_id_cnt0 - let (st0, _) ← betree_store_leaf_node_fwd id0 content0 st - let _ ← betree_store_leaf_node_fwd id1 content1 st0 - betree_node_id_counter_fresh_id_back node_id_cnt0 + let _ ← betree.List.hd_fwd (U64 × U64) content1 + let id0 ← betree.NodeIdCounter.fresh_id_fwd node_id_cnt + let node_id_cnt0 ← betree.NodeIdCounter.fresh_id_back node_id_cnt + let id1 ← betree.NodeIdCounter.fresh_id_fwd node_id_cnt0 + let (st0, _) ← betree.store_leaf_node_fwd id0 content0 st + let _ ← betree.store_leaf_node_fwd id1 content1 st0 + betree.NodeIdCounter.fresh_id_back node_id_cnt0 /- [betree_main::betree::Node::{5}::lookup_in_bindings] -/ -divergent def betree_node_lookup_in_bindings_fwd +divergent def betree.Node.lookup_in_bindings_fwd (key : U64) (bindings : betree_list_t (U64 × U64)) : Result (Option U64) := match bindings with | betree_list_t.Cons hd tl => @@ -239,11 +237,11 @@ divergent def betree_node_lookup_in_bindings_fwd else if i > key then Result.ret Option.none - else betree_node_lookup_in_bindings_fwd key tl + else betree.Node.lookup_in_bindings_fwd key tl | betree_list_t.Nil => Result.ret Option.none /- [betree_main::betree::Node::{5}::lookup_first_message_for_key] -/ -divergent def betree_node_lookup_first_message_for_key_fwd +divergent def betree.Node.lookup_first_message_for_key_fwd (key : U64) (msgs : betree_list_t (U64 × betree_message_t)) : Result (betree_list_t (U64 × betree_message_t)) := @@ -252,11 +250,11 @@ divergent def betree_node_lookup_first_message_for_key_fwd let (i, m) := x if i >= key then Result.ret (betree_list_t.Cons (i, m) next_msgs) - else betree_node_lookup_first_message_for_key_fwd key next_msgs + else betree.Node.lookup_first_message_for_key_fwd key next_msgs | betree_list_t.Nil => Result.ret betree_list_t.Nil /- [betree_main::betree::Node::{5}::lookup_first_message_for_key] -/ -divergent def betree_node_lookup_first_message_for_key_back +divergent def betree.Node.lookup_first_message_for_key_back (key : U64) (msgs : betree_list_t (U64 × betree_message_t)) (ret0 : betree_list_t (U64 × betree_message_t)) : Result (betree_list_t (U64 × betree_message_t)) @@ -269,70 +267,70 @@ divergent def betree_node_lookup_first_message_for_key_back else do let next_msgs0 ← - betree_node_lookup_first_message_for_key_back key next_msgs ret0 + betree.Node.lookup_first_message_for_key_back key next_msgs ret0 Result.ret (betree_list_t.Cons (i, m) next_msgs0) | betree_list_t.Nil => Result.ret ret0 /- [betree_main::betree::Node::{5}::apply_upserts] -/ -divergent def betree_node_apply_upserts_fwd +divergent def betree.Node.apply_upserts_fwd (msgs : betree_list_t (U64 × betree_message_t)) (prev : Option U64) (key : U64) (st : State) : Result (State × U64) := do - let b ← betree_list_head_has_key_fwd betree_message_t msgs key + let b ← betree.List.head_has_key_fwd betree_message_t msgs key if b then do - let msg ← betree_list_pop_front_fwd (U64 × betree_message_t) msgs + let msg ← betree.List.pop_front_fwd (U64 × betree_message_t) msgs let (_, m) := msg match m with | betree_message_t.Insert i => Result.fail Error.panic | betree_message_t.Delete => Result.fail Error.panic | betree_message_t.Upsert s => do - let v ← betree_upsert_update_fwd prev s + let v ← betree.upsert_update_fwd prev s let msgs0 ← - betree_list_pop_front_back (U64 × betree_message_t) msgs - betree_node_apply_upserts_fwd msgs0 (Option.some v) key st + betree.List.pop_front_back (U64 × betree_message_t) msgs + betree.Node.apply_upserts_fwd msgs0 (Option.some v) key st else do - let (st0, v) ← opaque_defs.core_option_option_unwrap_fwd U64 prev st + let (st0, v) ← core.option.Option.unwrap_fwd U64 prev st let _ ← - betree_list_push_front_fwd_back (U64 × betree_message_t) msgs (key, + betree.List.push_front_fwd_back (U64 × betree_message_t) msgs (key, betree_message_t.Insert v) Result.ret (st0, v) /- [betree_main::betree::Node::{5}::apply_upserts] -/ -divergent def betree_node_apply_upserts_back +divergent def betree.Node.apply_upserts_back (msgs : betree_list_t (U64 × betree_message_t)) (prev : Option U64) (key : U64) (st : State) : Result (betree_list_t (U64 × betree_message_t)) := do - let b ← betree_list_head_has_key_fwd betree_message_t msgs key + let b ← betree.List.head_has_key_fwd betree_message_t msgs key if b then do - let msg ← betree_list_pop_front_fwd (U64 × betree_message_t) msgs + let msg ← betree.List.pop_front_fwd (U64 × betree_message_t) msgs let (_, m) := msg match m with | betree_message_t.Insert i => Result.fail Error.panic | betree_message_t.Delete => Result.fail Error.panic | betree_message_t.Upsert s => do - let v ← betree_upsert_update_fwd prev s + let v ← betree.upsert_update_fwd prev s let msgs0 ← - betree_list_pop_front_back (U64 × betree_message_t) msgs - betree_node_apply_upserts_back msgs0 (Option.some v) key st + betree.List.pop_front_back (U64 × betree_message_t) msgs + betree.Node.apply_upserts_back msgs0 (Option.some v) key st else do - let (_, v) ← opaque_defs.core_option_option_unwrap_fwd U64 prev st - betree_list_push_front_fwd_back (U64 × betree_message_t) msgs (key, + let (_, v) ← core.option.Option.unwrap_fwd U64 prev st + betree.List.push_front_fwd_back (U64 × betree_message_t) msgs (key, betree_message_t.Insert v) /- [betree_main::betree::Node::{5}::lookup] -/ -mutual divergent def betree_node_lookup_fwd +mutual divergent def betree.Node.lookup_fwd (self : betree_node_t) (key : U64) (st : State) : Result (State × (Option U64)) := @@ -340,8 +338,8 @@ mutual divergent def betree_node_lookup_fwd | betree_node_t.Internal node => do let ⟨ i, i0, n, n0 ⟩ := node - let (st0, msgs) ← betree_load_internal_node_fwd i st - let pending ← betree_node_lookup_first_message_for_key_fwd key msgs + let (st0, msgs) ← betree.load_internal_node_fwd i st + let pending ← betree.Node.lookup_first_message_for_key_fwd key msgs match pending with | betree_list_t.Cons p l => let (k, msg) := p @@ -349,10 +347,10 @@ mutual divergent def betree_node_lookup_fwd then do let (st1, opt) ← - betree_internal_lookup_in_children_fwd (betree_internal_t.mk i i0 + betree.Internal.lookup_in_children_fwd (betree_internal_t.mk i i0 n n0) key st0 let _ ← - betree_node_lookup_first_message_for_key_back key msgs + betree.Node.lookup_first_message_for_key_back key msgs (betree_list_t.Cons (k, msg) l) Result.ret (st1, opt) else @@ -360,58 +358,58 @@ mutual divergent def betree_node_lookup_fwd | betree_message_t.Insert v => do let _ ← - betree_node_lookup_first_message_for_key_back key msgs + betree.Node.lookup_first_message_for_key_back key msgs (betree_list_t.Cons (k, betree_message_t.Insert v) l) Result.ret (st0, Option.some v) | betree_message_t.Delete => do let _ ← - betree_node_lookup_first_message_for_key_back key msgs + betree.Node.lookup_first_message_for_key_back key msgs (betree_list_t.Cons (k, betree_message_t.Delete) l) Result.ret (st0, Option.none) | betree_message_t.Upsert ufs => do let (st1, v) ← - betree_internal_lookup_in_children_fwd (betree_internal_t.mk i + betree.Internal.lookup_in_children_fwd (betree_internal_t.mk i i0 n n0) key st0 let (st2, v0) ← - betree_node_apply_upserts_fwd (betree_list_t.Cons (k, + betree.Node.apply_upserts_fwd (betree_list_t.Cons (k, betree_message_t.Upsert ufs) l) v key st1 let node0 ← - betree_internal_lookup_in_children_back (betree_internal_t.mk i + betree.Internal.lookup_in_children_back (betree_internal_t.mk i i0 n n0) key st0 let ⟨ i1, _, _, _ ⟩ := node0 let pending0 ← - betree_node_apply_upserts_back (betree_list_t.Cons (k, + betree.Node.apply_upserts_back (betree_list_t.Cons (k, betree_message_t.Upsert ufs) l) v key st1 let msgs0 ← - betree_node_lookup_first_message_for_key_back key msgs pending0 - let (st3, _) ← betree_store_internal_node_fwd i1 msgs0 st2 + betree.Node.lookup_first_message_for_key_back key msgs pending0 + let (st3, _) ← betree.store_internal_node_fwd i1 msgs0 st2 Result.ret (st3, Option.some v0) | betree_list_t.Nil => do let (st1, opt) ← - betree_internal_lookup_in_children_fwd (betree_internal_t.mk i i0 n + betree.Internal.lookup_in_children_fwd (betree_internal_t.mk i i0 n n0) key st0 let _ ← - betree_node_lookup_first_message_for_key_back key msgs + betree.Node.lookup_first_message_for_key_back key msgs betree_list_t.Nil Result.ret (st1, opt) | betree_node_t.Leaf node => do - let (st0, bindings) ← betree_load_leaf_node_fwd node.betree_leaf_id st - let opt ← betree_node_lookup_in_bindings_fwd key bindings + let (st0, bindings) ← betree.load_leaf_node_fwd node.betree_leaf_id st + let opt ← betree.Node.lookup_in_bindings_fwd key bindings Result.ret (st0, opt) /- [betree_main::betree::Node::{5}::lookup] -/ -divergent def betree_node_lookup_back +divergent def betree.Node.lookup_back (self : betree_node_t) (key : U64) (st : State) : Result betree_node_t := match self with | betree_node_t.Internal node => do let ⟨ i, i0, n, n0 ⟩ := node - let (st0, msgs) ← betree_load_internal_node_fwd i st - let pending ← betree_node_lookup_first_message_for_key_fwd key msgs + let (st0, msgs) ← betree.load_internal_node_fwd i st + let pending ← betree.Node.lookup_first_message_for_key_fwd key msgs match pending with | betree_list_t.Cons p l => let (k, msg) := p @@ -419,10 +417,10 @@ divergent def betree_node_lookup_back then do let _ ← - betree_node_lookup_first_message_for_key_back key msgs + betree.Node.lookup_first_message_for_key_back key msgs (betree_list_t.Cons (k, msg) l) let node0 ← - betree_internal_lookup_in_children_back (betree_internal_t.mk i + betree.Internal.lookup_in_children_back (betree_internal_t.mk i i0 n n0) key st0 Result.ret (betree_node_t.Internal node0) else @@ -430,64 +428,64 @@ divergent def betree_node_lookup_back | betree_message_t.Insert v => do let _ ← - betree_node_lookup_first_message_for_key_back key msgs + betree.Node.lookup_first_message_for_key_back key msgs (betree_list_t.Cons (k, betree_message_t.Insert v) l) Result.ret (betree_node_t.Internal (betree_internal_t.mk i i0 n n0)) | betree_message_t.Delete => do let _ ← - betree_node_lookup_first_message_for_key_back key msgs + betree.Node.lookup_first_message_for_key_back key msgs (betree_list_t.Cons (k, betree_message_t.Delete) l) Result.ret (betree_node_t.Internal (betree_internal_t.mk i i0 n n0)) | betree_message_t.Upsert ufs => do let (st1, v) ← - betree_internal_lookup_in_children_fwd (betree_internal_t.mk i + betree.Internal.lookup_in_children_fwd (betree_internal_t.mk i i0 n n0) key st0 let (st2, _) ← - betree_node_apply_upserts_fwd (betree_list_t.Cons (k, + betree.Node.apply_upserts_fwd (betree_list_t.Cons (k, betree_message_t.Upsert ufs) l) v key st1 let node0 ← - betree_internal_lookup_in_children_back (betree_internal_t.mk i + betree.Internal.lookup_in_children_back (betree_internal_t.mk i i0 n n0) key st0 let ⟨ i1, i2, n1, n2 ⟩ := node0 let pending0 ← - betree_node_apply_upserts_back (betree_list_t.Cons (k, + betree.Node.apply_upserts_back (betree_list_t.Cons (k, betree_message_t.Upsert ufs) l) v key st1 let msgs0 ← - betree_node_lookup_first_message_for_key_back key msgs pending0 - let _ ← betree_store_internal_node_fwd i1 msgs0 st2 + betree.Node.lookup_first_message_for_key_back key msgs pending0 + let _ ← betree.store_internal_node_fwd i1 msgs0 st2 Result.ret (betree_node_t.Internal (betree_internal_t.mk i1 i2 n1 n2)) | betree_list_t.Nil => do let _ ← - betree_node_lookup_first_message_for_key_back key msgs + betree.Node.lookup_first_message_for_key_back key msgs betree_list_t.Nil let node0 ← - betree_internal_lookup_in_children_back (betree_internal_t.mk i i0 + betree.Internal.lookup_in_children_back (betree_internal_t.mk i i0 n n0) key st0 Result.ret (betree_node_t.Internal node0) | betree_node_t.Leaf node => do - let (_, bindings) ← betree_load_leaf_node_fwd node.betree_leaf_id st - let _ ← betree_node_lookup_in_bindings_fwd key bindings + let (_, bindings) ← betree.load_leaf_node_fwd node.betree_leaf_id st + let _ ← betree.Node.lookup_in_bindings_fwd key bindings Result.ret (betree_node_t.Leaf node) /- [betree_main::betree::Internal::{4}::lookup_in_children] -/ -divergent def betree_internal_lookup_in_children_fwd +divergent def betree.Internal.lookup_in_children_fwd (self : betree_internal_t) (key : U64) (st : State) : Result (State × (Option U64)) := let ⟨ _, i, n, n0 ⟩ := self if key < i - then betree_node_lookup_fwd n key st - else betree_node_lookup_fwd n0 key st + then betree.Node.lookup_fwd n key st + else betree.Node.lookup_fwd n0 key st /- [betree_main::betree::Internal::{4}::lookup_in_children] -/ -divergent def betree_internal_lookup_in_children_back +divergent def betree.Internal.lookup_in_children_back (self : betree_internal_t) (key : U64) (st : State) : Result betree_internal_t := @@ -495,17 +493,17 @@ divergent def betree_internal_lookup_in_children_back if key < i0 then do - let n1 ← betree_node_lookup_back n key st + let n1 ← betree.Node.lookup_back n key st Result.ret (betree_internal_t.mk i i0 n1 n0) else do - let n1 ← betree_node_lookup_back n0 key st + let n1 ← betree.Node.lookup_back n0 key st Result.ret (betree_internal_t.mk i i0 n n1) end /- [betree_main::betree::Node::{5}::lookup_mut_in_bindings] -/ -divergent def betree_node_lookup_mut_in_bindings_fwd +divergent def betree.Node.lookup_mut_in_bindings_fwd (key : U64) (bindings : betree_list_t (U64 × U64)) : Result (betree_list_t (U64 × U64)) := @@ -514,11 +512,11 @@ divergent def betree_node_lookup_mut_in_bindings_fwd let (i, i0) := hd if i >= key then Result.ret (betree_list_t.Cons (i, i0) tl) - else betree_node_lookup_mut_in_bindings_fwd key tl + else betree.Node.lookup_mut_in_bindings_fwd key tl | betree_list_t.Nil => Result.ret betree_list_t.Nil /- [betree_main::betree::Node::{5}::lookup_mut_in_bindings] -/ -divergent def betree_node_lookup_mut_in_bindings_back +divergent def betree.Node.lookup_mut_in_bindings_back (key : U64) (bindings : betree_list_t (U64 × U64)) (ret0 : betree_list_t (U64 × U64)) : Result (betree_list_t (U64 × U64)) @@ -530,60 +528,60 @@ divergent def betree_node_lookup_mut_in_bindings_back then Result.ret ret0 else do - let tl0 ← betree_node_lookup_mut_in_bindings_back key tl ret0 + let tl0 ← betree.Node.lookup_mut_in_bindings_back key tl ret0 Result.ret (betree_list_t.Cons (i, i0) tl0) | betree_list_t.Nil => Result.ret ret0 /- [betree_main::betree::Node::{5}::apply_to_leaf] -/ -def betree_node_apply_to_leaf_fwd_back +def betree.Node.apply_to_leaf_fwd_back (bindings : betree_list_t (U64 × U64)) (key : U64) (new_msg : betree_message_t) : Result (betree_list_t (U64 × U64)) := do - let bindings0 ← betree_node_lookup_mut_in_bindings_fwd key bindings - let b ← betree_list_head_has_key_fwd U64 bindings0 key + let bindings0 ← betree.Node.lookup_mut_in_bindings_fwd key bindings + let b ← betree.List.head_has_key_fwd U64 bindings0 key if b then do - let hd ← betree_list_pop_front_fwd (U64 × U64) bindings0 + let hd ← betree.List.pop_front_fwd (U64 × U64) bindings0 match new_msg with | betree_message_t.Insert v => do - let bindings1 ← betree_list_pop_front_back (U64 × U64) bindings0 + let bindings1 ← betree.List.pop_front_back (U64 × U64) bindings0 let bindings2 ← - betree_list_push_front_fwd_back (U64 × U64) bindings1 (key, v) - betree_node_lookup_mut_in_bindings_back key bindings bindings2 + betree.List.push_front_fwd_back (U64 × U64) bindings1 (key, v) + betree.Node.lookup_mut_in_bindings_back key bindings bindings2 | betree_message_t.Delete => do - let bindings1 ← betree_list_pop_front_back (U64 × U64) bindings0 - betree_node_lookup_mut_in_bindings_back key bindings bindings1 + let bindings1 ← betree.List.pop_front_back (U64 × U64) bindings0 + betree.Node.lookup_mut_in_bindings_back key bindings bindings1 | betree_message_t.Upsert s => do let (_, i) := hd - let v ← betree_upsert_update_fwd (Option.some i) s - let bindings1 ← betree_list_pop_front_back (U64 × U64) bindings0 + let v ← betree.upsert_update_fwd (Option.some i) s + let bindings1 ← betree.List.pop_front_back (U64 × U64) bindings0 let bindings2 ← - betree_list_push_front_fwd_back (U64 × U64) bindings1 (key, v) - betree_node_lookup_mut_in_bindings_back key bindings bindings2 + betree.List.push_front_fwd_back (U64 × U64) bindings1 (key, v) + betree.Node.lookup_mut_in_bindings_back key bindings bindings2 else match new_msg with | betree_message_t.Insert v => do let bindings1 ← - betree_list_push_front_fwd_back (U64 × U64) bindings0 (key, v) - betree_node_lookup_mut_in_bindings_back key bindings bindings1 + betree.List.push_front_fwd_back (U64 × U64) bindings0 (key, v) + betree.Node.lookup_mut_in_bindings_back key bindings bindings1 | betree_message_t.Delete => - betree_node_lookup_mut_in_bindings_back key bindings bindings0 + betree.Node.lookup_mut_in_bindings_back key bindings bindings0 | betree_message_t.Upsert s => do - let v ← betree_upsert_update_fwd Option.none s + let v ← betree.upsert_update_fwd Option.none s let bindings1 ← - betree_list_push_front_fwd_back (U64 × U64) bindings0 (key, v) - betree_node_lookup_mut_in_bindings_back key bindings bindings1 + betree.List.push_front_fwd_back (U64 × U64) bindings0 (key, v) + betree.Node.lookup_mut_in_bindings_back key bindings bindings1 /- [betree_main::betree::Node::{5}::apply_messages_to_leaf] -/ -divergent def betree_node_apply_messages_to_leaf_fwd_back +divergent def betree.Node.apply_messages_to_leaf_fwd_back (bindings : betree_list_t (U64 × U64)) (new_msgs : betree_list_t (U64 × betree_message_t)) : Result (betree_list_t (U64 × U64)) @@ -592,12 +590,12 @@ divergent def betree_node_apply_messages_to_leaf_fwd_back | betree_list_t.Cons new_msg new_msgs_tl => do let (i, m) := new_msg - let bindings0 ← betree_node_apply_to_leaf_fwd_back bindings i m - betree_node_apply_messages_to_leaf_fwd_back bindings0 new_msgs_tl + let bindings0 ← betree.Node.apply_to_leaf_fwd_back bindings i m + betree.Node.apply_messages_to_leaf_fwd_back bindings0 new_msgs_tl | betree_list_t.Nil => Result.ret bindings /- [betree_main::betree::Node::{5}::filter_messages_for_key] -/ -divergent def betree_node_filter_messages_for_key_fwd_back +divergent def betree.Node.filter_messages_for_key_fwd_back (key : U64) (msgs : betree_list_t (U64 × betree_message_t)) : Result (betree_list_t (U64 × betree_message_t)) := @@ -608,14 +606,14 @@ divergent def betree_node_filter_messages_for_key_fwd_back then do let msgs0 ← - betree_list_pop_front_back (U64 × betree_message_t) + betree.List.pop_front_back (U64 × betree_message_t) (betree_list_t.Cons (k, m) l) - betree_node_filter_messages_for_key_fwd_back key msgs0 + betree.Node.filter_messages_for_key_fwd_back key msgs0 else Result.ret (betree_list_t.Cons (k, m) l) | betree_list_t.Nil => Result.ret betree_list_t.Nil /- [betree_main::betree::Node::{5}::lookup_first_message_after_key] -/ -divergent def betree_node_lookup_first_message_after_key_fwd +divergent def betree.Node.lookup_first_message_after_key_fwd (key : U64) (msgs : betree_list_t (U64 × betree_message_t)) : Result (betree_list_t (U64 × betree_message_t)) := @@ -623,12 +621,12 @@ divergent def betree_node_lookup_first_message_after_key_fwd | betree_list_t.Cons p next_msgs => let (k, m) := p if k = key - then betree_node_lookup_first_message_after_key_fwd key next_msgs + then betree.Node.lookup_first_message_after_key_fwd key next_msgs else Result.ret (betree_list_t.Cons (k, m) next_msgs) | betree_list_t.Nil => Result.ret betree_list_t.Nil /- [betree_main::betree::Node::{5}::lookup_first_message_after_key] -/ -divergent def betree_node_lookup_first_message_after_key_back +divergent def betree.Node.lookup_first_message_after_key_back (key : U64) (msgs : betree_list_t (U64 × betree_message_t)) (ret0 : betree_list_t (U64 × betree_message_t)) : Result (betree_list_t (U64 × betree_message_t)) @@ -640,79 +638,79 @@ divergent def betree_node_lookup_first_message_after_key_back then do let next_msgs0 ← - betree_node_lookup_first_message_after_key_back key next_msgs ret0 + betree.Node.lookup_first_message_after_key_back key next_msgs ret0 Result.ret (betree_list_t.Cons (k, m) next_msgs0) else Result.ret ret0 | betree_list_t.Nil => Result.ret ret0 /- [betree_main::betree::Node::{5}::apply_to_internal] -/ -def betree_node_apply_to_internal_fwd_back +def betree.Node.apply_to_internal_fwd_back (msgs : betree_list_t (U64 × betree_message_t)) (key : U64) (new_msg : betree_message_t) : Result (betree_list_t (U64 × betree_message_t)) := do - let msgs0 ← betree_node_lookup_first_message_for_key_fwd key msgs - let b ← betree_list_head_has_key_fwd betree_message_t msgs0 key + let msgs0 ← betree.Node.lookup_first_message_for_key_fwd key msgs + let b ← betree.List.head_has_key_fwd betree_message_t msgs0 key if b then match new_msg with | betree_message_t.Insert i => do - let msgs1 ← betree_node_filter_messages_for_key_fwd_back key msgs0 + let msgs1 ← betree.Node.filter_messages_for_key_fwd_back key msgs0 let msgs2 ← - betree_list_push_front_fwd_back (U64 × betree_message_t) msgs1 + betree.List.push_front_fwd_back (U64 × betree_message_t) msgs1 (key, betree_message_t.Insert i) - betree_node_lookup_first_message_for_key_back key msgs msgs2 + betree.Node.lookup_first_message_for_key_back key msgs msgs2 | betree_message_t.Delete => do - let msgs1 ← betree_node_filter_messages_for_key_fwd_back key msgs0 + let msgs1 ← betree.Node.filter_messages_for_key_fwd_back key msgs0 let msgs2 ← - betree_list_push_front_fwd_back (U64 × betree_message_t) msgs1 + betree.List.push_front_fwd_back (U64 × betree_message_t) msgs1 (key, betree_message_t.Delete) - betree_node_lookup_first_message_for_key_back key msgs msgs2 + betree.Node.lookup_first_message_for_key_back key msgs msgs2 | betree_message_t.Upsert s => do - let p ← betree_list_hd_fwd (U64 × betree_message_t) msgs0 + let p ← betree.List.hd_fwd (U64 × betree_message_t) msgs0 let (_, m) := p match m with | betree_message_t.Insert prev => do - let v ← betree_upsert_update_fwd (Option.some prev) s + let v ← betree.upsert_update_fwd (Option.some prev) s let msgs1 ← - betree_list_pop_front_back (U64 × betree_message_t) msgs0 + betree.List.pop_front_back (U64 × betree_message_t) msgs0 let msgs2 ← - betree_list_push_front_fwd_back (U64 × betree_message_t) msgs1 + betree.List.push_front_fwd_back (U64 × betree_message_t) msgs1 (key, betree_message_t.Insert v) - betree_node_lookup_first_message_for_key_back key msgs msgs2 + betree.Node.lookup_first_message_for_key_back key msgs msgs2 | betree_message_t.Delete => do - let v ← betree_upsert_update_fwd Option.none s + let v ← betree.upsert_update_fwd Option.none s let msgs1 ← - betree_list_pop_front_back (U64 × betree_message_t) msgs0 + betree.List.pop_front_back (U64 × betree_message_t) msgs0 let msgs2 ← - betree_list_push_front_fwd_back (U64 × betree_message_t) msgs1 + betree.List.push_front_fwd_back (U64 × betree_message_t) msgs1 (key, betree_message_t.Insert v) - betree_node_lookup_first_message_for_key_back key msgs msgs2 + betree.Node.lookup_first_message_for_key_back key msgs msgs2 | betree_message_t.Upsert ufs => do let msgs1 ← - betree_node_lookup_first_message_after_key_fwd key msgs0 + betree.Node.lookup_first_message_after_key_fwd key msgs0 let msgs2 ← - betree_list_push_front_fwd_back (U64 × betree_message_t) msgs1 + betree.List.push_front_fwd_back (U64 × betree_message_t) msgs1 (key, betree_message_t.Upsert s) let msgs3 ← - betree_node_lookup_first_message_after_key_back key msgs0 msgs2 - betree_node_lookup_first_message_for_key_back key msgs msgs3 + betree.Node.lookup_first_message_after_key_back key msgs0 msgs2 + betree.Node.lookup_first_message_for_key_back key msgs msgs3 else do let msgs1 ← - betree_list_push_front_fwd_back (U64 × betree_message_t) msgs0 (key, + betree.List.push_front_fwd_back (U64 × betree_message_t) msgs0 (key, new_msg) - betree_node_lookup_first_message_for_key_back key msgs msgs1 + betree.Node.lookup_first_message_for_key_back key msgs msgs1 /- [betree_main::betree::Node::{5}::apply_messages_to_internal] -/ -divergent def betree_node_apply_messages_to_internal_fwd_back +divergent def betree.Node.apply_messages_to_internal_fwd_back (msgs : betree_list_t (U64 × betree_message_t)) (new_msgs : betree_list_t (U64 × betree_message_t)) : Result (betree_list_t (U64 × betree_message_t)) @@ -721,12 +719,12 @@ divergent def betree_node_apply_messages_to_internal_fwd_back | betree_list_t.Cons new_msg new_msgs_tl => do let (i, m) := new_msg - let msgs0 ← betree_node_apply_to_internal_fwd_back msgs i m - betree_node_apply_messages_to_internal_fwd_back msgs0 new_msgs_tl + let msgs0 ← betree.Node.apply_to_internal_fwd_back msgs i m + betree.Node.apply_messages_to_internal_fwd_back msgs0 new_msgs_tl | betree_list_t.Nil => Result.ret msgs /- [betree_main::betree::Node::{5}::apply_messages] -/ -mutual divergent def betree_node_apply_messages_fwd +mutual divergent def betree.Node.apply_messages_fwd (self : betree_node_t) (params : betree_params_t) (node_id_cnt : betree_node_id_counter_t) (msgs : betree_list_t (U64 × betree_message_t)) (st : State) : @@ -736,49 +734,49 @@ mutual divergent def betree_node_apply_messages_fwd | betree_node_t.Internal node => do let ⟨ i, i0, n, n0 ⟩ := node - let (st0, content) ← betree_load_internal_node_fwd i st + let (st0, content) ← betree.load_internal_node_fwd i st let content0 ← - betree_node_apply_messages_to_internal_fwd_back content msgs - let num_msgs ← betree_list_len_fwd (U64 × betree_message_t) content0 + betree.Node.apply_messages_to_internal_fwd_back content msgs + let num_msgs ← betree.List.len_fwd (U64 × betree_message_t) content0 if num_msgs >= params.betree_params_min_flush_size then do let (st1, content1) ← - betree_internal_flush_fwd (betree_internal_t.mk i i0 n n0) params + betree.Internal.flush_fwd (betree_internal_t.mk i i0 n n0) params node_id_cnt content0 st0 let (node0, _) ← - betree_internal_flush_back (betree_internal_t.mk i i0 n n0) params + betree.Internal.flush_back (betree_internal_t.mk i i0 n n0) params node_id_cnt content0 st0 let ⟨ i1, _, _, _ ⟩ := node0 - let (st2, _) ← betree_store_internal_node_fwd i1 content1 st1 + let (st2, _) ← betree.store_internal_node_fwd i1 content1 st1 Result.ret (st2, ()) else do - let (st1, _) ← betree_store_internal_node_fwd i content0 st0 + let (st1, _) ← betree.store_internal_node_fwd i content0 st0 Result.ret (st1, ()) | betree_node_t.Leaf node => do - let (st0, content) ← betree_load_leaf_node_fwd node.betree_leaf_id st - let content0 ← betree_node_apply_messages_to_leaf_fwd_back content msgs - let len ← betree_list_len_fwd (U64 × U64) content0 + let (st0, content) ← betree.load_leaf_node_fwd node.betree_leaf_id st + let content0 ← betree.Node.apply_messages_to_leaf_fwd_back content msgs + let len ← betree.List.len_fwd (U64 × U64) content0 let i ← (U64.ofInt 2 (by intlit)) * params.betree_params_split_size if len >= i then do let (st1, _) ← - betree_leaf_split_fwd node content0 params node_id_cnt st0 + betree.Leaf.split_fwd node content0 params node_id_cnt st0 let (st2, _) ← - betree_store_leaf_node_fwd node.betree_leaf_id betree_list_t.Nil + betree.store_leaf_node_fwd node.betree_leaf_id betree_list_t.Nil st1 Result.ret (st2, ()) else do let (st1, _) ← - betree_store_leaf_node_fwd node.betree_leaf_id content0 st0 + betree.store_leaf_node_fwd node.betree_leaf_id content0 st0 Result.ret (st1, ()) /- [betree_main::betree::Node::{5}::apply_messages] -/ -divergent def betree_node_apply_messages_back +divergent def betree.Node.apply_messages_back (self : betree_node_t) (params : betree_params_t) (node_id_cnt : betree_node_id_counter_t) (msgs : betree_list_t (U64 × betree_message_t)) (st : State) : @@ -788,53 +786,53 @@ divergent def betree_node_apply_messages_back | betree_node_t.Internal node => do let ⟨ i, i0, n, n0 ⟩ := node - let (st0, content) ← betree_load_internal_node_fwd i st + let (st0, content) ← betree.load_internal_node_fwd i st let content0 ← - betree_node_apply_messages_to_internal_fwd_back content msgs - let num_msgs ← betree_list_len_fwd (U64 × betree_message_t) content0 + betree.Node.apply_messages_to_internal_fwd_back content msgs + let num_msgs ← betree.List.len_fwd (U64 × betree_message_t) content0 if num_msgs >= params.betree_params_min_flush_size then do let (st1, content1) ← - betree_internal_flush_fwd (betree_internal_t.mk i i0 n n0) params + betree.Internal.flush_fwd (betree_internal_t.mk i i0 n n0) params node_id_cnt content0 st0 let (node0, node_id_cnt0) ← - betree_internal_flush_back (betree_internal_t.mk i i0 n n0) params + betree.Internal.flush_back (betree_internal_t.mk i i0 n n0) params node_id_cnt content0 st0 let ⟨ i1, i2, n1, n2 ⟩ := node0 - let _ ← betree_store_internal_node_fwd i1 content1 st1 + let _ ← betree.store_internal_node_fwd i1 content1 st1 Result.ret (betree_node_t.Internal (betree_internal_t.mk i1 i2 n1 n2), node_id_cnt0) else do - let _ ← betree_store_internal_node_fwd i content0 st0 + let _ ← betree.store_internal_node_fwd i content0 st0 Result.ret (betree_node_t.Internal (betree_internal_t.mk i i0 n n0), node_id_cnt) | betree_node_t.Leaf node => do - let (st0, content) ← betree_load_leaf_node_fwd node.betree_leaf_id st - let content0 ← betree_node_apply_messages_to_leaf_fwd_back content msgs - let len ← betree_list_len_fwd (U64 × U64) content0 + let (st0, content) ← betree.load_leaf_node_fwd node.betree_leaf_id st + let content0 ← betree.Node.apply_messages_to_leaf_fwd_back content msgs + let len ← betree.List.len_fwd (U64 × U64) content0 let i ← (U64.ofInt 2 (by intlit)) * params.betree_params_split_size if len >= i then do let (st1, new_node) ← - betree_leaf_split_fwd node content0 params node_id_cnt st0 + betree.Leaf.split_fwd node content0 params node_id_cnt st0 let _ ← - betree_store_leaf_node_fwd node.betree_leaf_id betree_list_t.Nil + betree.store_leaf_node_fwd node.betree_leaf_id betree_list_t.Nil st1 let node_id_cnt0 ← - betree_leaf_split_back node content0 params node_id_cnt st0 + betree.Leaf.split_back node content0 params node_id_cnt st0 Result.ret (betree_node_t.Internal new_node, node_id_cnt0) else do - let _ ← betree_store_leaf_node_fwd node.betree_leaf_id content0 st0 + let _ ← betree.store_leaf_node_fwd node.betree_leaf_id content0 st0 Result.ret (betree_node_t.Leaf { node with betree_leaf_size := len }, node_id_cnt) /- [betree_main::betree::Internal::{4}::flush] -/ -divergent def betree_internal_flush_fwd +divergent def betree.Internal.flush_fwd (self : betree_internal_t) (params : betree_params_t) (node_id_cnt : betree_node_id_counter_t) (content : betree_list_t (U64 × betree_message_t)) (st : State) : @@ -842,39 +840,39 @@ divergent def betree_internal_flush_fwd := do let ⟨ _, i, n, n0 ⟩ := self - let p ← betree_list_partition_at_pivot_fwd betree_message_t content i + let p ← betree.List.partition_at_pivot_fwd betree_message_t content i let (msgs_left, msgs_right) := p - let len_left ← betree_list_len_fwd (U64 × betree_message_t) msgs_left + let len_left ← betree.List.len_fwd (U64 × betree_message_t) msgs_left if len_left >= params.betree_params_min_flush_size then do let (st0, _) ← - betree_node_apply_messages_fwd n params node_id_cnt msgs_left st + betree.Node.apply_messages_fwd n params node_id_cnt msgs_left st let (_, node_id_cnt0) ← - betree_node_apply_messages_back n params node_id_cnt msgs_left st + betree.Node.apply_messages_back n params node_id_cnt msgs_left st let len_right ← - betree_list_len_fwd (U64 × betree_message_t) msgs_right + betree.List.len_fwd (U64 × betree_message_t) msgs_right if len_right >= params.betree_params_min_flush_size then do let (st1, _) ← - betree_node_apply_messages_fwd n0 params node_id_cnt0 msgs_right + betree.Node.apply_messages_fwd n0 params node_id_cnt0 msgs_right st0 let _ ← - betree_node_apply_messages_back n0 params node_id_cnt0 msgs_right + betree.Node.apply_messages_back n0 params node_id_cnt0 msgs_right st0 Result.ret (st1, betree_list_t.Nil) else Result.ret (st0, msgs_right) else do let (st0, _) ← - betree_node_apply_messages_fwd n0 params node_id_cnt msgs_right st + betree.Node.apply_messages_fwd n0 params node_id_cnt msgs_right st let _ ← - betree_node_apply_messages_back n0 params node_id_cnt msgs_right st + betree.Node.apply_messages_back n0 params node_id_cnt msgs_right st Result.ret (st0, msgs_left) /- [betree_main::betree::Internal::{4}::flush] -/ -divergent def betree_internal_flush_back +divergent def betree.Internal.flush_back (self : betree_internal_t) (params : betree_params_t) (node_id_cnt : betree_node_id_counter_t) (content : betree_list_t (U64 × betree_message_t)) (st : State) : @@ -882,36 +880,36 @@ divergent def betree_internal_flush_back := do let ⟨ i, i0, n, n0 ⟩ := self - let p ← betree_list_partition_at_pivot_fwd betree_message_t content i0 + let p ← betree.List.partition_at_pivot_fwd betree_message_t content i0 let (msgs_left, msgs_right) := p - let len_left ← betree_list_len_fwd (U64 × betree_message_t) msgs_left + let len_left ← betree.List.len_fwd (U64 × betree_message_t) msgs_left if len_left >= params.betree_params_min_flush_size then do let (st0, _) ← - betree_node_apply_messages_fwd n params node_id_cnt msgs_left st + betree.Node.apply_messages_fwd n params node_id_cnt msgs_left st let (n1, node_id_cnt0) ← - betree_node_apply_messages_back n params node_id_cnt msgs_left st + betree.Node.apply_messages_back n params node_id_cnt msgs_left st let len_right ← - betree_list_len_fwd (U64 × betree_message_t) msgs_right + betree.List.len_fwd (U64 × betree_message_t) msgs_right if len_right >= params.betree_params_min_flush_size then do let (n2, node_id_cnt1) ← - betree_node_apply_messages_back n0 params node_id_cnt0 msgs_right + betree.Node.apply_messages_back n0 params node_id_cnt0 msgs_right st0 Result.ret (betree_internal_t.mk i i0 n1 n2, node_id_cnt1) else Result.ret (betree_internal_t.mk i i0 n1 n0, node_id_cnt0) else do let (n1, node_id_cnt0) ← - betree_node_apply_messages_back n0 params node_id_cnt msgs_right st + betree.Node.apply_messages_back n0 params node_id_cnt msgs_right st Result.ret (betree_internal_t.mk i i0 n n1, node_id_cnt0) end /- [betree_main::betree::Node::{5}::apply] -/ -def betree_node_apply_fwd +def betree.Node.apply_fwd (self : betree_node_t) (params : betree_params_t) (node_id_cnt : betree_node_id_counter_t) (key : U64) (new_msg : betree_message_t) (st : State) : @@ -920,34 +918,34 @@ def betree_node_apply_fwd do let l := betree_list_t.Nil let (st0, _) ← - betree_node_apply_messages_fwd self params node_id_cnt + betree.Node.apply_messages_fwd self params node_id_cnt (betree_list_t.Cons (key, new_msg) l) st let _ ← - betree_node_apply_messages_back self params node_id_cnt + betree.Node.apply_messages_back self params node_id_cnt (betree_list_t.Cons (key, new_msg) l) st Result.ret (st0, ()) /- [betree_main::betree::Node::{5}::apply] -/ -def betree_node_apply_back +def betree.Node.apply_back (self : betree_node_t) (params : betree_params_t) (node_id_cnt : betree_node_id_counter_t) (key : U64) (new_msg : betree_message_t) (st : State) : Result (betree_node_t × betree_node_id_counter_t) := let l := betree_list_t.Nil - betree_node_apply_messages_back self params node_id_cnt (betree_list_t.Cons + betree.Node.apply_messages_back self params node_id_cnt (betree_list_t.Cons (key, new_msg) l) st /- [betree_main::betree::BeTree::{6}::new] -/ -def betree_be_tree_new_fwd +def betree.BeTree.new_fwd (min_flush_size : U64) (split_size : U64) (st : State) : Result (State × betree_be_tree_t) := do - let node_id_cnt ← betree_node_id_counter_new_fwd - let id ← betree_node_id_counter_fresh_id_fwd node_id_cnt - let (st0, _) ← betree_store_leaf_node_fwd id betree_list_t.Nil st - let node_id_cnt0 ← betree_node_id_counter_fresh_id_back node_id_cnt + let node_id_cnt ← betree.NodeIdCounter.new_fwd + let id ← betree.NodeIdCounter.fresh_id_fwd node_id_cnt + let (st0, _) ← betree.store_leaf_node_fwd id betree_list_t.Nil st + let node_id_cnt0 ← betree.NodeIdCounter.fresh_id_back node_id_cnt Result.ret (st0, { betree_be_tree_params := @@ -965,103 +963,103 @@ def betree_be_tree_new_fwd }) /- [betree_main::betree::BeTree::{6}::apply] -/ -def betree_be_tree_apply_fwd +def betree.BeTree.apply_fwd (self : betree_be_tree_t) (key : U64) (msg : betree_message_t) (st : State) : Result (State × Unit) := do let (st0, _) ← - betree_node_apply_fwd self.betree_be_tree_root self.betree_be_tree_params + betree.Node.apply_fwd self.betree_be_tree_root self.betree_be_tree_params self.betree_be_tree_node_id_cnt key msg st let _ ← - betree_node_apply_back self.betree_be_tree_root + betree.Node.apply_back self.betree_be_tree_root self.betree_be_tree_params self.betree_be_tree_node_id_cnt key msg st Result.ret (st0, ()) /- [betree_main::betree::BeTree::{6}::apply] -/ -def betree_be_tree_apply_back +def betree.BeTree.apply_back (self : betree_be_tree_t) (key : U64) (msg : betree_message_t) (st : State) : Result betree_be_tree_t := do let (n, nic) ← - betree_node_apply_back self.betree_be_tree_root + betree.Node.apply_back self.betree_be_tree_root self.betree_be_tree_params self.betree_be_tree_node_id_cnt key msg st Result.ret { self with betree_be_tree_node_id_cnt := nic, betree_be_tree_root := n } /- [betree_main::betree::BeTree::{6}::insert] -/ -def betree_be_tree_insert_fwd +def betree.BeTree.insert_fwd (self : betree_be_tree_t) (key : U64) (value : U64) (st : State) : Result (State × Unit) := do let (st0, _) ← - betree_be_tree_apply_fwd self key (betree_message_t.Insert value) st + betree.BeTree.apply_fwd self key (betree_message_t.Insert value) st let _ ← - betree_be_tree_apply_back self key (betree_message_t.Insert value) st + betree.BeTree.apply_back self key (betree_message_t.Insert value) st Result.ret (st0, ()) /- [betree_main::betree::BeTree::{6}::insert] -/ -def betree_be_tree_insert_back +def betree.BeTree.insert_back (self : betree_be_tree_t) (key : U64) (value : U64) (st : State) : Result betree_be_tree_t := - betree_be_tree_apply_back self key (betree_message_t.Insert value) st + betree.BeTree.apply_back self key (betree_message_t.Insert value) st /- [betree_main::betree::BeTree::{6}::delete] -/ -def betree_be_tree_delete_fwd +def betree.BeTree.delete_fwd (self : betree_be_tree_t) (key : U64) (st : State) : Result (State × Unit) := do let (st0, _) ← - betree_be_tree_apply_fwd self key betree_message_t.Delete st - let _ ← betree_be_tree_apply_back self key betree_message_t.Delete st + betree.BeTree.apply_fwd self key betree_message_t.Delete st + let _ ← betree.BeTree.apply_back self key betree_message_t.Delete st Result.ret (st0, ()) /- [betree_main::betree::BeTree::{6}::delete] -/ -def betree_be_tree_delete_back +def betree.BeTree.delete_back (self : betree_be_tree_t) (key : U64) (st : State) : Result betree_be_tree_t := - betree_be_tree_apply_back self key betree_message_t.Delete st + betree.BeTree.apply_back self key betree_message_t.Delete st /- [betree_main::betree::BeTree::{6}::upsert] -/ -def betree_be_tree_upsert_fwd +def betree.BeTree.upsert_fwd (self : betree_be_tree_t) (key : U64) (upd : betree_upsert_fun_state_t) (st : State) : Result (State × Unit) := do let (st0, _) ← - betree_be_tree_apply_fwd self key (betree_message_t.Upsert upd) st + betree.BeTree.apply_fwd self key (betree_message_t.Upsert upd) st let _ ← - betree_be_tree_apply_back self key (betree_message_t.Upsert upd) st + betree.BeTree.apply_back self key (betree_message_t.Upsert upd) st Result.ret (st0, ()) /- [betree_main::betree::BeTree::{6}::upsert] -/ -def betree_be_tree_upsert_back +def betree.BeTree.upsert_back (self : betree_be_tree_t) (key : U64) (upd : betree_upsert_fun_state_t) (st : State) : Result betree_be_tree_t := - betree_be_tree_apply_back self key (betree_message_t.Upsert upd) st + betree.BeTree.apply_back self key (betree_message_t.Upsert upd) st /- [betree_main::betree::BeTree::{6}::lookup] -/ -def betree_be_tree_lookup_fwd +def betree.BeTree.lookup_fwd (self : betree_be_tree_t) (key : U64) (st : State) : Result (State × (Option U64)) := - betree_node_lookup_fwd self.betree_be_tree_root key st + betree.Node.lookup_fwd self.betree_be_tree_root key st /- [betree_main::betree::BeTree::{6}::lookup] -/ -def betree_be_tree_lookup_back +def betree.BeTree.lookup_back (self : betree_be_tree_t) (key : U64) (st : State) : Result betree_be_tree_t := do - let n ← betree_node_lookup_back self.betree_be_tree_root key st + let n ← betree.Node.lookup_back self.betree_be_tree_root key st Result.ret { self with betree_be_tree_root := n } /- [betree_main::main] -/ @@ -1071,4 +1069,4 @@ def main_fwd : Result Unit := /- Unit test for [betree_main::main] -/ #assert (main_fwd == .ret ()) -end BetreeMain +end betree_main diff --git a/tests/lean/BetreeMain/FunsExternal.lean b/tests/lean/BetreeMain/FunsExternal.lean new file mode 100644 index 00000000..0c715ef9 --- /dev/null +++ b/tests/lean/BetreeMain/FunsExternal.lean @@ -0,0 +1,35 @@ +-- [betree_main]: external functions. +import Base +import BetreeMain.Types +open Primitives +open betree_main + +-- TODO: fill those bodies + +/- [betree_main::betree_utils::load_internal_node] -/ +def betree_utils.load_internal_node_fwd + : + U64 → State → Result (State × (betree_list_t (U64 × betree_message_t))) := + fun _ _ => .fail .panic + +/- [betree_main::betree_utils::store_internal_node] -/ +def betree_utils.store_internal_node_fwd + : + U64 → betree_list_t (U64 × betree_message_t) → State → Result (State + × Unit) := + fun _ _ _ => .fail .panic + +/- [betree_main::betree_utils::load_leaf_node] -/ +def betree_utils.load_leaf_node_fwd + : U64 → State → Result (State × (betree_list_t (U64 × U64))) := + fun _ _ => .fail .panic + +/- [betree_main::betree_utils::store_leaf_node] -/ +def betree_utils.store_leaf_node_fwd + : U64 → betree_list_t (U64 × U64) → State → Result (State × Unit) := + fun _ _ _ => .fail .panic + +/- [core::option::Option::{0}::unwrap] -/ +def core.option.Option.unwrap_fwd + (T : Type) : Option T → State → Result (State × T) := + fun _ _ => .fail .panic diff --git a/tests/lean/BetreeMain/FunsExternal_Template.lean b/tests/lean/BetreeMain/FunsExternal_Template.lean new file mode 100644 index 00000000..9b5a54e5 --- /dev/null +++ b/tests/lean/BetreeMain/FunsExternal_Template.lean @@ -0,0 +1,31 @@ +-- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS +-- [betree_main]: external functions. +-- This is a template file: rename it to "FunsExternal.lean" and fill the holes. +import Base +import BetreeMain.Types +open Primitives +open betree_main + +/- [betree_main::betree_utils::load_internal_node] -/ +axiom betree_utils.load_internal_node_fwd + : + U64 → State → Result (State × (betree_list_t (U64 × betree_message_t))) + +/- [betree_main::betree_utils::store_internal_node] -/ +axiom betree_utils.store_internal_node_fwd + : + U64 → betree_list_t (U64 × betree_message_t) → State → Result (State + × Unit) + +/- [betree_main::betree_utils::load_leaf_node] -/ +axiom betree_utils.load_leaf_node_fwd + : U64 → State → Result (State × (betree_list_t (U64 × U64))) + +/- [betree_main::betree_utils::store_leaf_node] -/ +axiom betree_utils.store_leaf_node_fwd + : U64 → betree_list_t (U64 × U64) → State → Result (State × Unit) + +/- [core::option::Option::{0}::unwrap] -/ +axiom core.option.Option.unwrap_fwd + (T : Type) : Option T → State → Result (State × T) + diff --git a/tests/lean/BetreeMain/Opaque.lean b/tests/lean/BetreeMain/Opaque.lean deleted file mode 100644 index d043186b..00000000 --- a/tests/lean/BetreeMain/Opaque.lean +++ /dev/null @@ -1,35 +0,0 @@ --- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS --- [betree_main]: opaque function definitions -import Base -import BetreeMain.Types -open Primitives - -namespace BetreeMain - -structure OpaqueDefs where - - /- [betree_main::betree_utils::load_internal_node] -/ - betree_utils_load_internal_node_fwd - : - U64 → State → Result (State × (betree_list_t (U64 × - betree_message_t))) - - /- [betree_main::betree_utils::store_internal_node] -/ - betree_utils_store_internal_node_fwd - : - U64 → betree_list_t (U64 × betree_message_t) → State → Result (State - × Unit) - - /- [betree_main::betree_utils::load_leaf_node] -/ - betree_utils_load_leaf_node_fwd - : U64 → State → Result (State × (betree_list_t (U64 × U64))) - - /- [betree_main::betree_utils::store_leaf_node] -/ - betree_utils_store_leaf_node_fwd - : U64 → betree_list_t (U64 × U64) → State → Result (State × Unit) - - /- [core::option::Option::{0}::unwrap] -/ - core_option_option_unwrap_fwd - (T : Type) : Option T → State → Result (State × T) - -end BetreeMain diff --git a/tests/lean/BetreeMain/Types.lean b/tests/lean/BetreeMain/Types.lean index cfed6a28..3ecbd668 100644 --- a/tests/lean/BetreeMain/Types.lean +++ b/tests/lean/BetreeMain/Types.lean @@ -2,8 +2,7 @@ -- [betree_main]: type definitions import Base open Primitives - -namespace BetreeMain +namespace betree_main /- [betree_main::betree::List] -/ inductive betree_list_t (T : Type) := @@ -57,4 +56,4 @@ structure betree_be_tree_t where /- The state type used in the state-error monad -/ axiom State : Type -end BetreeMain +end betree_main diff --git a/tests/lean/Constants.lean b/tests/lean/Constants.lean index 9f6a47de..39c270be 100644 --- a/tests/lean/Constants.lean +++ b/tests/lean/Constants.lean @@ -2,8 +2,7 @@ -- [constants] import Base open Primitives - -namespace Constants +namespace constants /- [constants::X0] -/ def x0_body : Result U32 := Result.ret (U32.ofInt 0 (by intlit)) @@ -69,11 +68,11 @@ structure wrap_t (T : Type) where wrap_val : T /- [constants::Wrap::{0}::new] -/ -def wrap_new_fwd (T : Type) (val : T) : Result (wrap_t T) := +def Wrap.new_fwd (T : Type) (val : T) : Result (wrap_t T) := Result.ret { wrap_val := val } /- [constants::Y] -/ -def y_body : Result (wrap_t I32) := wrap_new_fwd I32 (I32.ofInt 2 (by intlit)) +def y_body : Result (wrap_t I32) := Wrap.new_fwd I32 (I32.ofInt 2 (by intlit)) def y_c : wrap_t I32 := eval_global y_body (by simp) /- [constants::unwrap_y] -/ @@ -132,4 +131,4 @@ def s4_body : Result (pair_t U32 U32) := mk_pair1_fwd (U32.ofInt 7 (by intlit)) (U32.ofInt 8 (by intlit)) def s4_c : pair_t U32 U32 := eval_global s4_body (by simp) -end Constants +end constants diff --git a/tests/lean/External/ExternalFuns.lean b/tests/lean/External/ExternalFuns.lean deleted file mode 100644 index d63db2ac..00000000 --- a/tests/lean/External/ExternalFuns.lean +++ /dev/null @@ -1,9 +0,0 @@ -import Base -import External.Types -import External.Opaque - -namespace External - -def opaque_defs : OpaqueDefs := sorry - -end External diff --git a/tests/lean/External/Funs.lean b/tests/lean/External/Funs.lean index e36987e0..6bfffc33 100644 --- a/tests/lean/External/Funs.lean +++ b/tests/lean/External/Funs.lean @@ -2,18 +2,17 @@ -- [external]: function definitions import Base import External.Types -import External.ExternalFuns +import External.FunsExternal open Primitives - -namespace External +namespace external /- [external::swap] -/ def swap_fwd (T : Type) (x : T) (y : T) (st : State) : Result (State × Unit) := do - let (st0, _) ← opaque_defs.core_mem_swap_fwd T x y st - let (st1, _) ← opaque_defs.core_mem_swap_back0 T x y st st0 - let (st2, _) ← opaque_defs.core_mem_swap_back1 T x y st st1 + let (st0, _) ← core.mem.swap_fwd T x y st + let (st1, _) ← core.mem.swap_back0 T x y st st0 + let (st2, _) ← core.mem.swap_back1 T x y st st1 Result.ret (st2, ()) /- [external::swap] -/ @@ -22,18 +21,17 @@ def swap_back Result (State × (T × T)) := do - let (st1, _) ← opaque_defs.core_mem_swap_fwd T x y st - let (st2, x0) ← opaque_defs.core_mem_swap_back0 T x y st st1 - let (_, y0) ← opaque_defs.core_mem_swap_back1 T x y st st2 + let (st1, _) ← core.mem.swap_fwd T x y st + let (st2, x0) ← core.mem.swap_back0 T x y st st1 + let (_, y0) ← core.mem.swap_back1 T x y st st2 Result.ret (st0, (x0, y0)) /- [external::test_new_non_zero_u32] -/ def test_new_non_zero_u32_fwd (x : U32) (st : State) : Result (State × core_num_nonzero_non_zero_u32_t) := do - let (st0, opt) ← opaque_defs.core_num_nonzero_non_zero_u32_new_fwd x st - opaque_defs.core_option_option_unwrap_fwd core_num_nonzero_non_zero_u32_t - opt st0 + let (st0, opt) ← core.num.nonzero.NonZeroU32.new_fwd x st + core.option.Option.unwrap_fwd core_num_nonzero_non_zero_u32_t opt st0 /- [external::test_vec] -/ def test_vec_fwd : Result Unit := @@ -49,9 +47,9 @@ def test_vec_fwd : Result Unit := def custom_swap_fwd (T : Type) (x : T) (y : T) (st : State) : Result (State × T) := do - let (st0, _) ← opaque_defs.core_mem_swap_fwd T x y st - let (st1, x0) ← opaque_defs.core_mem_swap_back0 T x y st st0 - let (st2, _) ← opaque_defs.core_mem_swap_back1 T x y st st1 + let (st0, _) ← core.mem.swap_fwd T x y st + let (st1, x0) ← core.mem.swap_back0 T x y st st0 + let (st2, _) ← core.mem.swap_back1 T x y st st1 Result.ret (st2, x0) /- [external::custom_swap] -/ @@ -60,9 +58,9 @@ def custom_swap_back Result (State × (T × T)) := do - let (st1, _) ← opaque_defs.core_mem_swap_fwd T x y st - let (st2, _) ← opaque_defs.core_mem_swap_back0 T x y st st1 - let (_, y0) ← opaque_defs.core_mem_swap_back1 T x y st st2 + let (st1, _) ← core.mem.swap_fwd T x y st + let (st2, _) ← core.mem.swap_back0 T x y st st1 + let (_, y0) ← core.mem.swap_back1 T x y st st2 Result.ret (st0, (ret0, y0)) /- [external::test_custom_swap] -/ @@ -88,4 +86,4 @@ def test_swap_non_zero_fwd (x : U32) (st : State) : Result (State × U32) := then Result.fail Error.panic else Result.ret (st1, x0) -end External +end external diff --git a/tests/lean/External/FunsExternal.lean b/tests/lean/External/FunsExternal.lean new file mode 100644 index 00000000..5326dd77 --- /dev/null +++ b/tests/lean/External/FunsExternal.lean @@ -0,0 +1,33 @@ +-- [external]: templates for the external functions. +import Base +import External.Types +open Primitives +open external + +-- TODO: fill those bodies + +/- [core::mem::swap] -/ +def core.mem.swap_fwd + (T : Type) : T → T → State → Result (State × Unit) := + fun _x _y s => .ret (s, ()) + +/- [core::mem::swap] -/ +def core.mem.swap_back0 + (T : Type) : T → T → State → State → Result (State × T) := + fun _x y _s0 s1 => .ret (s1, y) + +/- [core::mem::swap] -/ +def core.mem.swap_back1 + (T : Type) : T → T → State → State → Result (State × T) := + fun x _y _s0 s1 => .ret (s1, x) + +/- [core::num::nonzero::NonZeroU32::{14}::new] -/ +def core.num.nonzero.NonZeroU32.new_fwd + : + U32 → State → Result (State × (Option core_num_nonzero_non_zero_u32_t)) := + fun _ _ => .fail .panic + +/- [core::option::Option::{0}::unwrap] -/ +def core.option.Option.unwrap_fwd + (T : Type) : Option T → State → Result (State × T) := + fun _ _ => .fail .panic diff --git a/tests/lean/External/FunsExternal_Template.lean b/tests/lean/External/FunsExternal_Template.lean new file mode 100644 index 00000000..d6c0eb1d --- /dev/null +++ b/tests/lean/External/FunsExternal_Template.lean @@ -0,0 +1,29 @@ +-- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS +-- [external]: external functions. +-- This is a template file: rename it to "FunsExternal.lean" and fill the holes. +import Base +import External.Types +open Primitives +open external + +/- [core::mem::swap] -/ +axiom core.mem.swap_fwd + (T : Type) : T → T → State → Result (State × Unit) + +/- [core::mem::swap] -/ +axiom core.mem.swap_back0 + (T : Type) : T → T → State → State → Result (State × T) + +/- [core::mem::swap] -/ +axiom core.mem.swap_back1 + (T : Type) : T → T → State → State → Result (State × T) + +/- [core::num::nonzero::NonZeroU32::{14}::new] -/ +axiom core.num.nonzero.NonZeroU32.new_fwd + : + U32 → State → Result (State × (Option core_num_nonzero_non_zero_u32_t)) + +/- [core::option::Option::{0}::unwrap] -/ +axiom core.option.Option.unwrap_fwd + (T : Type) : Option T → State → Result (State × T) + diff --git a/tests/lean/External/Opaque.lean b/tests/lean/External/Opaque.lean index 1c0db095..d0297523 100644 --- a/tests/lean/External/Opaque.lean +++ b/tests/lean/External/Opaque.lean @@ -4,29 +4,29 @@ import Base import External.Types open Primitives -namespace External +namespace external structure OpaqueDefs where /- [core::mem::swap] -/ - core_mem_swap_fwd (T : Type) : T → T → State → Result (State × Unit) + core.mem.swap_fwd (T : Type) : T → T → State → Result (State × Unit) /- [core::mem::swap] -/ - core_mem_swap_back0 + core.mem.swap_back0 (T : Type) : T → T → State → State → Result (State × T) /- [core::mem::swap] -/ - core_mem_swap_back1 + core.mem.swap_back1 (T : Type) : T → T → State → State → Result (State × T) /- [core::num::nonzero::NonZeroU32::{14}::new] -/ - core_num_nonzero_non_zero_u32_new_fwd + core.num.nonzero.NonZeroU32.new_fwd : U32 → State → Result (State × (Option core_num_nonzero_non_zero_u32_t)) /- [core::option::Option::{0}::unwrap] -/ - core_option_option_unwrap_fwd + core.option.Option.unwrap_fwd (T : Type) : Option T → State → Result (State × T) -end External +end external diff --git a/tests/lean/External/Types.lean b/tests/lean/External/Types.lean index fda0670e..316f6474 100644 --- a/tests/lean/External/Types.lean +++ b/tests/lean/External/Types.lean @@ -2,8 +2,7 @@ -- [external]: type definitions import Base open Primitives - -namespace External +namespace external /- [core::num::nonzero::NonZeroU32] -/ axiom core_num_nonzero_non_zero_u32_t : Type @@ -11,4 +10,4 @@ axiom core_num_nonzero_non_zero_u32_t : Type /- The state type used in the state-error monad -/ axiom State : Type -end External +end external diff --git a/tests/lean/Hashmap/Funs.lean b/tests/lean/Hashmap/Funs.lean index b4254726..8f54eca8 100644 --- a/tests/lean/Hashmap/Funs.lean +++ b/tests/lean/Hashmap/Funs.lean @@ -3,38 +3,37 @@ import Base import Hashmap.Types open Primitives - -namespace Hashmap +namespace hashmap /- [hashmap::hash_key] -/ def hash_key_fwd (k : Usize) : Result Usize := Result.ret k /- [hashmap::HashMap::{0}::allocate_slots] -/ -divergent def hash_map_allocate_slots_loop_fwd +divergent def HashMap.allocate_slots_loop_fwd (T : Type) (slots : Vec (list_t T)) (n : Usize) : Result (Vec (list_t T)) := if n > (Usize.ofInt 0 (by intlit)) then do let slots0 ← vec_push_back (list_t T) slots list_t.Nil let n0 ← n - (Usize.ofInt 1 (by intlit)) - hash_map_allocate_slots_loop_fwd T slots0 n0 + HashMap.allocate_slots_loop_fwd T slots0 n0 else Result.ret slots /- [hashmap::HashMap::{0}::allocate_slots] -/ -def hash_map_allocate_slots_fwd +def HashMap.allocate_slots_fwd (T : Type) (slots : Vec (list_t T)) (n : Usize) : Result (Vec (list_t T)) := - hash_map_allocate_slots_loop_fwd T slots n + HashMap.allocate_slots_loop_fwd T slots n /- [hashmap::HashMap::{0}::new_with_capacity] -/ -def hash_map_new_with_capacity_fwd +def HashMap.new_with_capacity_fwd (T : Type) (capacity : Usize) (max_load_dividend : Usize) (max_load_divisor : Usize) : Result (hash_map_t T) := do let v := vec_new (list_t T) - let slots ← hash_map_allocate_slots_fwd T v capacity + let slots ← HashMap.allocate_slots_fwd T v capacity let i ← capacity * max_load_dividend let i0 ← i / max_load_divisor Result.ret @@ -46,12 +45,12 @@ def hash_map_new_with_capacity_fwd } /- [hashmap::HashMap::{0}::new] -/ -def hash_map_new_fwd (T : Type) : Result (hash_map_t T) := - hash_map_new_with_capacity_fwd T (Usize.ofInt 32 (by intlit)) +def HashMap.new_fwd (T : Type) : Result (hash_map_t T) := + HashMap.new_with_capacity_fwd T (Usize.ofInt 32 (by intlit)) (Usize.ofInt 4 (by intlit)) (Usize.ofInt 5 (by intlit)) /- [hashmap::HashMap::{0}::clear] -/ -divergent def hash_map_clear_loop_fwd_back +divergent def HashMap.clear_loop_fwd_back (T : Type) (slots : Vec (list_t T)) (i : Usize) : Result (Vec (list_t T)) := let i0 := vec_len (list_t T) slots if i < i0 @@ -59,15 +58,15 @@ divergent def hash_map_clear_loop_fwd_back do let i1 ← i + (Usize.ofInt 1 (by intlit)) let slots0 ← vec_index_mut_back (list_t T) slots i list_t.Nil - hash_map_clear_loop_fwd_back T slots0 i1 + HashMap.clear_loop_fwd_back T slots0 i1 else Result.ret slots /- [hashmap::HashMap::{0}::clear] -/ -def hash_map_clear_fwd_back +def HashMap.clear_fwd_back (T : Type) (self : hash_map_t T) : Result (hash_map_t T) := do let v ← - hash_map_clear_loop_fwd_back T self.hash_map_slots + HashMap.clear_loop_fwd_back T self.hash_map_slots (Usize.ofInt 0 (by intlit)) Result.ret { @@ -78,26 +77,26 @@ def hash_map_clear_fwd_back } /- [hashmap::HashMap::{0}::len] -/ -def hash_map_len_fwd (T : Type) (self : hash_map_t T) : Result Usize := +def HashMap.len_fwd (T : Type) (self : hash_map_t T) : Result Usize := Result.ret self.hash_map_num_entries /- [hashmap::HashMap::{0}::insert_in_list] -/ -divergent def hash_map_insert_in_list_loop_fwd +divergent def HashMap.insert_in_list_loop_fwd (T : Type) (key : Usize) (value : T) (ls : list_t T) : Result Bool := match ls with | list_t.Cons ckey cvalue tl => if ckey = key then Result.ret false - else hash_map_insert_in_list_loop_fwd T key value tl + else HashMap.insert_in_list_loop_fwd T key value tl | list_t.Nil => Result.ret true /- [hashmap::HashMap::{0}::insert_in_list] -/ -def hash_map_insert_in_list_fwd +def HashMap.insert_in_list_fwd (T : Type) (key : Usize) (value : T) (ls : list_t T) : Result Bool := - hash_map_insert_in_list_loop_fwd T key value ls + HashMap.insert_in_list_loop_fwd T key value ls /- [hashmap::HashMap::{0}::insert_in_list] -/ -divergent def hash_map_insert_in_list_loop_back +divergent def HashMap.insert_in_list_loop_back (T : Type) (key : Usize) (value : T) (ls : list_t T) : Result (list_t T) := match ls with | list_t.Cons ckey cvalue tl => @@ -105,18 +104,18 @@ divergent def hash_map_insert_in_list_loop_back then Result.ret (list_t.Cons ckey value tl) else do - let tl0 ← hash_map_insert_in_list_loop_back T key value tl + let tl0 ← HashMap.insert_in_list_loop_back T key value tl Result.ret (list_t.Cons ckey cvalue tl0) | list_t.Nil => let l := list_t.Nil Result.ret (list_t.Cons key value l) /- [hashmap::HashMap::{0}::insert_in_list] -/ -def hash_map_insert_in_list_back +def HashMap.insert_in_list_back (T : Type) (key : Usize) (value : T) (ls : list_t T) : Result (list_t T) := - hash_map_insert_in_list_loop_back T key value ls + HashMap.insert_in_list_loop_back T key value ls /- [hashmap::HashMap::{0}::insert_no_resize] -/ -def hash_map_insert_no_resize_fwd_back +def HashMap.insert_no_resize_fwd_back (T : Type) (self : hash_map_t T) (key : Usize) (value : T) : Result (hash_map_t T) := @@ -125,18 +124,18 @@ def hash_map_insert_no_resize_fwd_back let i := vec_len (list_t T) self.hash_map_slots let hash_mod ← hash % i let l ← vec_index_mut_fwd (list_t T) self.hash_map_slots hash_mod - let inserted ← hash_map_insert_in_list_fwd T key value l + let inserted ← HashMap.insert_in_list_fwd T key value l if inserted then do let i0 ← self.hash_map_num_entries + (Usize.ofInt 1 (by intlit)) - let l0 ← hash_map_insert_in_list_back T key value l + let l0 ← HashMap.insert_in_list_back T key value l let v ← vec_index_mut_back (list_t T) self.hash_map_slots hash_mod l0 Result.ret { self with hash_map_num_entries := i0, hash_map_slots := v } else do - let l0 ← hash_map_insert_in_list_back T key value l + let l0 ← HashMap.insert_in_list_back T key value l let v ← vec_index_mut_back (list_t T) self.hash_map_slots hash_mod l0 Result.ret { self with hash_map_slots := v } @@ -146,22 +145,22 @@ def core_num_u32_max_body : Result U32 := def core_num_u32_max_c : U32 := eval_global core_num_u32_max_body (by simp) /- [hashmap::HashMap::{0}::move_elements_from_list] -/ -divergent def hash_map_move_elements_from_list_loop_fwd_back +divergent def HashMap.move_elements_from_list_loop_fwd_back (T : Type) (ntable : hash_map_t T) (ls : list_t T) : Result (hash_map_t T) := match ls with | list_t.Cons k v tl => do - let ntable0 ← hash_map_insert_no_resize_fwd_back T ntable k v - hash_map_move_elements_from_list_loop_fwd_back T ntable0 tl + let ntable0 ← HashMap.insert_no_resize_fwd_back T ntable k v + HashMap.move_elements_from_list_loop_fwd_back T ntable0 tl | list_t.Nil => Result.ret ntable /- [hashmap::HashMap::{0}::move_elements_from_list] -/ -def hash_map_move_elements_from_list_fwd_back +def HashMap.move_elements_from_list_fwd_back (T : Type) (ntable : hash_map_t T) (ls : list_t T) : Result (hash_map_t T) := - hash_map_move_elements_from_list_loop_fwd_back T ntable ls + HashMap.move_elements_from_list_loop_fwd_back T ntable ls /- [hashmap::HashMap::{0}::move_elements] -/ -divergent def hash_map_move_elements_loop_fwd_back +divergent def HashMap.move_elements_loop_fwd_back (T : Type) (ntable : hash_map_t T) (slots : Vec (list_t T)) (i : Usize) : Result ((hash_map_t T) × (Vec (list_t T))) := @@ -171,22 +170,22 @@ divergent def hash_map_move_elements_loop_fwd_back do let l ← vec_index_mut_fwd (list_t T) slots i let ls := mem_replace_fwd (list_t T) l list_t.Nil - let ntable0 ← hash_map_move_elements_from_list_fwd_back T ntable ls + let ntable0 ← HashMap.move_elements_from_list_fwd_back T ntable ls let i1 ← i + (Usize.ofInt 1 (by intlit)) let l0 := mem_replace_back (list_t T) l list_t.Nil let slots0 ← vec_index_mut_back (list_t T) slots i l0 - hash_map_move_elements_loop_fwd_back T ntable0 slots0 i1 + HashMap.move_elements_loop_fwd_back T ntable0 slots0 i1 else Result.ret (ntable, slots) /- [hashmap::HashMap::{0}::move_elements] -/ -def hash_map_move_elements_fwd_back +def HashMap.move_elements_fwd_back (T : Type) (ntable : hash_map_t T) (slots : Vec (list_t T)) (i : Usize) : Result ((hash_map_t T) × (Vec (list_t T))) := - hash_map_move_elements_loop_fwd_back T ntable slots i + HashMap.move_elements_loop_fwd_back T ntable slots i /- [hashmap::HashMap::{0}::try_resize] -/ -def hash_map_try_resize_fwd_back +def HashMap.try_resize_fwd_back (T : Type) (self : hash_map_t T) : Result (hash_map_t T) := do let max_usize ← Scalar.cast .Usize core_num_u32_max_c @@ -198,9 +197,9 @@ def hash_map_try_resize_fwd_back then do let i2 ← capacity * (Usize.ofInt 2 (by intlit)) - let ntable ← hash_map_new_with_capacity_fwd T i2 i i0 + let ntable ← HashMap.new_with_capacity_fwd T i2 i i0 let (ntable0, _) ← - hash_map_move_elements_fwd_back T ntable self.hash_map_slots + HashMap.move_elements_fwd_back T ntable self.hash_map_slots (Usize.ofInt 0 (by intlit)) Result.ret { @@ -212,84 +211,84 @@ def hash_map_try_resize_fwd_back else Result.ret { self with hash_map_max_load_factor := (i, i0) } /- [hashmap::HashMap::{0}::insert] -/ -def hash_map_insert_fwd_back +def HashMap.insert_fwd_back (T : Type) (self : hash_map_t T) (key : Usize) (value : T) : Result (hash_map_t T) := do - let self0 ← hash_map_insert_no_resize_fwd_back T self key value - let i ← hash_map_len_fwd T self0 + let self0 ← HashMap.insert_no_resize_fwd_back T self key value + let i ← HashMap.len_fwd T self0 if i > self0.hash_map_max_load - then hash_map_try_resize_fwd_back T self0 + then HashMap.try_resize_fwd_back T self0 else Result.ret self0 /- [hashmap::HashMap::{0}::contains_key_in_list] -/ -divergent def hash_map_contains_key_in_list_loop_fwd +divergent def HashMap.contains_key_in_list_loop_fwd (T : Type) (key : Usize) (ls : list_t T) : Result Bool := match ls with | list_t.Cons ckey t tl => if ckey = key then Result.ret true - else hash_map_contains_key_in_list_loop_fwd T key tl + else HashMap.contains_key_in_list_loop_fwd T key tl | list_t.Nil => Result.ret false /- [hashmap::HashMap::{0}::contains_key_in_list] -/ -def hash_map_contains_key_in_list_fwd +def HashMap.contains_key_in_list_fwd (T : Type) (key : Usize) (ls : list_t T) : Result Bool := - hash_map_contains_key_in_list_loop_fwd T key ls + HashMap.contains_key_in_list_loop_fwd T key ls /- [hashmap::HashMap::{0}::contains_key] -/ -def hash_map_contains_key_fwd +def HashMap.contains_key_fwd (T : Type) (self : hash_map_t T) (key : Usize) : Result Bool := do let hash ← hash_key_fwd key let i := vec_len (list_t T) self.hash_map_slots let hash_mod ← hash % i let l ← vec_index_fwd (list_t T) self.hash_map_slots hash_mod - hash_map_contains_key_in_list_fwd T key l + HashMap.contains_key_in_list_fwd T key l /- [hashmap::HashMap::{0}::get_in_list] -/ -divergent def hash_map_get_in_list_loop_fwd +divergent def HashMap.get_in_list_loop_fwd (T : Type) (key : Usize) (ls : list_t T) : Result T := match ls with | list_t.Cons ckey cvalue tl => if ckey = key then Result.ret cvalue - else hash_map_get_in_list_loop_fwd T key tl + else HashMap.get_in_list_loop_fwd T key tl | list_t.Nil => Result.fail Error.panic /- [hashmap::HashMap::{0}::get_in_list] -/ -def hash_map_get_in_list_fwd +def HashMap.get_in_list_fwd (T : Type) (key : Usize) (ls : list_t T) : Result T := - hash_map_get_in_list_loop_fwd T key ls + HashMap.get_in_list_loop_fwd T key ls /- [hashmap::HashMap::{0}::get] -/ -def hash_map_get_fwd +def HashMap.get_fwd (T : Type) (self : hash_map_t T) (key : Usize) : Result T := do let hash ← hash_key_fwd key let i := vec_len (list_t T) self.hash_map_slots let hash_mod ← hash % i let l ← vec_index_fwd (list_t T) self.hash_map_slots hash_mod - hash_map_get_in_list_fwd T key l + HashMap.get_in_list_fwd T key l /- [hashmap::HashMap::{0}::get_mut_in_list] -/ -divergent def hash_map_get_mut_in_list_loop_fwd +divergent def HashMap.get_mut_in_list_loop_fwd (T : Type) (ls : list_t T) (key : Usize) : Result T := match ls with | list_t.Cons ckey cvalue tl => if ckey = key then Result.ret cvalue - else hash_map_get_mut_in_list_loop_fwd T tl key + else HashMap.get_mut_in_list_loop_fwd T tl key | list_t.Nil => Result.fail Error.panic /- [hashmap::HashMap::{0}::get_mut_in_list] -/ -def hash_map_get_mut_in_list_fwd +def HashMap.get_mut_in_list_fwd (T : Type) (ls : list_t T) (key : Usize) : Result T := - hash_map_get_mut_in_list_loop_fwd T ls key + HashMap.get_mut_in_list_loop_fwd T ls key /- [hashmap::HashMap::{0}::get_mut_in_list] -/ -divergent def hash_map_get_mut_in_list_loop_back +divergent def HashMap.get_mut_in_list_loop_back (T : Type) (ls : list_t T) (key : Usize) (ret0 : T) : Result (list_t T) := match ls with | list_t.Cons ckey cvalue tl => @@ -297,27 +296,27 @@ divergent def hash_map_get_mut_in_list_loop_back then Result.ret (list_t.Cons ckey ret0 tl) else do - let tl0 ← hash_map_get_mut_in_list_loop_back T tl key ret0 + let tl0 ← HashMap.get_mut_in_list_loop_back T tl key ret0 Result.ret (list_t.Cons ckey cvalue tl0) | list_t.Nil => Result.fail Error.panic /- [hashmap::HashMap::{0}::get_mut_in_list] -/ -def hash_map_get_mut_in_list_back +def HashMap.get_mut_in_list_back (T : Type) (ls : list_t T) (key : Usize) (ret0 : T) : Result (list_t T) := - hash_map_get_mut_in_list_loop_back T ls key ret0 + HashMap.get_mut_in_list_loop_back T ls key ret0 /- [hashmap::HashMap::{0}::get_mut] -/ -def hash_map_get_mut_fwd +def HashMap.get_mut_fwd (T : Type) (self : hash_map_t T) (key : Usize) : Result T := do let hash ← hash_key_fwd key let i := vec_len (list_t T) self.hash_map_slots let hash_mod ← hash % i let l ← vec_index_mut_fwd (list_t T) self.hash_map_slots hash_mod - hash_map_get_mut_in_list_fwd T l key + HashMap.get_mut_in_list_fwd T l key /- [hashmap::HashMap::{0}::get_mut] -/ -def hash_map_get_mut_back +def HashMap.get_mut_back (T : Type) (self : hash_map_t T) (key : Usize) (ret0 : T) : Result (hash_map_t T) := @@ -326,12 +325,12 @@ def hash_map_get_mut_back let i := vec_len (list_t T) self.hash_map_slots let hash_mod ← hash % i let l ← vec_index_mut_fwd (list_t T) self.hash_map_slots hash_mod - let l0 ← hash_map_get_mut_in_list_back T l key ret0 + let l0 ← HashMap.get_mut_in_list_back T l key ret0 let v ← vec_index_mut_back (list_t T) self.hash_map_slots hash_mod l0 Result.ret { self with hash_map_slots := v } /- [hashmap::HashMap::{0}::remove_from_list] -/ -divergent def hash_map_remove_from_list_loop_fwd +divergent def HashMap.remove_from_list_loop_fwd (T : Type) (key : Usize) (ls : list_t T) : Result (Option T) := match ls with | list_t.Cons ckey t tl => @@ -342,16 +341,16 @@ divergent def hash_map_remove_from_list_loop_fwd match mv_ls with | list_t.Cons i cvalue tl0 => Result.ret (Option.some cvalue) | list_t.Nil => Result.fail Error.panic - else hash_map_remove_from_list_loop_fwd T key tl + else HashMap.remove_from_list_loop_fwd T key tl | list_t.Nil => Result.ret Option.none /- [hashmap::HashMap::{0}::remove_from_list] -/ -def hash_map_remove_from_list_fwd +def HashMap.remove_from_list_fwd (T : Type) (key : Usize) (ls : list_t T) : Result (Option T) := - hash_map_remove_from_list_loop_fwd T key ls + HashMap.remove_from_list_loop_fwd T key ls /- [hashmap::HashMap::{0}::remove_from_list] -/ -divergent def hash_map_remove_from_list_loop_back +divergent def HashMap.remove_from_list_loop_back (T : Type) (key : Usize) (ls : list_t T) : Result (list_t T) := match ls with | list_t.Cons ckey t tl => @@ -364,24 +363,24 @@ divergent def hash_map_remove_from_list_loop_back | list_t.Nil => Result.fail Error.panic else do - let tl0 ← hash_map_remove_from_list_loop_back T key tl + let tl0 ← HashMap.remove_from_list_loop_back T key tl Result.ret (list_t.Cons ckey t tl0) | list_t.Nil => Result.ret list_t.Nil /- [hashmap::HashMap::{0}::remove_from_list] -/ -def hash_map_remove_from_list_back +def HashMap.remove_from_list_back (T : Type) (key : Usize) (ls : list_t T) : Result (list_t T) := - hash_map_remove_from_list_loop_back T key ls + HashMap.remove_from_list_loop_back T key ls /- [hashmap::HashMap::{0}::remove] -/ -def hash_map_remove_fwd +def HashMap.remove_fwd (T : Type) (self : hash_map_t T) (key : Usize) : Result (Option T) := do let hash ← hash_key_fwd key let i := vec_len (list_t T) self.hash_map_slots let hash_mod ← hash % i let l ← vec_index_mut_fwd (list_t T) self.hash_map_slots hash_mod - let x ← hash_map_remove_from_list_fwd T key l + let x ← HashMap.remove_from_list_fwd T key l match x with | Option.none => Result.ret Option.none | Option.some x0 => @@ -390,24 +389,24 @@ def hash_map_remove_fwd Result.ret (Option.some x0) /- [hashmap::HashMap::{0}::remove] -/ -def hash_map_remove_back +def HashMap.remove_back (T : Type) (self : hash_map_t T) (key : Usize) : Result (hash_map_t T) := do let hash ← hash_key_fwd key let i := vec_len (list_t T) self.hash_map_slots let hash_mod ← hash % i let l ← vec_index_mut_fwd (list_t T) self.hash_map_slots hash_mod - let x ← hash_map_remove_from_list_fwd T key l + let x ← HashMap.remove_from_list_fwd T key l match x with | Option.none => do - let l0 ← hash_map_remove_from_list_back T key l + let l0 ← HashMap.remove_from_list_back T key l let v ← vec_index_mut_back (list_t T) self.hash_map_slots hash_mod l0 Result.ret { self with hash_map_slots := v } | Option.some x0 => do let i0 ← self.hash_map_num_entries - (Usize.ofInt 1 (by intlit)) - let l0 ← hash_map_remove_from_list_back T key l + let l0 ← HashMap.remove_from_list_back T key l let v ← vec_index_mut_back (list_t T) self.hash_map_slots hash_mod l0 Result.ret { self with hash_map_num_entries := i0, hash_map_slots := v } @@ -415,34 +414,33 @@ def hash_map_remove_back /- [hashmap::test1] -/ def test1_fwd : Result Unit := do - let hm ← hash_map_new_fwd U64 + let hm ← HashMap.new_fwd U64 let hm0 ← - hash_map_insert_fwd_back U64 hm (Usize.ofInt 0 (by intlit)) + HashMap.insert_fwd_back U64 hm (Usize.ofInt 0 (by intlit)) (U64.ofInt 42 (by intlit)) let hm1 ← - hash_map_insert_fwd_back U64 hm0 (Usize.ofInt 128 (by intlit)) + HashMap.insert_fwd_back U64 hm0 (Usize.ofInt 128 (by intlit)) (U64.ofInt 18 (by intlit)) let hm2 ← - hash_map_insert_fwd_back U64 hm1 (Usize.ofInt 1024 (by intlit)) + HashMap.insert_fwd_back U64 hm1 (Usize.ofInt 1024 (by intlit)) (U64.ofInt 138 (by intlit)) let hm3 ← - hash_map_insert_fwd_back U64 hm2 (Usize.ofInt 1056 (by intlit)) + HashMap.insert_fwd_back U64 hm2 (Usize.ofInt 1056 (by intlit)) (U64.ofInt 256 (by intlit)) - let i ← hash_map_get_fwd U64 hm3 (Usize.ofInt 128 (by intlit)) + let i ← HashMap.get_fwd U64 hm3 (Usize.ofInt 128 (by intlit)) if not (i = (U64.ofInt 18 (by intlit))) then Result.fail Error.panic else do let hm4 ← - hash_map_get_mut_back U64 hm3 (Usize.ofInt 1024 (by intlit)) + HashMap.get_mut_back U64 hm3 (Usize.ofInt 1024 (by intlit)) (U64.ofInt 56 (by intlit)) - let i0 ← hash_map_get_fwd U64 hm4 (Usize.ofInt 1024 (by intlit)) + let i0 ← HashMap.get_fwd U64 hm4 (Usize.ofInt 1024 (by intlit)) if not (i0 = (U64.ofInt 56 (by intlit))) then Result.fail Error.panic else do - let x ← - hash_map_remove_fwd U64 hm4 (Usize.ofInt 1024 (by intlit)) + let x ← HashMap.remove_fwd U64 hm4 (Usize.ofInt 1024 (by intlit)) match x with | Option.none => Result.fail Error.panic | Option.some x0 => @@ -451,21 +449,21 @@ def test1_fwd : Result Unit := else do let hm5 ← - hash_map_remove_back U64 hm4 (Usize.ofInt 1024 (by intlit)) + HashMap.remove_back U64 hm4 (Usize.ofInt 1024 (by intlit)) let i1 ← - hash_map_get_fwd U64 hm5 (Usize.ofInt 0 (by intlit)) + HashMap.get_fwd U64 hm5 (Usize.ofInt 0 (by intlit)) if not (i1 = (U64.ofInt 42 (by intlit))) then Result.fail Error.panic else do let i2 ← - hash_map_get_fwd U64 hm5 (Usize.ofInt 128 (by intlit)) + HashMap.get_fwd U64 hm5 (Usize.ofInt 128 (by intlit)) if not (i2 = (U64.ofInt 18 (by intlit))) then Result.fail Error.panic else do let i3 ← - hash_map_get_fwd U64 hm5 + HashMap.get_fwd U64 hm5 (Usize.ofInt 1056 (by intlit)) if not (i3 = (U64.ofInt 256 (by intlit))) then Result.fail Error.panic @@ -474,4 +472,4 @@ def test1_fwd : Result Unit := /- Unit test for [hashmap::test1] -/ #assert (test1_fwd == .ret ()) -end Hashmap +end hashmap diff --git a/tests/lean/Hashmap/Types.lean b/tests/lean/Hashmap/Types.lean index 0aec6acf..75a6500f 100644 --- a/tests/lean/Hashmap/Types.lean +++ b/tests/lean/Hashmap/Types.lean @@ -2,8 +2,7 @@ -- [hashmap]: type definitions import Base open Primitives - -namespace Hashmap +namespace hashmap /- [hashmap::List] -/ inductive list_t (T : Type) := @@ -17,4 +16,4 @@ structure hash_map_t (T : Type) where hash_map_max_load : Usize hash_map_slots : Vec (list_t T) -end Hashmap +end hashmap diff --git a/tests/lean/HashmapMain/ExternalFuns.lean b/tests/lean/HashmapMain/ExternalFuns.lean deleted file mode 100644 index bc831158..00000000 --- a/tests/lean/HashmapMain/ExternalFuns.lean +++ /dev/null @@ -1,9 +0,0 @@ -import Base -import HashmapMain.Types -import HashmapMain.Opaque - -namespace HashmapMain - -def opaque_defs : OpaqueDefs := by sorry - -end HashmapMain diff --git a/tests/lean/HashmapMain/Funs.lean b/tests/lean/HashmapMain/Funs.lean index 34a0eca1..06929431 100644 --- a/tests/lean/HashmapMain/Funs.lean +++ b/tests/lean/HashmapMain/Funs.lean @@ -2,17 +2,16 @@ -- [hashmap_main]: function definitions import Base import HashmapMain.Types -import HashmapMain.ExternalFuns +import HashmapMain.FunsExternal open Primitives - -namespace HashmapMain +namespace hashmap_main /- [hashmap_main::hashmap::hash_key] -/ -def hashmap_hash_key_fwd (k : Usize) : Result Usize := +def hashmap.hash_key_fwd (k : Usize) : Result Usize := Result.ret k /- [hashmap_main::hashmap::HashMap::{0}::allocate_slots] -/ -divergent def hashmap_hash_map_allocate_slots_loop_fwd +divergent def hashmap.HashMap.allocate_slots_loop_fwd (T : Type) (slots : Vec (hashmap_list_t T)) (n : Usize) : Result (Vec (hashmap_list_t T)) := @@ -21,25 +20,25 @@ divergent def hashmap_hash_map_allocate_slots_loop_fwd do let slots0 ← vec_push_back (hashmap_list_t T) slots hashmap_list_t.Nil let n0 ← n - (Usize.ofInt 1 (by intlit)) - hashmap_hash_map_allocate_slots_loop_fwd T slots0 n0 + hashmap.HashMap.allocate_slots_loop_fwd T slots0 n0 else Result.ret slots /- [hashmap_main::hashmap::HashMap::{0}::allocate_slots] -/ -def hashmap_hash_map_allocate_slots_fwd +def hashmap.HashMap.allocate_slots_fwd (T : Type) (slots : Vec (hashmap_list_t T)) (n : Usize) : Result (Vec (hashmap_list_t T)) := - hashmap_hash_map_allocate_slots_loop_fwd T slots n + hashmap.HashMap.allocate_slots_loop_fwd T slots n /- [hashmap_main::hashmap::HashMap::{0}::new_with_capacity] -/ -def hashmap_hash_map_new_with_capacity_fwd +def hashmap.HashMap.new_with_capacity_fwd (T : Type) (capacity : Usize) (max_load_dividend : Usize) (max_load_divisor : Usize) : Result (hashmap_hash_map_t T) := do let v := vec_new (hashmap_list_t T) - let slots ← hashmap_hash_map_allocate_slots_fwd T v capacity + let slots ← hashmap.HashMap.allocate_slots_fwd T v capacity let i ← capacity * max_load_dividend let i0 ← i / max_load_divisor Result.ret @@ -52,12 +51,12 @@ def hashmap_hash_map_new_with_capacity_fwd } /- [hashmap_main::hashmap::HashMap::{0}::new] -/ -def hashmap_hash_map_new_fwd (T : Type) : Result (hashmap_hash_map_t T) := - hashmap_hash_map_new_with_capacity_fwd T (Usize.ofInt 32 (by intlit)) +def hashmap.HashMap.new_fwd (T : Type) : Result (hashmap_hash_map_t T) := + hashmap.HashMap.new_with_capacity_fwd T (Usize.ofInt 32 (by intlit)) (Usize.ofInt 4 (by intlit)) (Usize.ofInt 5 (by intlit)) /- [hashmap_main::hashmap::HashMap::{0}::clear] -/ -divergent def hashmap_hash_map_clear_loop_fwd_back +divergent def hashmap.HashMap.clear_loop_fwd_back (T : Type) (slots : Vec (hashmap_list_t T)) (i : Usize) : Result (Vec (hashmap_list_t T)) := @@ -68,15 +67,15 @@ divergent def hashmap_hash_map_clear_loop_fwd_back let i1 ← i + (Usize.ofInt 1 (by intlit)) let slots0 ← vec_index_mut_back (hashmap_list_t T) slots i hashmap_list_t.Nil - hashmap_hash_map_clear_loop_fwd_back T slots0 i1 + hashmap.HashMap.clear_loop_fwd_back T slots0 i1 else Result.ret slots /- [hashmap_main::hashmap::HashMap::{0}::clear] -/ -def hashmap_hash_map_clear_fwd_back +def hashmap.HashMap.clear_fwd_back (T : Type) (self : hashmap_hash_map_t T) : Result (hashmap_hash_map_t T) := do let v ← - hashmap_hash_map_clear_loop_fwd_back T self.hashmap_hash_map_slots + hashmap.HashMap.clear_loop_fwd_back T self.hashmap_hash_map_slots (Usize.ofInt 0 (by intlit)) Result.ret { @@ -87,27 +86,27 @@ def hashmap_hash_map_clear_fwd_back } /- [hashmap_main::hashmap::HashMap::{0}::len] -/ -def hashmap_hash_map_len_fwd +def hashmap.HashMap.len_fwd (T : Type) (self : hashmap_hash_map_t T) : Result Usize := Result.ret self.hashmap_hash_map_num_entries /- [hashmap_main::hashmap::HashMap::{0}::insert_in_list] -/ -divergent def hashmap_hash_map_insert_in_list_loop_fwd +divergent def hashmap.HashMap.insert_in_list_loop_fwd (T : Type) (key : Usize) (value : T) (ls : hashmap_list_t T) : Result Bool := match ls with | hashmap_list_t.Cons ckey cvalue tl => if ckey = key then Result.ret false - else hashmap_hash_map_insert_in_list_loop_fwd T key value tl + else hashmap.HashMap.insert_in_list_loop_fwd T key value tl | hashmap_list_t.Nil => Result.ret true /- [hashmap_main::hashmap::HashMap::{0}::insert_in_list] -/ -def hashmap_hash_map_insert_in_list_fwd +def hashmap.HashMap.insert_in_list_fwd (T : Type) (key : Usize) (value : T) (ls : hashmap_list_t T) : Result Bool := - hashmap_hash_map_insert_in_list_loop_fwd T key value ls + hashmap.HashMap.insert_in_list_loop_fwd T key value ls /- [hashmap_main::hashmap::HashMap::{0}::insert_in_list] -/ -divergent def hashmap_hash_map_insert_in_list_loop_back +divergent def hashmap.HashMap.insert_in_list_loop_back (T : Type) (key : Usize) (value : T) (ls : hashmap_list_t T) : Result (hashmap_list_t T) := @@ -117,37 +116,37 @@ divergent def hashmap_hash_map_insert_in_list_loop_back then Result.ret (hashmap_list_t.Cons ckey value tl) else do - let tl0 ← hashmap_hash_map_insert_in_list_loop_back T key value tl + let tl0 ← hashmap.HashMap.insert_in_list_loop_back T key value tl Result.ret (hashmap_list_t.Cons ckey cvalue tl0) | hashmap_list_t.Nil => let l := hashmap_list_t.Nil Result.ret (hashmap_list_t.Cons key value l) /- [hashmap_main::hashmap::HashMap::{0}::insert_in_list] -/ -def hashmap_hash_map_insert_in_list_back +def hashmap.HashMap.insert_in_list_back (T : Type) (key : Usize) (value : T) (ls : hashmap_list_t T) : Result (hashmap_list_t T) := - hashmap_hash_map_insert_in_list_loop_back T key value ls + hashmap.HashMap.insert_in_list_loop_back T key value ls /- [hashmap_main::hashmap::HashMap::{0}::insert_no_resize] -/ -def hashmap_hash_map_insert_no_resize_fwd_back +def hashmap.HashMap.insert_no_resize_fwd_back (T : Type) (self : hashmap_hash_map_t T) (key : Usize) (value : T) : Result (hashmap_hash_map_t T) := do - let hash ← hashmap_hash_key_fwd key + let hash ← hashmap.hash_key_fwd key let i := vec_len (hashmap_list_t T) self.hashmap_hash_map_slots let hash_mod ← hash % i let l ← vec_index_mut_fwd (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod - let inserted ← hashmap_hash_map_insert_in_list_fwd T key value l + let inserted ← hashmap.HashMap.insert_in_list_fwd T key value l if inserted then do let i0 ← self.hashmap_hash_map_num_entries + (Usize.ofInt 1 (by intlit)) - let l0 ← hashmap_hash_map_insert_in_list_back T key value l + let l0 ← hashmap.HashMap.insert_in_list_back T key value l let v ← vec_index_mut_back (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod l0 @@ -159,7 +158,7 @@ def hashmap_hash_map_insert_no_resize_fwd_back } else do - let l0 ← hashmap_hash_map_insert_in_list_back T key value l + let l0 ← hashmap.HashMap.insert_in_list_back T key value l let v ← vec_index_mut_back (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod l0 @@ -171,26 +170,26 @@ def core_num_u32_max_body : Result U32 := def core_num_u32_max_c : U32 := eval_global core_num_u32_max_body (by simp) /- [hashmap_main::hashmap::HashMap::{0}::move_elements_from_list] -/ -divergent def hashmap_hash_map_move_elements_from_list_loop_fwd_back +divergent def hashmap.HashMap.move_elements_from_list_loop_fwd_back (T : Type) (ntable : hashmap_hash_map_t T) (ls : hashmap_list_t T) : Result (hashmap_hash_map_t T) := match ls with | hashmap_list_t.Cons k v tl => do - let ntable0 ← hashmap_hash_map_insert_no_resize_fwd_back T ntable k v - hashmap_hash_map_move_elements_from_list_loop_fwd_back T ntable0 tl + let ntable0 ← hashmap.HashMap.insert_no_resize_fwd_back T ntable k v + hashmap.HashMap.move_elements_from_list_loop_fwd_back T ntable0 tl | hashmap_list_t.Nil => Result.ret ntable /- [hashmap_main::hashmap::HashMap::{0}::move_elements_from_list] -/ -def hashmap_hash_map_move_elements_from_list_fwd_back +def hashmap.HashMap.move_elements_from_list_fwd_back (T : Type) (ntable : hashmap_hash_map_t T) (ls : hashmap_list_t T) : Result (hashmap_hash_map_t T) := - hashmap_hash_map_move_elements_from_list_loop_fwd_back T ntable ls + hashmap.HashMap.move_elements_from_list_loop_fwd_back T ntable ls /- [hashmap_main::hashmap::HashMap::{0}::move_elements] -/ -divergent def hashmap_hash_map_move_elements_loop_fwd_back +divergent def hashmap.HashMap.move_elements_loop_fwd_back (T : Type) (ntable : hashmap_hash_map_t T) (slots : Vec (hashmap_list_t T)) (i : Usize) : Result ((hashmap_hash_map_t T) × (Vec (hashmap_list_t T))) @@ -202,23 +201,23 @@ divergent def hashmap_hash_map_move_elements_loop_fwd_back let l ← vec_index_mut_fwd (hashmap_list_t T) slots i let ls := mem_replace_fwd (hashmap_list_t T) l hashmap_list_t.Nil let ntable0 ← - hashmap_hash_map_move_elements_from_list_fwd_back T ntable ls + hashmap.HashMap.move_elements_from_list_fwd_back T ntable ls let i1 ← i + (Usize.ofInt 1 (by intlit)) let l0 := mem_replace_back (hashmap_list_t T) l hashmap_list_t.Nil let slots0 ← vec_index_mut_back (hashmap_list_t T) slots i l0 - hashmap_hash_map_move_elements_loop_fwd_back T ntable0 slots0 i1 + hashmap.HashMap.move_elements_loop_fwd_back T ntable0 slots0 i1 else Result.ret (ntable, slots) /- [hashmap_main::hashmap::HashMap::{0}::move_elements] -/ -def hashmap_hash_map_move_elements_fwd_back +def hashmap.HashMap.move_elements_fwd_back (T : Type) (ntable : hashmap_hash_map_t T) (slots : Vec (hashmap_list_t T)) (i : Usize) : Result ((hashmap_hash_map_t T) × (Vec (hashmap_list_t T))) := - hashmap_hash_map_move_elements_loop_fwd_back T ntable slots i + hashmap.HashMap.move_elements_loop_fwd_back T ntable slots i /- [hashmap_main::hashmap::HashMap::{0}::try_resize] -/ -def hashmap_hash_map_try_resize_fwd_back +def hashmap.HashMap.try_resize_fwd_back (T : Type) (self : hashmap_hash_map_t T) : Result (hashmap_hash_map_t T) := do let max_usize ← Scalar.cast .Usize core_num_u32_max_c @@ -230,9 +229,9 @@ def hashmap_hash_map_try_resize_fwd_back then do let i2 ← capacity * (Usize.ofInt 2 (by intlit)) - let ntable ← hashmap_hash_map_new_with_capacity_fwd T i2 i i0 + let ntable ← hashmap.HashMap.new_with_capacity_fwd T i2 i i0 let (ntable0, _) ← - hashmap_hash_map_move_elements_fwd_back T ntable + hashmap.HashMap.move_elements_fwd_back T ntable self.hashmap_hash_map_slots (Usize.ofInt 0 (by intlit)) Result.ret { @@ -244,86 +243,86 @@ def hashmap_hash_map_try_resize_fwd_back else Result.ret { self with hashmap_hash_map_max_load_factor := (i, i0) } /- [hashmap_main::hashmap::HashMap::{0}::insert] -/ -def hashmap_hash_map_insert_fwd_back +def hashmap.HashMap.insert_fwd_back (T : Type) (self : hashmap_hash_map_t T) (key : Usize) (value : T) : Result (hashmap_hash_map_t T) := do - let self0 ← hashmap_hash_map_insert_no_resize_fwd_back T self key value - let i ← hashmap_hash_map_len_fwd T self0 + let self0 ← hashmap.HashMap.insert_no_resize_fwd_back T self key value + let i ← hashmap.HashMap.len_fwd T self0 if i > self0.hashmap_hash_map_max_load - then hashmap_hash_map_try_resize_fwd_back T self0 + then hashmap.HashMap.try_resize_fwd_back T self0 else Result.ret self0 /- [hashmap_main::hashmap::HashMap::{0}::contains_key_in_list] -/ -divergent def hashmap_hash_map_contains_key_in_list_loop_fwd +divergent def hashmap.HashMap.contains_key_in_list_loop_fwd (T : Type) (key : Usize) (ls : hashmap_list_t T) : Result Bool := match ls with | hashmap_list_t.Cons ckey t tl => if ckey = key then Result.ret true - else hashmap_hash_map_contains_key_in_list_loop_fwd T key tl + else hashmap.HashMap.contains_key_in_list_loop_fwd T key tl | hashmap_list_t.Nil => Result.ret false /- [hashmap_main::hashmap::HashMap::{0}::contains_key_in_list] -/ -def hashmap_hash_map_contains_key_in_list_fwd +def hashmap.HashMap.contains_key_in_list_fwd (T : Type) (key : Usize) (ls : hashmap_list_t T) : Result Bool := - hashmap_hash_map_contains_key_in_list_loop_fwd T key ls + hashmap.HashMap.contains_key_in_list_loop_fwd T key ls /- [hashmap_main::hashmap::HashMap::{0}::contains_key] -/ -def hashmap_hash_map_contains_key_fwd +def hashmap.HashMap.contains_key_fwd (T : Type) (self : hashmap_hash_map_t T) (key : Usize) : Result Bool := do - let hash ← hashmap_hash_key_fwd key + let hash ← hashmap.hash_key_fwd key let i := vec_len (hashmap_list_t T) self.hashmap_hash_map_slots let hash_mod ← hash % i let l ← vec_index_fwd (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod - hashmap_hash_map_contains_key_in_list_fwd T key l + hashmap.HashMap.contains_key_in_list_fwd T key l /- [hashmap_main::hashmap::HashMap::{0}::get_in_list] -/ -divergent def hashmap_hash_map_get_in_list_loop_fwd +divergent def hashmap.HashMap.get_in_list_loop_fwd (T : Type) (key : Usize) (ls : hashmap_list_t T) : Result T := match ls with | hashmap_list_t.Cons ckey cvalue tl => if ckey = key then Result.ret cvalue - else hashmap_hash_map_get_in_list_loop_fwd T key tl + else hashmap.HashMap.get_in_list_loop_fwd T key tl | hashmap_list_t.Nil => Result.fail Error.panic /- [hashmap_main::hashmap::HashMap::{0}::get_in_list] -/ -def hashmap_hash_map_get_in_list_fwd +def hashmap.HashMap.get_in_list_fwd (T : Type) (key : Usize) (ls : hashmap_list_t T) : Result T := - hashmap_hash_map_get_in_list_loop_fwd T key ls + hashmap.HashMap.get_in_list_loop_fwd T key ls /- [hashmap_main::hashmap::HashMap::{0}::get] -/ -def hashmap_hash_map_get_fwd +def hashmap.HashMap.get_fwd (T : Type) (self : hashmap_hash_map_t T) (key : Usize) : Result T := do - let hash ← hashmap_hash_key_fwd key + let hash ← hashmap.hash_key_fwd key let i := vec_len (hashmap_list_t T) self.hashmap_hash_map_slots let hash_mod ← hash % i let l ← vec_index_fwd (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod - hashmap_hash_map_get_in_list_fwd T key l + hashmap.HashMap.get_in_list_fwd T key l /- [hashmap_main::hashmap::HashMap::{0}::get_mut_in_list] -/ -divergent def hashmap_hash_map_get_mut_in_list_loop_fwd +divergent def hashmap.HashMap.get_mut_in_list_loop_fwd (T : Type) (ls : hashmap_list_t T) (key : Usize) : Result T := match ls with | hashmap_list_t.Cons ckey cvalue tl => if ckey = key then Result.ret cvalue - else hashmap_hash_map_get_mut_in_list_loop_fwd T tl key + else hashmap.HashMap.get_mut_in_list_loop_fwd T tl key | hashmap_list_t.Nil => Result.fail Error.panic /- [hashmap_main::hashmap::HashMap::{0}::get_mut_in_list] -/ -def hashmap_hash_map_get_mut_in_list_fwd +def hashmap.HashMap.get_mut_in_list_fwd (T : Type) (ls : hashmap_list_t T) (key : Usize) : Result T := - hashmap_hash_map_get_mut_in_list_loop_fwd T ls key + hashmap.HashMap.get_mut_in_list_loop_fwd T ls key /- [hashmap_main::hashmap::HashMap::{0}::get_mut_in_list] -/ -divergent def hashmap_hash_map_get_mut_in_list_loop_back +divergent def hashmap.HashMap.get_mut_in_list_loop_back (T : Type) (ls : hashmap_list_t T) (key : Usize) (ret0 : T) : Result (hashmap_list_t T) := @@ -333,47 +332,47 @@ divergent def hashmap_hash_map_get_mut_in_list_loop_back then Result.ret (hashmap_list_t.Cons ckey ret0 tl) else do - let tl0 ← hashmap_hash_map_get_mut_in_list_loop_back T tl key ret0 + let tl0 ← hashmap.HashMap.get_mut_in_list_loop_back T tl key ret0 Result.ret (hashmap_list_t.Cons ckey cvalue tl0) | hashmap_list_t.Nil => Result.fail Error.panic /- [hashmap_main::hashmap::HashMap::{0}::get_mut_in_list] -/ -def hashmap_hash_map_get_mut_in_list_back +def hashmap.HashMap.get_mut_in_list_back (T : Type) (ls : hashmap_list_t T) (key : Usize) (ret0 : T) : Result (hashmap_list_t T) := - hashmap_hash_map_get_mut_in_list_loop_back T ls key ret0 + hashmap.HashMap.get_mut_in_list_loop_back T ls key ret0 /- [hashmap_main::hashmap::HashMap::{0}::get_mut] -/ -def hashmap_hash_map_get_mut_fwd +def hashmap.HashMap.get_mut_fwd (T : Type) (self : hashmap_hash_map_t T) (key : Usize) : Result T := do - let hash ← hashmap_hash_key_fwd key + let hash ← hashmap.hash_key_fwd key let i := vec_len (hashmap_list_t T) self.hashmap_hash_map_slots let hash_mod ← hash % i let l ← vec_index_mut_fwd (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod - hashmap_hash_map_get_mut_in_list_fwd T l key + hashmap.HashMap.get_mut_in_list_fwd T l key /- [hashmap_main::hashmap::HashMap::{0}::get_mut] -/ -def hashmap_hash_map_get_mut_back +def hashmap.HashMap.get_mut_back (T : Type) (self : hashmap_hash_map_t T) (key : Usize) (ret0 : T) : Result (hashmap_hash_map_t T) := do - let hash ← hashmap_hash_key_fwd key + let hash ← hashmap.hash_key_fwd key let i := vec_len (hashmap_list_t T) self.hashmap_hash_map_slots let hash_mod ← hash % i let l ← vec_index_mut_fwd (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod - let l0 ← hashmap_hash_map_get_mut_in_list_back T l key ret0 + let l0 ← hashmap.HashMap.get_mut_in_list_back T l key ret0 let v ← vec_index_mut_back (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod l0 Result.ret { self with hashmap_hash_map_slots := v } /- [hashmap_main::hashmap::HashMap::{0}::remove_from_list] -/ -divergent def hashmap_hash_map_remove_from_list_loop_fwd +divergent def hashmap.HashMap.remove_from_list_loop_fwd (T : Type) (key : Usize) (ls : hashmap_list_t T) : Result (Option T) := match ls with | hashmap_list_t.Cons ckey t tl => @@ -385,16 +384,16 @@ divergent def hashmap_hash_map_remove_from_list_loop_fwd match mv_ls with | hashmap_list_t.Cons i cvalue tl0 => Result.ret (Option.some cvalue) | hashmap_list_t.Nil => Result.fail Error.panic - else hashmap_hash_map_remove_from_list_loop_fwd T key tl + else hashmap.HashMap.remove_from_list_loop_fwd T key tl | hashmap_list_t.Nil => Result.ret Option.none /- [hashmap_main::hashmap::HashMap::{0}::remove_from_list] -/ -def hashmap_hash_map_remove_from_list_fwd +def hashmap.HashMap.remove_from_list_fwd (T : Type) (key : Usize) (ls : hashmap_list_t T) : Result (Option T) := - hashmap_hash_map_remove_from_list_loop_fwd T key ls + hashmap.HashMap.remove_from_list_loop_fwd T key ls /- [hashmap_main::hashmap::HashMap::{0}::remove_from_list] -/ -divergent def hashmap_hash_map_remove_from_list_loop_back +divergent def hashmap.HashMap.remove_from_list_loop_back (T : Type) (key : Usize) (ls : hashmap_list_t T) : Result (hashmap_list_t T) := @@ -410,27 +409,27 @@ divergent def hashmap_hash_map_remove_from_list_loop_back | hashmap_list_t.Nil => Result.fail Error.panic else do - let tl0 ← hashmap_hash_map_remove_from_list_loop_back T key tl + let tl0 ← hashmap.HashMap.remove_from_list_loop_back T key tl Result.ret (hashmap_list_t.Cons ckey t tl0) | hashmap_list_t.Nil => Result.ret hashmap_list_t.Nil /- [hashmap_main::hashmap::HashMap::{0}::remove_from_list] -/ -def hashmap_hash_map_remove_from_list_back +def hashmap.HashMap.remove_from_list_back (T : Type) (key : Usize) (ls : hashmap_list_t T) : Result (hashmap_list_t T) := - hashmap_hash_map_remove_from_list_loop_back T key ls + hashmap.HashMap.remove_from_list_loop_back T key ls /- [hashmap_main::hashmap::HashMap::{0}::remove] -/ -def hashmap_hash_map_remove_fwd +def hashmap.HashMap.remove_fwd (T : Type) (self : hashmap_hash_map_t T) (key : Usize) : Result (Option T) := do - let hash ← hashmap_hash_key_fwd key + let hash ← hashmap.hash_key_fwd key let i := vec_len (hashmap_list_t T) self.hashmap_hash_map_slots let hash_mod ← hash % i let l ← vec_index_mut_fwd (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod - let x ← hashmap_hash_map_remove_from_list_fwd T key l + let x ← hashmap.HashMap.remove_from_list_fwd T key l match x with | Option.none => Result.ret Option.none | Option.some x0 => @@ -440,21 +439,21 @@ def hashmap_hash_map_remove_fwd Result.ret (Option.some x0) /- [hashmap_main::hashmap::HashMap::{0}::remove] -/ -def hashmap_hash_map_remove_back +def hashmap.HashMap.remove_back (T : Type) (self : hashmap_hash_map_t T) (key : Usize) : Result (hashmap_hash_map_t T) := do - let hash ← hashmap_hash_key_fwd key + let hash ← hashmap.hash_key_fwd key let i := vec_len (hashmap_list_t T) self.hashmap_hash_map_slots let hash_mod ← hash % i let l ← vec_index_mut_fwd (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod - let x ← hashmap_hash_map_remove_from_list_fwd T key l + let x ← hashmap.HashMap.remove_from_list_fwd T key l match x with | Option.none => do - let l0 ← hashmap_hash_map_remove_from_list_back T key l + let l0 ← hashmap.HashMap.remove_from_list_back T key l let v ← vec_index_mut_back (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod l0 @@ -463,7 +462,7 @@ def hashmap_hash_map_remove_back do let i0 ← self.hashmap_hash_map_num_entries - (Usize.ofInt 1 (by intlit)) - let l0 ← hashmap_hash_map_remove_from_list_back T key l + let l0 ← hashmap.HashMap.remove_from_list_back T key l let v ← vec_index_mut_back (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod l0 @@ -475,38 +474,37 @@ def hashmap_hash_map_remove_back } /- [hashmap_main::hashmap::test1] -/ -def hashmap_test1_fwd : Result Unit := +def hashmap.test1_fwd : Result Unit := do - let hm ← hashmap_hash_map_new_fwd U64 + let hm ← hashmap.HashMap.new_fwd U64 let hm0 ← - hashmap_hash_map_insert_fwd_back U64 hm (Usize.ofInt 0 (by intlit)) + hashmap.HashMap.insert_fwd_back U64 hm (Usize.ofInt 0 (by intlit)) (U64.ofInt 42 (by intlit)) let hm1 ← - hashmap_hash_map_insert_fwd_back U64 hm0 (Usize.ofInt 128 (by intlit)) + hashmap.HashMap.insert_fwd_back U64 hm0 (Usize.ofInt 128 (by intlit)) (U64.ofInt 18 (by intlit)) let hm2 ← - hashmap_hash_map_insert_fwd_back U64 hm1 (Usize.ofInt 1024 (by intlit)) + hashmap.HashMap.insert_fwd_back U64 hm1 (Usize.ofInt 1024 (by intlit)) (U64.ofInt 138 (by intlit)) let hm3 ← - hashmap_hash_map_insert_fwd_back U64 hm2 (Usize.ofInt 1056 (by intlit)) + hashmap.HashMap.insert_fwd_back U64 hm2 (Usize.ofInt 1056 (by intlit)) (U64.ofInt 256 (by intlit)) - let i ← hashmap_hash_map_get_fwd U64 hm3 (Usize.ofInt 128 (by intlit)) + let i ← hashmap.HashMap.get_fwd U64 hm3 (Usize.ofInt 128 (by intlit)) if not (i = (U64.ofInt 18 (by intlit))) then Result.fail Error.panic else do let hm4 ← - hashmap_hash_map_get_mut_back U64 hm3 (Usize.ofInt 1024 (by intlit)) + hashmap.HashMap.get_mut_back U64 hm3 (Usize.ofInt 1024 (by intlit)) (U64.ofInt 56 (by intlit)) let i0 ← - hashmap_hash_map_get_fwd U64 hm4 (Usize.ofInt 1024 (by intlit)) + hashmap.HashMap.get_fwd U64 hm4 (Usize.ofInt 1024 (by intlit)) if not (i0 = (U64.ofInt 56 (by intlit))) then Result.fail Error.panic else do let x ← - hashmap_hash_map_remove_fwd U64 hm4 - (Usize.ofInt 1024 (by intlit)) + hashmap.HashMap.remove_fwd U64 hm4 (Usize.ofInt 1024 (by intlit)) match x with | Option.none => Result.fail Error.panic | Option.some x0 => @@ -515,39 +513,38 @@ def hashmap_test1_fwd : Result Unit := else do let hm5 ← - hashmap_hash_map_remove_back U64 hm4 + hashmap.HashMap.remove_back U64 hm4 (Usize.ofInt 1024 (by intlit)) let i1 ← - hashmap_hash_map_get_fwd U64 hm5 - (Usize.ofInt 0 (by intlit)) + hashmap.HashMap.get_fwd U64 hm5 (Usize.ofInt 0 (by intlit)) if not (i1 = (U64.ofInt 42 (by intlit))) then Result.fail Error.panic else do let i2 ← - hashmap_hash_map_get_fwd U64 hm5 + hashmap.HashMap.get_fwd U64 hm5 (Usize.ofInt 128 (by intlit)) if not (i2 = (U64.ofInt 18 (by intlit))) then Result.fail Error.panic else do let i3 ← - hashmap_hash_map_get_fwd U64 hm5 + hashmap.HashMap.get_fwd U64 hm5 (Usize.ofInt 1056 (by intlit)) if not (i3 = (U64.ofInt 256 (by intlit))) then Result.fail Error.panic else Result.ret () /- Unit test for [hashmap_main::hashmap::test1] -/ -#assert (hashmap_test1_fwd == .ret ()) +#assert (hashmap.test1_fwd == .ret ()) /- [hashmap_main::insert_on_disk] -/ def insert_on_disk_fwd (key : Usize) (value : U64) (st : State) : Result (State × Unit) := do - let (st0, hm) ← opaque_defs.hashmap_utils_deserialize_fwd st - let hm0 ← hashmap_hash_map_insert_fwd_back U64 hm key value - let (st1, _) ← opaque_defs.hashmap_utils_serialize_fwd hm0 st0 + let (st0, hm) ← hashmap_utils.deserialize_fwd st + let hm0 ← hashmap.HashMap.insert_fwd_back U64 hm key value + let (st1, _) ← hashmap_utils.serialize_fwd hm0 st0 Result.ret (st1, ()) /- [hashmap_main::main] -/ @@ -557,4 +554,4 @@ def main_fwd : Result Unit := /- Unit test for [hashmap_main::main] -/ #assert (main_fwd == .ret ()) -end HashmapMain +end hashmap_main diff --git a/tests/lean/HashmapMain/FunsExternal.lean b/tests/lean/HashmapMain/FunsExternal.lean new file mode 100644 index 00000000..d917b485 --- /dev/null +++ b/tests/lean/HashmapMain/FunsExternal.lean @@ -0,0 +1,17 @@ +-- [hashmap_main]: templates for the external functions. +import Base +import HashmapMain.Types +open Primitives +open hashmap_main + +-- TODO: fill those bodies + +/- [hashmap_main::hashmap_utils::deserialize] -/ +def hashmap_utils.deserialize_fwd + : State → Result (State × (hashmap_hash_map_t U64)) := + fun _ => .fail .panic + +/- [hashmap_main::hashmap_utils::serialize] -/ +def hashmap_utils.serialize_fwd + : hashmap_hash_map_t U64 → State → Result (State × Unit) := + fun _ _ => .fail .panic diff --git a/tests/lean/HashmapMain/FunsExternal_Template.lean b/tests/lean/HashmapMain/FunsExternal_Template.lean new file mode 100644 index 00000000..86286373 --- /dev/null +++ b/tests/lean/HashmapMain/FunsExternal_Template.lean @@ -0,0 +1,16 @@ +-- THIS FILE WAS AUTOMATICALLY GENERATED BY AENEAS +-- [hashmap_main]: external functions. +-- This is a template file: rename it to "FunsExternal.lean" and fill the holes. +import Base +import HashmapMain.Types +open Primitives +open hashmap_main + +/- [hashmap_main::hashmap_utils::deserialize] -/ +axiom hashmap_utils.deserialize_fwd + : State → Result (State × (hashmap_hash_map_t U64)) + +/- [hashmap_main::hashmap_utils::serialize] -/ +axiom hashmap_utils.serialize_fwd + : hashmap_hash_map_t U64 → State → Result (State × Unit) + diff --git a/tests/lean/HashmapMain/Opaque.lean b/tests/lean/HashmapMain/Opaque.lean index 10e4d8bd..abf04c94 100644 --- a/tests/lean/HashmapMain/Opaque.lean +++ b/tests/lean/HashmapMain/Opaque.lean @@ -4,16 +4,16 @@ import Base import HashmapMain.Types open Primitives -namespace HashmapMain +namespace hashmap_main structure OpaqueDefs where /- [hashmap_main::hashmap_utils::deserialize] -/ - hashmap_utils_deserialize_fwd + hashmap_utils.deserialize_fwd : State → Result (State × (hashmap_hash_map_t U64)) /- [hashmap_main::hashmap_utils::serialize] -/ - hashmap_utils_serialize_fwd + hashmap_utils.serialize_fwd : hashmap_hash_map_t U64 → State → Result (State × Unit) -end HashmapMain +end hashmap_main diff --git a/tests/lean/HashmapMain/Types.lean b/tests/lean/HashmapMain/Types.lean index b91ff3a7..16641146 100644 --- a/tests/lean/HashmapMain/Types.lean +++ b/tests/lean/HashmapMain/Types.lean @@ -2,8 +2,7 @@ -- [hashmap_main]: type definitions import Base open Primitives - -namespace HashmapMain +namespace hashmap_main /- [hashmap_main::hashmap::List] -/ inductive hashmap_list_t (T : Type) := @@ -20,4 +19,4 @@ structure hashmap_hash_map_t (T : Type) where /- The state type used in the state-error monad -/ axiom State : Type -end HashmapMain +end hashmap_main diff --git a/tests/lean/Loops/Funs.lean b/tests/lean/Loops/Funs.lean index 9e084327..694f5450 100644 --- a/tests/lean/Loops/Funs.lean +++ b/tests/lean/Loops/Funs.lean @@ -3,8 +3,7 @@ import Base import Loops.Types open Primitives - -namespace Loops +namespace loops /- [loops::sum] -/ divergent def sum_loop_fwd (max : U32) (i : U32) (s : U32) : Result U32 := @@ -624,4 +623,4 @@ def list_nth_shared_mut_loop_pair_merge_back := list_nth_shared_mut_loop_pair_merge_loop_back T ls0 ls1 i ret0 -end Loops +end loops diff --git a/tests/lean/Loops/Types.lean b/tests/lean/Loops/Types.lean index ca0403e9..5b5ed31f 100644 --- a/tests/lean/Loops/Types.lean +++ b/tests/lean/Loops/Types.lean @@ -2,12 +2,11 @@ -- [loops]: type definitions import Base open Primitives - -namespace Loops +namespace loops /- [loops::List] -/ inductive list_t (T : Type) := | Cons : T → list_t T → list_t T | Nil : list_t T -end Loops +end loops diff --git a/tests/lean/NoNestedBorrows.lean b/tests/lean/NoNestedBorrows.lean index 769bb311..67abc8f6 100644 --- a/tests/lean/NoNestedBorrows.lean +++ b/tests/lean/NoNestedBorrows.lean @@ -2,8 +2,7 @@ -- [no_nested_borrows] import Base open Primitives - -namespace NoNestedBorrows +namespace no_nested_borrows /- [no_nested_borrows::Pair] -/ structure pair_t (T1 T2 : Type) where @@ -541,4 +540,4 @@ def test_shared_borrow_enum1_fwd (l : list_t U32) : Result U32 := def test_shared_borrow_enum2_fwd : Result U32 := Result.ret (U32.ofInt 0 (by intlit)) -end NoNestedBorrows +end no_nested_borrows diff --git a/tests/lean/Paper.lean b/tests/lean/Paper.lean index edcb5c1b..c34941ef 100644 --- a/tests/lean/Paper.lean +++ b/tests/lean/Paper.lean @@ -2,8 +2,7 @@ -- [paper] import Base open Primitives - -namespace Paper +namespace paper /- [paper::ref_incr] -/ def ref_incr_fwd_back (x : I32) : Result I32 := @@ -125,4 +124,4 @@ def call_choose_fwd (p : (U32 × U32)) : Result U32 := let (px0, _) ← choose_back U32 true px py pz0 Result.ret px0 -end Paper +end paper diff --git a/tests/lean/PoloniusList.lean b/tests/lean/PoloniusList.lean index 0f2a05e3..0ff01b47 100644 --- a/tests/lean/PoloniusList.lean +++ b/tests/lean/PoloniusList.lean @@ -2,8 +2,7 @@ -- [polonius_list] import Base open Primitives - -namespace PoloniusList +namespace polonius_list /- [polonius_list::List] -/ inductive list_t (T : Type) := @@ -33,4 +32,4 @@ divergent def get_list_at_x_back Result.ret (list_t.Cons hd tl0) | list_t.Nil => Result.ret ret0 -end PoloniusList +end polonius_list diff --git a/tests/lean/Tests.lean b/tests/lean/Tests.lean deleted file mode 100644 index 9b12270e..00000000 --- a/tests/lean/Tests.lean +++ /dev/null @@ -1,9 +0,0 @@ -import BetreeMain -import Constants -import External -import Hashmap -import HashmapMain -import Loops -import NoNestedBorrows -import Paper -import PoloniusList diff --git a/tests/lean/lakefile.lean b/tests/lean/lakefile.lean index 217e533f..ae63b129 100644 --- a/tests/lean/lakefile.lean +++ b/tests/lean/lakefile.lean @@ -8,15 +8,12 @@ require Base from "../../backends/lean" package «tests» {} -@[default_target] -lean_lib «Tests» {} - -lean_lib betreeMain -lean_lib constants -lean_lib external -lean_lib hashmap -lean_lib hashmapMain -lean_lib loops -lean_lib noNestedBorrows -lean_lib paper -lean_lib poloniusList +@[default_target] lean_lib betreeMain +@[default_target] lean_lib constants +@[default_target] lean_lib external +@[default_target] lean_lib hashmap +@[default_target] lean_lib hashmapMain +@[default_target] lean_lib loops +@[default_target] lean_lib noNestedBorrows +@[default_target] lean_lib paper +@[default_target] lean_lib poloniusList -- cgit v1.2.3 From c07721dedb2cfe4c726c42606e623395cdfe5b80 Mon Sep 17 00:00:00 2001 From: Son Ho Date: Wed, 5 Jul 2023 15:09:07 +0200 Subject: Simplify the generated names for the types in Lean --- tests/lean/BetreeMain/Funs.lean | 614 +++++++++++----------- tests/lean/BetreeMain/FunsExternal.lean | 8 +- tests/lean/BetreeMain/FunsExternal_Template.lean | 11 +- tests/lean/BetreeMain/Types.lean | 44 +- tests/lean/Constants.lean | 28 +- tests/lean/External/Funs.lean | 4 +- tests/lean/External/FunsExternal_Template.lean | 3 +- tests/lean/External/Types.lean | 2 +- tests/lean/Hashmap/Funs.lean | 203 ++++--- tests/lean/Hashmap/Types.lean | 10 +- tests/lean/HashmapMain/Funs.lean | 232 ++++---- tests/lean/HashmapMain/FunsExternal.lean | 4 +- tests/lean/HashmapMain/FunsExternal_Template.lean | 4 +- tests/lean/HashmapMain/Types.lean | 10 +- tests/lean/Loops/Funs.lean | 355 ++++++------- tests/lean/Loops/Types.lean | 6 +- tests/lean/NoNestedBorrows.lean | 143 +++-- tests/lean/Paper.lean | 43 +- tests/lean/PoloniusList.lean | 22 +- 19 files changed, 857 insertions(+), 889 deletions(-) (limited to 'tests/lean') diff --git a/tests/lean/BetreeMain/Funs.lean b/tests/lean/BetreeMain/Funs.lean index 78e14146..3a678c71 100644 --- a/tests/lean/BetreeMain/Funs.lean +++ b/tests/lean/BetreeMain/Funs.lean @@ -9,13 +9,13 @@ namespace betree_main /- [betree_main::betree::load_internal_node] -/ def betree.load_internal_node_fwd (id : U64) (st : State) : - Result (State × (betree_list_t (U64 × betree_message_t))) + Result (State × (betree.List (U64 × betree.Message))) := betree_utils.load_internal_node_fwd id st /- [betree_main::betree::store_internal_node] -/ def betree.store_internal_node_fwd - (id : U64) (content : betree_list_t (U64 × betree_message_t)) (st : State) : + (id : U64) (content : betree.List (U64 × betree.Message)) (st : State) : Result (State × Unit) := do @@ -24,12 +24,12 @@ def betree.store_internal_node_fwd /- [betree_main::betree::load_leaf_node] -/ def betree.load_leaf_node_fwd - (id : U64) (st : State) : Result (State × (betree_list_t (U64 × U64))) := + (id : U64) (st : State) : Result (State × (betree.List (U64 × U64))) := betree_utils.load_leaf_node_fwd id st /- [betree_main::betree::store_leaf_node] -/ def betree.store_leaf_node_fwd - (id : U64) (content : betree_list_t (U64 × U64)) (st : State) : + (id : U64) (content : betree.List (U64 × U64)) (st : State) : Result (State × Unit) := do @@ -47,13 +47,13 @@ def betree.fresh_node_id_back (counter : U64) : Result U64 := counter + (U64.ofInt 1 (by intlit)) /- [betree_main::betree::NodeIdCounter::{0}::new] -/ -def betree.NodeIdCounter.new_fwd : Result betree_node_id_counter_t := +def betree.NodeIdCounter.new_fwd : Result betree.NodeIdCounter := Result.ret { betree_node_id_counter_next_node_id := (U64.ofInt 0 (by intlit)) } /- [betree_main::betree::NodeIdCounter::{0}::fresh_id] -/ def betree.NodeIdCounter.fresh_id_fwd - (self : betree_node_id_counter_t) : Result U64 := + (self : betree.NodeIdCounter) : Result U64 := do let _ ← self.betree_node_id_counter_next_node_id + (U64.ofInt 1 (by intlit)) @@ -61,7 +61,7 @@ def betree.NodeIdCounter.fresh_id_fwd /- [betree_main::betree::NodeIdCounter::{0}::fresh_id] -/ def betree.NodeIdCounter.fresh_id_back - (self : betree_node_id_counter_t) : Result betree_node_id_counter_t := + (self : betree.NodeIdCounter) : Result betree.NodeIdCounter := do let i ← self.betree_node_id_counter_next_node_id + (U64.ofInt 1 (by intlit)) @@ -74,113 +74,112 @@ def core_num_u64_max_c : U64 := eval_global core_num_u64_max_body (by simp) /- [betree_main::betree::upsert_update] -/ def betree.upsert_update_fwd - (prev : Option U64) (st : betree_upsert_fun_state_t) : Result U64 := + (prev : Option U64) (st : betree.UpsertFunState) : Result U64 := match prev with | Option.none => match st with - | betree_upsert_fun_state_t.Add v => Result.ret v - | betree_upsert_fun_state_t.Sub i => Result.ret (U64.ofInt 0 (by intlit)) + | betree.UpsertFunState.Add v => Result.ret v + | betree.UpsertFunState.Sub i => Result.ret (U64.ofInt 0 (by intlit)) | Option.some prev0 => match st with - | betree_upsert_fun_state_t.Add v => + | betree.UpsertFunState.Add v => do let margin ← core_num_u64_max_c - prev0 if margin >= v then prev0 + v else Result.ret core_num_u64_max_c - | betree_upsert_fun_state_t.Sub v => + | betree.UpsertFunState.Sub v => if prev0 >= v then prev0 - v else Result.ret (U64.ofInt 0 (by intlit)) /- [betree_main::betree::List::{1}::len] -/ divergent def betree.List.len_fwd - (T : Type) (self : betree_list_t T) : Result U64 := + (T : Type) (self : betree.List T) : Result U64 := match self with - | betree_list_t.Cons t tl => + | betree.List.Cons t tl => do let i ← betree.List.len_fwd T tl (U64.ofInt 1 (by intlit)) + i - | betree_list_t.Nil => Result.ret (U64.ofInt 0 (by intlit)) + | betree.List.Nil => Result.ret (U64.ofInt 0 (by intlit)) /- [betree_main::betree::List::{1}::split_at] -/ divergent def betree.List.split_at_fwd - (T : Type) (self : betree_list_t T) (n : U64) : - Result ((betree_list_t T) × (betree_list_t T)) + (T : Type) (self : betree.List T) (n : U64) : + Result ((betree.List T) × (betree.List T)) := if n = (U64.ofInt 0 (by intlit)) - then Result.ret (betree_list_t.Nil, self) + then Result.ret (betree.List.Nil, self) else match self with - | betree_list_t.Cons hd tl => + | betree.List.Cons hd tl => do let i ← n - (U64.ofInt 1 (by intlit)) let p ← betree.List.split_at_fwd T tl i let (ls0, ls1) := p let l := ls0 - Result.ret (betree_list_t.Cons hd l, ls1) - | betree_list_t.Nil => Result.fail Error.panic + Result.ret (betree.List.Cons hd l, ls1) + | betree.List.Nil => Result.fail Error.panic /- [betree_main::betree::List::{1}::push_front] -/ def betree.List.push_front_fwd_back - (T : Type) (self : betree_list_t T) (x : T) : Result (betree_list_t T) := - let tl := mem_replace_fwd (betree_list_t T) self betree_list_t.Nil + (T : Type) (self : betree.List T) (x : T) : Result (betree.List T) := + let tl := mem_replace_fwd (betree.List T) self betree.List.Nil let l := tl - Result.ret (betree_list_t.Cons x l) + Result.ret (betree.List.Cons x l) /- [betree_main::betree::List::{1}::pop_front] -/ -def betree.List.pop_front_fwd (T : Type) (self : betree_list_t T) : Result T := - let ls := mem_replace_fwd (betree_list_t T) self betree_list_t.Nil +def betree.List.pop_front_fwd (T : Type) (self : betree.List T) : Result T := + let ls := mem_replace_fwd (betree.List T) self betree.List.Nil match ls with - | betree_list_t.Cons x tl => Result.ret x - | betree_list_t.Nil => Result.fail Error.panic + | betree.List.Cons x tl => Result.ret x + | betree.List.Nil => Result.fail Error.panic /- [betree_main::betree::List::{1}::pop_front] -/ def betree.List.pop_front_back - (T : Type) (self : betree_list_t T) : Result (betree_list_t T) := - let ls := mem_replace_fwd (betree_list_t T) self betree_list_t.Nil + (T : Type) (self : betree.List T) : Result (betree.List T) := + let ls := mem_replace_fwd (betree.List T) self betree.List.Nil match ls with - | betree_list_t.Cons x tl => Result.ret tl - | betree_list_t.Nil => Result.fail Error.panic + | betree.List.Cons x tl => Result.ret tl + | betree.List.Nil => Result.fail Error.panic /- [betree_main::betree::List::{1}::hd] -/ -def betree.List.hd_fwd (T : Type) (self : betree_list_t T) : Result T := +def betree.List.hd_fwd (T : Type) (self : betree.List T) : Result T := match self with - | betree_list_t.Cons hd l => Result.ret hd - | betree_list_t.Nil => Result.fail Error.panic + | betree.List.Cons hd l => Result.ret hd + | betree.List.Nil => Result.fail Error.panic /- [betree_main::betree::List::{2}::head_has_key] -/ def betree.List.head_has_key_fwd - (T : Type) (self : betree_list_t (U64 × T)) (key : U64) : Result Bool := + (T : Type) (self : betree.List (U64 × T)) (key : U64) : Result Bool := match self with - | betree_list_t.Cons hd l => let (i, _) := hd - Result.ret (i = key) - | betree_list_t.Nil => Result.ret false + | betree.List.Cons hd l => let (i, _) := hd + Result.ret (i = key) + | betree.List.Nil => Result.ret false /- [betree_main::betree::List::{2}::partition_at_pivot] -/ divergent def betree.List.partition_at_pivot_fwd - (T : Type) (self : betree_list_t (U64 × T)) (pivot : U64) : - Result ((betree_list_t (U64 × T)) × (betree_list_t (U64 × T))) + (T : Type) (self : betree.List (U64 × T)) (pivot : U64) : + Result ((betree.List (U64 × T)) × (betree.List (U64 × T))) := match self with - | betree_list_t.Cons hd tl => + | betree.List.Cons hd tl => let (i, t) := hd if i >= pivot - then Result.ret (betree_list_t.Nil, betree_list_t.Cons (i, t) tl) + then Result.ret (betree.List.Nil, betree.List.Cons (i, t) tl) else do let p ← betree.List.partition_at_pivot_fwd T tl pivot let (ls0, ls1) := p let l := ls0 - Result.ret (betree_list_t.Cons (i, t) l, ls1) - | betree_list_t.Nil => Result.ret (betree_list_t.Nil, betree_list_t.Nil) + Result.ret (betree.List.Cons (i, t) l, ls1) + | betree.List.Nil => Result.ret (betree.List.Nil, betree.List.Nil) /- [betree_main::betree::Leaf::{3}::split] -/ def betree.Leaf.split_fwd - (self : betree_leaf_t) (content : betree_list_t (U64 × U64)) - (params : betree_params_t) (node_id_cnt : betree_node_id_counter_t) - (st : State) : - Result (State × betree_internal_t) + (self : betree.Leaf) (content : betree.List (U64 × U64)) + (params : betree.Params) (node_id_cnt : betree.NodeIdCounter) (st : State) : + Result (State × betree.Internal) := do let p ← @@ -194,24 +193,23 @@ def betree.Leaf.split_fwd let id1 ← betree.NodeIdCounter.fresh_id_fwd node_id_cnt0 let (st0, _) ← betree.store_leaf_node_fwd id0 content0 st let (st1, _) ← betree.store_leaf_node_fwd id1 content1 st0 - let n := betree_node_t.Leaf + let n := betree.Node.Leaf { betree_leaf_id := id0, betree_leaf_size := params.betree_params_split_size } - let n0 := betree_node_t.Leaf + let n0 := betree.Node.Leaf { betree_leaf_id := id1, betree_leaf_size := params.betree_params_split_size } - Result.ret (st1, betree_internal_t.mk self.betree_leaf_id pivot n n0) + Result.ret (st1, betree.Internal.mk self.betree_leaf_id pivot n n0) /- [betree_main::betree::Leaf::{3}::split] -/ def betree.Leaf.split_back - (self : betree_leaf_t) (content : betree_list_t (U64 × U64)) - (params : betree_params_t) (node_id_cnt : betree_node_id_counter_t) - (st : State) : - Result betree_node_id_counter_t + (self : betree.Leaf) (content : betree.List (U64 × U64)) + (params : betree.Params) (node_id_cnt : betree.NodeIdCounter) (st : State) : + Result betree.NodeIdCounter := do let p ← @@ -228,9 +226,9 @@ def betree.Leaf.split_back /- [betree_main::betree::Node::{5}::lookup_in_bindings] -/ divergent def betree.Node.lookup_in_bindings_fwd - (key : U64) (bindings : betree_list_t (U64 × U64)) : Result (Option U64) := + (key : U64) (bindings : betree.List (U64 × U64)) : Result (Option U64) := match bindings with - | betree_list_t.Cons hd tl => + | betree.List.Cons hd tl => let (i, i0) := hd if i = key then Result.ret (Option.some i0) @@ -238,29 +236,29 @@ divergent def betree.Node.lookup_in_bindings_fwd if i > key then Result.ret Option.none else betree.Node.lookup_in_bindings_fwd key tl - | betree_list_t.Nil => Result.ret Option.none + | betree.List.Nil => Result.ret Option.none /- [betree_main::betree::Node::{5}::lookup_first_message_for_key] -/ divergent def betree.Node.lookup_first_message_for_key_fwd - (key : U64) (msgs : betree_list_t (U64 × betree_message_t)) : - Result (betree_list_t (U64 × betree_message_t)) + (key : U64) (msgs : betree.List (U64 × betree.Message)) : + Result (betree.List (U64 × betree.Message)) := match msgs with - | betree_list_t.Cons x next_msgs => + | betree.List.Cons x next_msgs => let (i, m) := x if i >= key - then Result.ret (betree_list_t.Cons (i, m) next_msgs) + then Result.ret (betree.List.Cons (i, m) next_msgs) else betree.Node.lookup_first_message_for_key_fwd key next_msgs - | betree_list_t.Nil => Result.ret betree_list_t.Nil + | betree.List.Nil => Result.ret betree.List.Nil /- [betree_main::betree::Node::{5}::lookup_first_message_for_key] -/ divergent def betree.Node.lookup_first_message_for_key_back - (key : U64) (msgs : betree_list_t (U64 × betree_message_t)) - (ret0 : betree_list_t (U64 × betree_message_t)) : - Result (betree_list_t (U64 × betree_message_t)) + (key : U64) (msgs : betree.List (U64 × betree.Message)) + (ret0 : betree.List (U64 × betree.Message)) : + Result (betree.List (U64 × betree.Message)) := match msgs with - | betree_list_t.Cons x next_msgs => + | betree.List.Cons x next_msgs => let (i, m) := x if i >= key then Result.ret ret0 @@ -268,134 +266,134 @@ divergent def betree.Node.lookup_first_message_for_key_back do let next_msgs0 ← betree.Node.lookup_first_message_for_key_back key next_msgs ret0 - Result.ret (betree_list_t.Cons (i, m) next_msgs0) - | betree_list_t.Nil => Result.ret ret0 + Result.ret (betree.List.Cons (i, m) next_msgs0) + | betree.List.Nil => Result.ret ret0 /- [betree_main::betree::Node::{5}::apply_upserts] -/ divergent def betree.Node.apply_upserts_fwd - (msgs : betree_list_t (U64 × betree_message_t)) (prev : Option U64) - (key : U64) (st : State) : + (msgs : betree.List (U64 × betree.Message)) (prev : Option U64) (key : U64) + (st : State) : Result (State × U64) := do - let b ← betree.List.head_has_key_fwd betree_message_t msgs key + let b ← betree.List.head_has_key_fwd betree.Message msgs key if b then do - let msg ← betree.List.pop_front_fwd (U64 × betree_message_t) msgs + let msg ← betree.List.pop_front_fwd (U64 × betree.Message) msgs let (_, m) := msg match m with - | betree_message_t.Insert i => Result.fail Error.panic - | betree_message_t.Delete => Result.fail Error.panic - | betree_message_t.Upsert s => + | betree.Message.Insert i => Result.fail Error.panic + | betree.Message.Delete => Result.fail Error.panic + | betree.Message.Upsert s => do let v ← betree.upsert_update_fwd prev s let msgs0 ← - betree.List.pop_front_back (U64 × betree_message_t) msgs + betree.List.pop_front_back (U64 × betree.Message) msgs betree.Node.apply_upserts_fwd msgs0 (Option.some v) key st else do let (st0, v) ← core.option.Option.unwrap_fwd U64 prev st let _ ← - betree.List.push_front_fwd_back (U64 × betree_message_t) msgs (key, - betree_message_t.Insert v) + betree.List.push_front_fwd_back (U64 × betree.Message) msgs (key, + betree.Message.Insert v) Result.ret (st0, v) /- [betree_main::betree::Node::{5}::apply_upserts] -/ divergent def betree.Node.apply_upserts_back - (msgs : betree_list_t (U64 × betree_message_t)) (prev : Option U64) - (key : U64) (st : State) : - Result (betree_list_t (U64 × betree_message_t)) + (msgs : betree.List (U64 × betree.Message)) (prev : Option U64) (key : U64) + (st : State) : + Result (betree.List (U64 × betree.Message)) := do - let b ← betree.List.head_has_key_fwd betree_message_t msgs key + let b ← betree.List.head_has_key_fwd betree.Message msgs key if b then do - let msg ← betree.List.pop_front_fwd (U64 × betree_message_t) msgs + let msg ← betree.List.pop_front_fwd (U64 × betree.Message) msgs let (_, m) := msg match m with - | betree_message_t.Insert i => Result.fail Error.panic - | betree_message_t.Delete => Result.fail Error.panic - | betree_message_t.Upsert s => + | betree.Message.Insert i => Result.fail Error.panic + | betree.Message.Delete => Result.fail Error.panic + | betree.Message.Upsert s => do let v ← betree.upsert_update_fwd prev s let msgs0 ← - betree.List.pop_front_back (U64 × betree_message_t) msgs + betree.List.pop_front_back (U64 × betree.Message) msgs betree.Node.apply_upserts_back msgs0 (Option.some v) key st else do let (_, v) ← core.option.Option.unwrap_fwd U64 prev st - betree.List.push_front_fwd_back (U64 × betree_message_t) msgs (key, - betree_message_t.Insert v) + betree.List.push_front_fwd_back (U64 × betree.Message) msgs (key, + betree.Message.Insert v) /- [betree_main::betree::Node::{5}::lookup] -/ mutual divergent def betree.Node.lookup_fwd - (self : betree_node_t) (key : U64) (st : State) : + (self : betree.Node) (key : U64) (st : State) : Result (State × (Option U64)) := match self with - | betree_node_t.Internal node => + | betree.Node.Internal node => do let ⟨ i, i0, n, n0 ⟩ := node let (st0, msgs) ← betree.load_internal_node_fwd i st let pending ← betree.Node.lookup_first_message_for_key_fwd key msgs match pending with - | betree_list_t.Cons p l => + | betree.List.Cons p l => let (k, msg) := p if k != key then do let (st1, opt) ← - betree.Internal.lookup_in_children_fwd (betree_internal_t.mk i i0 - n n0) key st0 + betree.Internal.lookup_in_children_fwd (betree.Internal.mk i i0 n + n0) key st0 let _ ← betree.Node.lookup_first_message_for_key_back key msgs - (betree_list_t.Cons (k, msg) l) + (betree.List.Cons (k, msg) l) Result.ret (st1, opt) else match msg with - | betree_message_t.Insert v => + | betree.Message.Insert v => do let _ ← betree.Node.lookup_first_message_for_key_back key msgs - (betree_list_t.Cons (k, betree_message_t.Insert v) l) + (betree.List.Cons (k, betree.Message.Insert v) l) Result.ret (st0, Option.some v) - | betree_message_t.Delete => + | betree.Message.Delete => do let _ ← betree.Node.lookup_first_message_for_key_back key msgs - (betree_list_t.Cons (k, betree_message_t.Delete) l) + (betree.List.Cons (k, betree.Message.Delete) l) Result.ret (st0, Option.none) - | betree_message_t.Upsert ufs => + | betree.Message.Upsert ufs => do let (st1, v) ← - betree.Internal.lookup_in_children_fwd (betree_internal_t.mk i - i0 n n0) key st0 + betree.Internal.lookup_in_children_fwd (betree.Internal.mk i i0 + n n0) key st0 let (st2, v0) ← - betree.Node.apply_upserts_fwd (betree_list_t.Cons (k, - betree_message_t.Upsert ufs) l) v key st1 + betree.Node.apply_upserts_fwd (betree.List.Cons (k, + betree.Message.Upsert ufs) l) v key st1 let node0 ← - betree.Internal.lookup_in_children_back (betree_internal_t.mk i + betree.Internal.lookup_in_children_back (betree.Internal.mk i i0 n n0) key st0 let ⟨ i1, _, _, _ ⟩ := node0 let pending0 ← - betree.Node.apply_upserts_back (betree_list_t.Cons (k, - betree_message_t.Upsert ufs) l) v key st1 + betree.Node.apply_upserts_back (betree.List.Cons (k, + betree.Message.Upsert ufs) l) v key st1 let msgs0 ← betree.Node.lookup_first_message_for_key_back key msgs pending0 let (st3, _) ← betree.store_internal_node_fwd i1 msgs0 st2 Result.ret (st3, Option.some v0) - | betree_list_t.Nil => + | betree.List.Nil => do let (st1, opt) ← - betree.Internal.lookup_in_children_fwd (betree_internal_t.mk i i0 n + betree.Internal.lookup_in_children_fwd (betree.Internal.mk i i0 n n0) key st0 let _ ← betree.Node.lookup_first_message_for_key_back key msgs - betree_list_t.Nil + betree.List.Nil Result.ret (st1, opt) - | betree_node_t.Leaf node => + | betree.Node.Leaf node => do let (st0, bindings) ← betree.load_leaf_node_fwd node.betree_leaf_id st let opt ← betree.Node.lookup_in_bindings_fwd key bindings @@ -403,80 +401,78 @@ mutual divergent def betree.Node.lookup_fwd /- [betree_main::betree::Node::{5}::lookup] -/ divergent def betree.Node.lookup_back - (self : betree_node_t) (key : U64) (st : State) : Result betree_node_t := + (self : betree.Node) (key : U64) (st : State) : Result betree.Node := match self with - | betree_node_t.Internal node => + | betree.Node.Internal node => do let ⟨ i, i0, n, n0 ⟩ := node let (st0, msgs) ← betree.load_internal_node_fwd i st let pending ← betree.Node.lookup_first_message_for_key_fwd key msgs match pending with - | betree_list_t.Cons p l => + | betree.List.Cons p l => let (k, msg) := p if k != key then do let _ ← betree.Node.lookup_first_message_for_key_back key msgs - (betree_list_t.Cons (k, msg) l) + (betree.List.Cons (k, msg) l) let node0 ← - betree.Internal.lookup_in_children_back (betree_internal_t.mk i - i0 n n0) key st0 - Result.ret (betree_node_t.Internal node0) + betree.Internal.lookup_in_children_back (betree.Internal.mk i i0 + n n0) key st0 + Result.ret (betree.Node.Internal node0) else match msg with - | betree_message_t.Insert v => + | betree.Message.Insert v => do let _ ← betree.Node.lookup_first_message_for_key_back key msgs - (betree_list_t.Cons (k, betree_message_t.Insert v) l) - Result.ret (betree_node_t.Internal (betree_internal_t.mk i i0 n - n0)) - | betree_message_t.Delete => + (betree.List.Cons (k, betree.Message.Insert v) l) + Result.ret (betree.Node.Internal (betree.Internal.mk i i0 n n0)) + | betree.Message.Delete => do let _ ← betree.Node.lookup_first_message_for_key_back key msgs - (betree_list_t.Cons (k, betree_message_t.Delete) l) - Result.ret (betree_node_t.Internal (betree_internal_t.mk i i0 n - n0)) - | betree_message_t.Upsert ufs => + (betree.List.Cons (k, betree.Message.Delete) l) + Result.ret (betree.Node.Internal (betree.Internal.mk i i0 n n0)) + | betree.Message.Upsert ufs => do let (st1, v) ← - betree.Internal.lookup_in_children_fwd (betree_internal_t.mk i - i0 n n0) key st0 + betree.Internal.lookup_in_children_fwd (betree.Internal.mk i i0 + n n0) key st0 let (st2, _) ← - betree.Node.apply_upserts_fwd (betree_list_t.Cons (k, - betree_message_t.Upsert ufs) l) v key st1 + betree.Node.apply_upserts_fwd (betree.List.Cons (k, + betree.Message.Upsert ufs) l) v key st1 let node0 ← - betree.Internal.lookup_in_children_back (betree_internal_t.mk i + betree.Internal.lookup_in_children_back (betree.Internal.mk i i0 n n0) key st0 let ⟨ i1, i2, n1, n2 ⟩ := node0 let pending0 ← - betree.Node.apply_upserts_back (betree_list_t.Cons (k, - betree_message_t.Upsert ufs) l) v key st1 + betree.Node.apply_upserts_back (betree.List.Cons (k, + betree.Message.Upsert ufs) l) v key st1 let msgs0 ← betree.Node.lookup_first_message_for_key_back key msgs pending0 let _ ← betree.store_internal_node_fwd i1 msgs0 st2 - Result.ret (betree_node_t.Internal (betree_internal_t.mk i1 i2 n1 + Result.ret (betree.Node.Internal (betree.Internal.mk i1 i2 n1 n2)) - | betree_list_t.Nil => + | betree.List.Nil => do let _ ← betree.Node.lookup_first_message_for_key_back key msgs - betree_list_t.Nil + betree.List.Nil let node0 ← - betree.Internal.lookup_in_children_back (betree_internal_t.mk i i0 - n n0) key st0 - Result.ret (betree_node_t.Internal node0) - | betree_node_t.Leaf node => + betree.Internal.lookup_in_children_back (betree.Internal.mk i i0 n + n0) key st0 + Result.ret (betree.Node.Internal node0) + | betree.Node.Leaf node => do let (_, bindings) ← betree.load_leaf_node_fwd node.betree_leaf_id st let _ ← betree.Node.lookup_in_bindings_fwd key bindings - Result.ret (betree_node_t.Leaf node) + Result.ret (betree.Node.Leaf node) /- [betree_main::betree::Internal::{4}::lookup_in_children] -/ divergent def betree.Internal.lookup_in_children_fwd - (self : betree_internal_t) (key : U64) (st : State) : + (self : betree.Internal) (key : U64) (st : State) : Result (State × (Option U64)) := let ⟨ _, i, n, n0 ⟩ := self @@ -486,57 +482,55 @@ divergent def betree.Internal.lookup_in_children_fwd /- [betree_main::betree::Internal::{4}::lookup_in_children] -/ divergent def betree.Internal.lookup_in_children_back - (self : betree_internal_t) (key : U64) (st : State) : - Result betree_internal_t - := + (self : betree.Internal) (key : U64) (st : State) : Result betree.Internal := let ⟨ i, i0, n, n0 ⟩ := self if key < i0 then do let n1 ← betree.Node.lookup_back n key st - Result.ret (betree_internal_t.mk i i0 n1 n0) + Result.ret (betree.Internal.mk i i0 n1 n0) else do let n1 ← betree.Node.lookup_back n0 key st - Result.ret (betree_internal_t.mk i i0 n n1) + Result.ret (betree.Internal.mk i i0 n n1) end /- [betree_main::betree::Node::{5}::lookup_mut_in_bindings] -/ divergent def betree.Node.lookup_mut_in_bindings_fwd - (key : U64) (bindings : betree_list_t (U64 × U64)) : - Result (betree_list_t (U64 × U64)) + (key : U64) (bindings : betree.List (U64 × U64)) : + Result (betree.List (U64 × U64)) := match bindings with - | betree_list_t.Cons hd tl => + | betree.List.Cons hd tl => let (i, i0) := hd if i >= key - then Result.ret (betree_list_t.Cons (i, i0) tl) + then Result.ret (betree.List.Cons (i, i0) tl) else betree.Node.lookup_mut_in_bindings_fwd key tl - | betree_list_t.Nil => Result.ret betree_list_t.Nil + | betree.List.Nil => Result.ret betree.List.Nil /- [betree_main::betree::Node::{5}::lookup_mut_in_bindings] -/ divergent def betree.Node.lookup_mut_in_bindings_back - (key : U64) (bindings : betree_list_t (U64 × U64)) - (ret0 : betree_list_t (U64 × U64)) : - Result (betree_list_t (U64 × U64)) + (key : U64) (bindings : betree.List (U64 × U64)) + (ret0 : betree.List (U64 × U64)) : + Result (betree.List (U64 × U64)) := match bindings with - | betree_list_t.Cons hd tl => + | betree.List.Cons hd tl => let (i, i0) := hd if i >= key then Result.ret ret0 else do let tl0 ← betree.Node.lookup_mut_in_bindings_back key tl ret0 - Result.ret (betree_list_t.Cons (i, i0) tl0) - | betree_list_t.Nil => Result.ret ret0 + Result.ret (betree.List.Cons (i, i0) tl0) + | betree.List.Nil => Result.ret ret0 /- [betree_main::betree::Node::{5}::apply_to_leaf] -/ def betree.Node.apply_to_leaf_fwd_back - (bindings : betree_list_t (U64 × U64)) (key : U64) - (new_msg : betree_message_t) : - Result (betree_list_t (U64 × U64)) + (bindings : betree.List (U64 × U64)) (key : U64) (new_msg : betree.Message) + : + Result (betree.List (U64 × U64)) := do let bindings0 ← betree.Node.lookup_mut_in_bindings_fwd key bindings @@ -546,17 +540,17 @@ def betree.Node.apply_to_leaf_fwd_back do let hd ← betree.List.pop_front_fwd (U64 × U64) bindings0 match new_msg with - | betree_message_t.Insert v => + | betree.Message.Insert v => do let bindings1 ← betree.List.pop_front_back (U64 × U64) bindings0 let bindings2 ← betree.List.push_front_fwd_back (U64 × U64) bindings1 (key, v) betree.Node.lookup_mut_in_bindings_back key bindings bindings2 - | betree_message_t.Delete => + | betree.Message.Delete => do let bindings1 ← betree.List.pop_front_back (U64 × U64) bindings0 betree.Node.lookup_mut_in_bindings_back key bindings bindings1 - | betree_message_t.Upsert s => + | betree.Message.Upsert s => do let (_, i) := hd let v ← betree.upsert_update_fwd (Option.some i) s @@ -566,14 +560,14 @@ def betree.Node.apply_to_leaf_fwd_back betree.Node.lookup_mut_in_bindings_back key bindings bindings2 else match new_msg with - | betree_message_t.Insert v => + | betree.Message.Insert v => do let bindings1 ← betree.List.push_front_fwd_back (U64 × U64) bindings0 (key, v) betree.Node.lookup_mut_in_bindings_back key bindings bindings1 - | betree_message_t.Delete => + | betree.Message.Delete => betree.Node.lookup_mut_in_bindings_back key bindings bindings0 - | betree_message_t.Upsert s => + | betree.Message.Upsert s => do let v ← betree.upsert_update_fwd Option.none s let bindings1 ← @@ -582,170 +576,170 @@ def betree.Node.apply_to_leaf_fwd_back /- [betree_main::betree::Node::{5}::apply_messages_to_leaf] -/ divergent def betree.Node.apply_messages_to_leaf_fwd_back - (bindings : betree_list_t (U64 × U64)) - (new_msgs : betree_list_t (U64 × betree_message_t)) : - Result (betree_list_t (U64 × U64)) + (bindings : betree.List (U64 × U64)) + (new_msgs : betree.List (U64 × betree.Message)) : + Result (betree.List (U64 × U64)) := match new_msgs with - | betree_list_t.Cons new_msg new_msgs_tl => + | betree.List.Cons new_msg new_msgs_tl => do let (i, m) := new_msg let bindings0 ← betree.Node.apply_to_leaf_fwd_back bindings i m betree.Node.apply_messages_to_leaf_fwd_back bindings0 new_msgs_tl - | betree_list_t.Nil => Result.ret bindings + | betree.List.Nil => Result.ret bindings /- [betree_main::betree::Node::{5}::filter_messages_for_key] -/ divergent def betree.Node.filter_messages_for_key_fwd_back - (key : U64) (msgs : betree_list_t (U64 × betree_message_t)) : - Result (betree_list_t (U64 × betree_message_t)) + (key : U64) (msgs : betree.List (U64 × betree.Message)) : + Result (betree.List (U64 × betree.Message)) := match msgs with - | betree_list_t.Cons p l => + | betree.List.Cons p l => let (k, m) := p if k = key then do let msgs0 ← - betree.List.pop_front_back (U64 × betree_message_t) - (betree_list_t.Cons (k, m) l) + betree.List.pop_front_back (U64 × betree.Message) (betree.List.Cons + (k, m) l) betree.Node.filter_messages_for_key_fwd_back key msgs0 - else Result.ret (betree_list_t.Cons (k, m) l) - | betree_list_t.Nil => Result.ret betree_list_t.Nil + else Result.ret (betree.List.Cons (k, m) l) + | betree.List.Nil => Result.ret betree.List.Nil /- [betree_main::betree::Node::{5}::lookup_first_message_after_key] -/ divergent def betree.Node.lookup_first_message_after_key_fwd - (key : U64) (msgs : betree_list_t (U64 × betree_message_t)) : - Result (betree_list_t (U64 × betree_message_t)) + (key : U64) (msgs : betree.List (U64 × betree.Message)) : + Result (betree.List (U64 × betree.Message)) := match msgs with - | betree_list_t.Cons p next_msgs => + | betree.List.Cons p next_msgs => let (k, m) := p if k = key then betree.Node.lookup_first_message_after_key_fwd key next_msgs - else Result.ret (betree_list_t.Cons (k, m) next_msgs) - | betree_list_t.Nil => Result.ret betree_list_t.Nil + else Result.ret (betree.List.Cons (k, m) next_msgs) + | betree.List.Nil => Result.ret betree.List.Nil /- [betree_main::betree::Node::{5}::lookup_first_message_after_key] -/ divergent def betree.Node.lookup_first_message_after_key_back - (key : U64) (msgs : betree_list_t (U64 × betree_message_t)) - (ret0 : betree_list_t (U64 × betree_message_t)) : - Result (betree_list_t (U64 × betree_message_t)) + (key : U64) (msgs : betree.List (U64 × betree.Message)) + (ret0 : betree.List (U64 × betree.Message)) : + Result (betree.List (U64 × betree.Message)) := match msgs with - | betree_list_t.Cons p next_msgs => + | betree.List.Cons p next_msgs => let (k, m) := p if k = key then do let next_msgs0 ← betree.Node.lookup_first_message_after_key_back key next_msgs ret0 - Result.ret (betree_list_t.Cons (k, m) next_msgs0) + Result.ret (betree.List.Cons (k, m) next_msgs0) else Result.ret ret0 - | betree_list_t.Nil => Result.ret ret0 + | betree.List.Nil => Result.ret ret0 /- [betree_main::betree::Node::{5}::apply_to_internal] -/ def betree.Node.apply_to_internal_fwd_back - (msgs : betree_list_t (U64 × betree_message_t)) (key : U64) - (new_msg : betree_message_t) : - Result (betree_list_t (U64 × betree_message_t)) + (msgs : betree.List (U64 × betree.Message)) (key : U64) + (new_msg : betree.Message) : + Result (betree.List (U64 × betree.Message)) := do let msgs0 ← betree.Node.lookup_first_message_for_key_fwd key msgs - let b ← betree.List.head_has_key_fwd betree_message_t msgs0 key + let b ← betree.List.head_has_key_fwd betree.Message msgs0 key if b then match new_msg with - | betree_message_t.Insert i => + | betree.Message.Insert i => do let msgs1 ← betree.Node.filter_messages_for_key_fwd_back key msgs0 let msgs2 ← - betree.List.push_front_fwd_back (U64 × betree_message_t) msgs1 - (key, betree_message_t.Insert i) + betree.List.push_front_fwd_back (U64 × betree.Message) msgs1 (key, + betree.Message.Insert i) betree.Node.lookup_first_message_for_key_back key msgs msgs2 - | betree_message_t.Delete => + | betree.Message.Delete => do let msgs1 ← betree.Node.filter_messages_for_key_fwd_back key msgs0 let msgs2 ← - betree.List.push_front_fwd_back (U64 × betree_message_t) msgs1 - (key, betree_message_t.Delete) + betree.List.push_front_fwd_back (U64 × betree.Message) msgs1 (key, + betree.Message.Delete) betree.Node.lookup_first_message_for_key_back key msgs msgs2 - | betree_message_t.Upsert s => + | betree.Message.Upsert s => do - let p ← betree.List.hd_fwd (U64 × betree_message_t) msgs0 + let p ← betree.List.hd_fwd (U64 × betree.Message) msgs0 let (_, m) := p match m with - | betree_message_t.Insert prev => + | betree.Message.Insert prev => do let v ← betree.upsert_update_fwd (Option.some prev) s let msgs1 ← - betree.List.pop_front_back (U64 × betree_message_t) msgs0 + betree.List.pop_front_back (U64 × betree.Message) msgs0 let msgs2 ← - betree.List.push_front_fwd_back (U64 × betree_message_t) msgs1 - (key, betree_message_t.Insert v) + betree.List.push_front_fwd_back (U64 × betree.Message) msgs1 + (key, betree.Message.Insert v) betree.Node.lookup_first_message_for_key_back key msgs msgs2 - | betree_message_t.Delete => + | betree.Message.Delete => do let v ← betree.upsert_update_fwd Option.none s let msgs1 ← - betree.List.pop_front_back (U64 × betree_message_t) msgs0 + betree.List.pop_front_back (U64 × betree.Message) msgs0 let msgs2 ← - betree.List.push_front_fwd_back (U64 × betree_message_t) msgs1 - (key, betree_message_t.Insert v) + betree.List.push_front_fwd_back (U64 × betree.Message) msgs1 + (key, betree.Message.Insert v) betree.Node.lookup_first_message_for_key_back key msgs msgs2 - | betree_message_t.Upsert ufs => + | betree.Message.Upsert ufs => do let msgs1 ← betree.Node.lookup_first_message_after_key_fwd key msgs0 let msgs2 ← - betree.List.push_front_fwd_back (U64 × betree_message_t) msgs1 - (key, betree_message_t.Upsert s) + betree.List.push_front_fwd_back (U64 × betree.Message) msgs1 + (key, betree.Message.Upsert s) let msgs3 ← betree.Node.lookup_first_message_after_key_back key msgs0 msgs2 betree.Node.lookup_first_message_for_key_back key msgs msgs3 else do let msgs1 ← - betree.List.push_front_fwd_back (U64 × betree_message_t) msgs0 (key, + betree.List.push_front_fwd_back (U64 × betree.Message) msgs0 (key, new_msg) betree.Node.lookup_first_message_for_key_back key msgs msgs1 /- [betree_main::betree::Node::{5}::apply_messages_to_internal] -/ divergent def betree.Node.apply_messages_to_internal_fwd_back - (msgs : betree_list_t (U64 × betree_message_t)) - (new_msgs : betree_list_t (U64 × betree_message_t)) : - Result (betree_list_t (U64 × betree_message_t)) + (msgs : betree.List (U64 × betree.Message)) + (new_msgs : betree.List (U64 × betree.Message)) : + Result (betree.List (U64 × betree.Message)) := match new_msgs with - | betree_list_t.Cons new_msg new_msgs_tl => + | betree.List.Cons new_msg new_msgs_tl => do let (i, m) := new_msg let msgs0 ← betree.Node.apply_to_internal_fwd_back msgs i m betree.Node.apply_messages_to_internal_fwd_back msgs0 new_msgs_tl - | betree_list_t.Nil => Result.ret msgs + | betree.List.Nil => Result.ret msgs /- [betree_main::betree::Node::{5}::apply_messages] -/ mutual divergent def betree.Node.apply_messages_fwd - (self : betree_node_t) (params : betree_params_t) - (node_id_cnt : betree_node_id_counter_t) - (msgs : betree_list_t (U64 × betree_message_t)) (st : State) : + (self : betree.Node) (params : betree.Params) + (node_id_cnt : betree.NodeIdCounter) + (msgs : betree.List (U64 × betree.Message)) (st : State) : Result (State × Unit) := match self with - | betree_node_t.Internal node => + | betree.Node.Internal node => do let ⟨ i, i0, n, n0 ⟩ := node let (st0, content) ← betree.load_internal_node_fwd i st let content0 ← betree.Node.apply_messages_to_internal_fwd_back content msgs - let num_msgs ← betree.List.len_fwd (U64 × betree_message_t) content0 + let num_msgs ← betree.List.len_fwd (U64 × betree.Message) content0 if num_msgs >= params.betree_params_min_flush_size then do let (st1, content1) ← - betree.Internal.flush_fwd (betree_internal_t.mk i i0 n n0) params + betree.Internal.flush_fwd (betree.Internal.mk i i0 n n0) params node_id_cnt content0 st0 let (node0, _) ← - betree.Internal.flush_back (betree_internal_t.mk i i0 n n0) params + betree.Internal.flush_back (betree.Internal.mk i i0 n n0) params node_id_cnt content0 st0 let ⟨ i1, _, _, _ ⟩ := node0 let (st2, _) ← betree.store_internal_node_fwd i1 content1 st1 @@ -754,7 +748,7 @@ mutual divergent def betree.Node.apply_messages_fwd do let (st1, _) ← betree.store_internal_node_fwd i content0 st0 Result.ret (st1, ()) - | betree_node_t.Leaf node => + | betree.Node.Leaf node => do let (st0, content) ← betree.load_leaf_node_fwd node.betree_leaf_id st let content0 ← betree.Node.apply_messages_to_leaf_fwd_back content msgs @@ -766,8 +760,7 @@ mutual divergent def betree.Node.apply_messages_fwd let (st1, _) ← betree.Leaf.split_fwd node content0 params node_id_cnt st0 let (st2, _) ← - betree.store_leaf_node_fwd node.betree_leaf_id betree_list_t.Nil - st1 + betree.store_leaf_node_fwd node.betree_leaf_id betree.List.Nil st1 Result.ret (st2, ()) else do @@ -777,38 +770,38 @@ mutual divergent def betree.Node.apply_messages_fwd /- [betree_main::betree::Node::{5}::apply_messages] -/ divergent def betree.Node.apply_messages_back - (self : betree_node_t) (params : betree_params_t) - (node_id_cnt : betree_node_id_counter_t) - (msgs : betree_list_t (U64 × betree_message_t)) (st : State) : - Result (betree_node_t × betree_node_id_counter_t) + (self : betree.Node) (params : betree.Params) + (node_id_cnt : betree.NodeIdCounter) + (msgs : betree.List (U64 × betree.Message)) (st : State) : + Result (betree.Node × betree.NodeIdCounter) := match self with - | betree_node_t.Internal node => + | betree.Node.Internal node => do let ⟨ i, i0, n, n0 ⟩ := node let (st0, content) ← betree.load_internal_node_fwd i st let content0 ← betree.Node.apply_messages_to_internal_fwd_back content msgs - let num_msgs ← betree.List.len_fwd (U64 × betree_message_t) content0 + let num_msgs ← betree.List.len_fwd (U64 × betree.Message) content0 if num_msgs >= params.betree_params_min_flush_size then do let (st1, content1) ← - betree.Internal.flush_fwd (betree_internal_t.mk i i0 n n0) params + betree.Internal.flush_fwd (betree.Internal.mk i i0 n n0) params node_id_cnt content0 st0 let (node0, node_id_cnt0) ← - betree.Internal.flush_back (betree_internal_t.mk i i0 n n0) params + betree.Internal.flush_back (betree.Internal.mk i i0 n n0) params node_id_cnt content0 st0 let ⟨ i1, i2, n1, n2 ⟩ := node0 let _ ← betree.store_internal_node_fwd i1 content1 st1 - Result.ret (betree_node_t.Internal (betree_internal_t.mk i1 i2 n1 - n2), node_id_cnt0) + Result.ret (betree.Node.Internal (betree.Internal.mk i1 i2 n1 n2), + node_id_cnt0) else do let _ ← betree.store_internal_node_fwd i content0 st0 - Result.ret (betree_node_t.Internal (betree_internal_t.mk i i0 n n0), + Result.ret (betree.Node.Internal (betree.Internal.mk i i0 n n0), node_id_cnt) - | betree_node_t.Leaf node => + | betree.Node.Leaf node => do let (st0, content) ← betree.load_leaf_node_fwd node.betree_leaf_id st let content0 ← betree.Node.apply_messages_to_leaf_fwd_back content msgs @@ -820,29 +813,28 @@ divergent def betree.Node.apply_messages_back let (st1, new_node) ← betree.Leaf.split_fwd node content0 params node_id_cnt st0 let _ ← - betree.store_leaf_node_fwd node.betree_leaf_id betree_list_t.Nil - st1 + betree.store_leaf_node_fwd node.betree_leaf_id betree.List.Nil st1 let node_id_cnt0 ← betree.Leaf.split_back node content0 params node_id_cnt st0 - Result.ret (betree_node_t.Internal new_node, node_id_cnt0) + Result.ret (betree.Node.Internal new_node, node_id_cnt0) else do let _ ← betree.store_leaf_node_fwd node.betree_leaf_id content0 st0 - Result.ret (betree_node_t.Leaf { node with betree_leaf_size := len }, + Result.ret (betree.Node.Leaf { node with betree_leaf_size := len }, node_id_cnt) /- [betree_main::betree::Internal::{4}::flush] -/ divergent def betree.Internal.flush_fwd - (self : betree_internal_t) (params : betree_params_t) - (node_id_cnt : betree_node_id_counter_t) - (content : betree_list_t (U64 × betree_message_t)) (st : State) : - Result (State × (betree_list_t (U64 × betree_message_t))) + (self : betree.Internal) (params : betree.Params) + (node_id_cnt : betree.NodeIdCounter) + (content : betree.List (U64 × betree.Message)) (st : State) : + Result (State × (betree.List (U64 × betree.Message))) := do let ⟨ _, i, n, n0 ⟩ := self - let p ← betree.List.partition_at_pivot_fwd betree_message_t content i + let p ← betree.List.partition_at_pivot_fwd betree.Message content i let (msgs_left, msgs_right) := p - let len_left ← betree.List.len_fwd (U64 × betree_message_t) msgs_left + let len_left ← betree.List.len_fwd (U64 × betree.Message) msgs_left if len_left >= params.betree_params_min_flush_size then do @@ -851,7 +843,7 @@ divergent def betree.Internal.flush_fwd let (_, node_id_cnt0) ← betree.Node.apply_messages_back n params node_id_cnt msgs_left st let len_right ← - betree.List.len_fwd (U64 × betree_message_t) msgs_right + betree.List.len_fwd (U64 × betree.Message) msgs_right if len_right >= params.betree_params_min_flush_size then do @@ -861,7 +853,7 @@ divergent def betree.Internal.flush_fwd let _ ← betree.Node.apply_messages_back n0 params node_id_cnt0 msgs_right st0 - Result.ret (st1, betree_list_t.Nil) + Result.ret (st1, betree.List.Nil) else Result.ret (st0, msgs_right) else do @@ -873,16 +865,16 @@ divergent def betree.Internal.flush_fwd /- [betree_main::betree::Internal::{4}::flush] -/ divergent def betree.Internal.flush_back - (self : betree_internal_t) (params : betree_params_t) - (node_id_cnt : betree_node_id_counter_t) - (content : betree_list_t (U64 × betree_message_t)) (st : State) : - Result (betree_internal_t × betree_node_id_counter_t) + (self : betree.Internal) (params : betree.Params) + (node_id_cnt : betree.NodeIdCounter) + (content : betree.List (U64 × betree.Message)) (st : State) : + Result (betree.Internal × betree.NodeIdCounter) := do let ⟨ i, i0, n, n0 ⟩ := self - let p ← betree.List.partition_at_pivot_fwd betree_message_t content i0 + let p ← betree.List.partition_at_pivot_fwd betree.Message content i0 let (msgs_left, msgs_right) := p - let len_left ← betree.List.len_fwd (U64 × betree_message_t) msgs_left + let len_left ← betree.List.len_fwd (U64 × betree.Message) msgs_left if len_left >= params.betree_params_min_flush_size then do @@ -891,60 +883,60 @@ divergent def betree.Internal.flush_back let (n1, node_id_cnt0) ← betree.Node.apply_messages_back n params node_id_cnt msgs_left st let len_right ← - betree.List.len_fwd (U64 × betree_message_t) msgs_right + betree.List.len_fwd (U64 × betree.Message) msgs_right if len_right >= params.betree_params_min_flush_size then do let (n2, node_id_cnt1) ← betree.Node.apply_messages_back n0 params node_id_cnt0 msgs_right st0 - Result.ret (betree_internal_t.mk i i0 n1 n2, node_id_cnt1) - else Result.ret (betree_internal_t.mk i i0 n1 n0, node_id_cnt0) + Result.ret (betree.Internal.mk i i0 n1 n2, node_id_cnt1) + else Result.ret (betree.Internal.mk i i0 n1 n0, node_id_cnt0) else do let (n1, node_id_cnt0) ← betree.Node.apply_messages_back n0 params node_id_cnt msgs_right st - Result.ret (betree_internal_t.mk i i0 n n1, node_id_cnt0) + Result.ret (betree.Internal.mk i i0 n n1, node_id_cnt0) end /- [betree_main::betree::Node::{5}::apply] -/ def betree.Node.apply_fwd - (self : betree_node_t) (params : betree_params_t) - (node_id_cnt : betree_node_id_counter_t) (key : U64) - (new_msg : betree_message_t) (st : State) : + (self : betree.Node) (params : betree.Params) + (node_id_cnt : betree.NodeIdCounter) (key : U64) (new_msg : betree.Message) + (st : State) : Result (State × Unit) := do - let l := betree_list_t.Nil + let l := betree.List.Nil let (st0, _) ← - betree.Node.apply_messages_fwd self params node_id_cnt - (betree_list_t.Cons (key, new_msg) l) st + betree.Node.apply_messages_fwd self params node_id_cnt (betree.List.Cons + (key, new_msg) l) st let _ ← - betree.Node.apply_messages_back self params node_id_cnt - (betree_list_t.Cons (key, new_msg) l) st + betree.Node.apply_messages_back self params node_id_cnt (betree.List.Cons + (key, new_msg) l) st Result.ret (st0, ()) /- [betree_main::betree::Node::{5}::apply] -/ def betree.Node.apply_back - (self : betree_node_t) (params : betree_params_t) - (node_id_cnt : betree_node_id_counter_t) (key : U64) - (new_msg : betree_message_t) (st : State) : - Result (betree_node_t × betree_node_id_counter_t) + (self : betree.Node) (params : betree.Params) + (node_id_cnt : betree.NodeIdCounter) (key : U64) (new_msg : betree.Message) + (st : State) : + Result (betree.Node × betree.NodeIdCounter) := - let l := betree_list_t.Nil - betree.Node.apply_messages_back self params node_id_cnt (betree_list_t.Cons + let l := betree.List.Nil + betree.Node.apply_messages_back self params node_id_cnt (betree.List.Cons (key, new_msg) l) st /- [betree_main::betree::BeTree::{6}::new] -/ def betree.BeTree.new_fwd (min_flush_size : U64) (split_size : U64) (st : State) : - Result (State × betree_be_tree_t) + Result (State × betree.BeTree) := do let node_id_cnt ← betree.NodeIdCounter.new_fwd let id ← betree.NodeIdCounter.fresh_id_fwd node_id_cnt - let (st0, _) ← betree.store_leaf_node_fwd id betree_list_t.Nil st + let (st0, _) ← betree.store_leaf_node_fwd id betree.List.Nil st let node_id_cnt0 ← betree.NodeIdCounter.fresh_id_back node_id_cnt Result.ret (st0, { @@ -955,7 +947,7 @@ def betree.BeTree.new_fwd }, betree_be_tree_node_id_cnt := node_id_cnt0, betree_be_tree_root := - (betree_node_t.Leaf + (betree.Node.Leaf { betree_leaf_id := id, betree_leaf_size := (U64.ofInt 0 (by intlit)) @@ -964,7 +956,7 @@ def betree.BeTree.new_fwd /- [betree_main::betree::BeTree::{6}::apply] -/ def betree.BeTree.apply_fwd - (self : betree_be_tree_t) (key : U64) (msg : betree_message_t) (st : State) : + (self : betree.BeTree) (key : U64) (msg : betree.Message) (st : State) : Result (State × Unit) := do @@ -978,8 +970,8 @@ def betree.BeTree.apply_fwd /- [betree_main::betree::BeTree::{6}::apply] -/ def betree.BeTree.apply_back - (self : betree_be_tree_t) (key : U64) (msg : betree_message_t) (st : State) : - Result betree_be_tree_t + (self : betree.BeTree) (key : U64) (msg : betree.Message) (st : State) : + Result betree.BeTree := do let (n, nic) ← @@ -990,74 +982,66 @@ def betree.BeTree.apply_back /- [betree_main::betree::BeTree::{6}::insert] -/ def betree.BeTree.insert_fwd - (self : betree_be_tree_t) (key : U64) (value : U64) (st : State) : + (self : betree.BeTree) (key : U64) (value : U64) (st : State) : Result (State × Unit) := do let (st0, _) ← - betree.BeTree.apply_fwd self key (betree_message_t.Insert value) st + betree.BeTree.apply_fwd self key (betree.Message.Insert value) st let _ ← - betree.BeTree.apply_back self key (betree_message_t.Insert value) st + betree.BeTree.apply_back self key (betree.Message.Insert value) st Result.ret (st0, ()) /- [betree_main::betree::BeTree::{6}::insert] -/ def betree.BeTree.insert_back - (self : betree_be_tree_t) (key : U64) (value : U64) (st : State) : - Result betree_be_tree_t + (self : betree.BeTree) (key : U64) (value : U64) (st : State) : + Result betree.BeTree := - betree.BeTree.apply_back self key (betree_message_t.Insert value) st + betree.BeTree.apply_back self key (betree.Message.Insert value) st /- [betree_main::betree::BeTree::{6}::delete] -/ def betree.BeTree.delete_fwd - (self : betree_be_tree_t) (key : U64) (st : State) : - Result (State × Unit) - := + (self : betree.BeTree) (key : U64) (st : State) : Result (State × Unit) := do - let (st0, _) ← - betree.BeTree.apply_fwd self key betree_message_t.Delete st - let _ ← betree.BeTree.apply_back self key betree_message_t.Delete st + let (st0, _) ← betree.BeTree.apply_fwd self key betree.Message.Delete st + let _ ← betree.BeTree.apply_back self key betree.Message.Delete st Result.ret (st0, ()) /- [betree_main::betree::BeTree::{6}::delete] -/ def betree.BeTree.delete_back - (self : betree_be_tree_t) (key : U64) (st : State) : - Result betree_be_tree_t - := - betree.BeTree.apply_back self key betree_message_t.Delete st + (self : betree.BeTree) (key : U64) (st : State) : Result betree.BeTree := + betree.BeTree.apply_back self key betree.Message.Delete st /- [betree_main::betree::BeTree::{6}::upsert] -/ def betree.BeTree.upsert_fwd - (self : betree_be_tree_t) (key : U64) (upd : betree_upsert_fun_state_t) - (st : State) : + (self : betree.BeTree) (key : U64) (upd : betree.UpsertFunState) (st : State) + : Result (State × Unit) := do let (st0, _) ← - betree.BeTree.apply_fwd self key (betree_message_t.Upsert upd) st - let _ ← - betree.BeTree.apply_back self key (betree_message_t.Upsert upd) st + betree.BeTree.apply_fwd self key (betree.Message.Upsert upd) st + let _ ← betree.BeTree.apply_back self key (betree.Message.Upsert upd) st Result.ret (st0, ()) /- [betree_main::betree::BeTree::{6}::upsert] -/ def betree.BeTree.upsert_back - (self : betree_be_tree_t) (key : U64) (upd : betree_upsert_fun_state_t) - (st : State) : - Result betree_be_tree_t + (self : betree.BeTree) (key : U64) (upd : betree.UpsertFunState) (st : State) + : + Result betree.BeTree := - betree.BeTree.apply_back self key (betree_message_t.Upsert upd) st + betree.BeTree.apply_back self key (betree.Message.Upsert upd) st /- [betree_main::betree::BeTree::{6}::lookup] -/ def betree.BeTree.lookup_fwd - (self : betree_be_tree_t) (key : U64) (st : State) : + (self : betree.BeTree) (key : U64) (st : State) : Result (State × (Option U64)) := betree.Node.lookup_fwd self.betree_be_tree_root key st /- [betree_main::betree::BeTree::{6}::lookup] -/ def betree.BeTree.lookup_back - (self : betree_be_tree_t) (key : U64) (st : State) : - Result betree_be_tree_t - := + (self : betree.BeTree) (key : U64) (st : State) : Result betree.BeTree := do let n ← betree.Node.lookup_back self.betree_be_tree_root key st Result.ret { self with betree_be_tree_root := n } diff --git a/tests/lean/BetreeMain/FunsExternal.lean b/tests/lean/BetreeMain/FunsExternal.lean index 0c715ef9..7bcba380 100644 --- a/tests/lean/BetreeMain/FunsExternal.lean +++ b/tests/lean/BetreeMain/FunsExternal.lean @@ -9,24 +9,24 @@ open betree_main /- [betree_main::betree_utils::load_internal_node] -/ def betree_utils.load_internal_node_fwd : - U64 → State → Result (State × (betree_list_t (U64 × betree_message_t))) := + U64 → State → Result (State × (betree.List (U64 × betree.Message))) := fun _ _ => .fail .panic /- [betree_main::betree_utils::store_internal_node] -/ def betree_utils.store_internal_node_fwd : - U64 → betree_list_t (U64 × betree_message_t) → State → Result (State + U64 → betree.List (U64 × betree.Message) → State → Result (State × Unit) := fun _ _ _ => .fail .panic /- [betree_main::betree_utils::load_leaf_node] -/ def betree_utils.load_leaf_node_fwd - : U64 → State → Result (State × (betree_list_t (U64 × U64))) := + : U64 → State → Result (State × (betree.List (U64 × U64))) := fun _ _ => .fail .panic /- [betree_main::betree_utils::store_leaf_node] -/ def betree_utils.store_leaf_node_fwd - : U64 → betree_list_t (U64 × U64) → State → Result (State × Unit) := + : U64 → betree.List (U64 × U64) → State → Result (State × Unit) := fun _ _ _ => .fail .panic /- [core::option::Option::{0}::unwrap] -/ diff --git a/tests/lean/BetreeMain/FunsExternal_Template.lean b/tests/lean/BetreeMain/FunsExternal_Template.lean index 9b5a54e5..d768bb20 100644 --- a/tests/lean/BetreeMain/FunsExternal_Template.lean +++ b/tests/lean/BetreeMain/FunsExternal_Template.lean @@ -8,22 +8,21 @@ open betree_main /- [betree_main::betree_utils::load_internal_node] -/ axiom betree_utils.load_internal_node_fwd - : - U64 → State → Result (State × (betree_list_t (U64 × betree_message_t))) + : U64 → State → Result (State × (betree.List (U64 × betree.Message))) /- [betree_main::betree_utils::store_internal_node] -/ axiom betree_utils.store_internal_node_fwd : - U64 → betree_list_t (U64 × betree_message_t) → State → Result (State - × Unit) + U64 → betree.List (U64 × betree.Message) → State → Result (State × + Unit) /- [betree_main::betree_utils::load_leaf_node] -/ axiom betree_utils.load_leaf_node_fwd - : U64 → State → Result (State × (betree_list_t (U64 × U64))) + : U64 → State → Result (State × (betree.List (U64 × U64))) /- [betree_main::betree_utils::store_leaf_node] -/ axiom betree_utils.store_leaf_node_fwd - : U64 → betree_list_t (U64 × U64) → State → Result (State × Unit) + : U64 → betree.List (U64 × U64) → State → Result (State × Unit) /- [core::option::Option::{0}::unwrap] -/ axiom core.option.Option.unwrap_fwd diff --git a/tests/lean/BetreeMain/Types.lean b/tests/lean/BetreeMain/Types.lean index 3ecbd668..afbdac9a 100644 --- a/tests/lean/BetreeMain/Types.lean +++ b/tests/lean/BetreeMain/Types.lean @@ -5,53 +5,53 @@ open Primitives namespace betree_main /- [betree_main::betree::List] -/ -inductive betree_list_t (T : Type) := -| Cons : T → betree_list_t T → betree_list_t T -| Nil : betree_list_t T +inductive betree.List (T : Type) := +| Cons : T → betree.List T → betree.List T +| Nil : betree.List T /- [betree_main::betree::UpsertFunState] -/ -inductive betree_upsert_fun_state_t := -| Add : U64 → betree_upsert_fun_state_t -| Sub : U64 → betree_upsert_fun_state_t +inductive betree.UpsertFunState := +| Add : U64 → betree.UpsertFunState +| Sub : U64 → betree.UpsertFunState /- [betree_main::betree::Message] -/ -inductive betree_message_t := -| Insert : U64 → betree_message_t -| Delete : betree_message_t -| Upsert : betree_upsert_fun_state_t → betree_message_t +inductive betree.Message := +| Insert : U64 → betree.Message +| Delete : betree.Message +| Upsert : betree.UpsertFunState → betree.Message /- [betree_main::betree::Leaf] -/ -structure betree_leaf_t where +structure betree.Leaf where betree_leaf_id : U64 betree_leaf_size : U64 mutual /- [betree_main::betree::Node] -/ -inductive betree_node_t := -| Internal : betree_internal_t → betree_node_t -| Leaf : betree_leaf_t → betree_node_t +inductive betree.Node := +| Internal : betree.Internal → betree.Node +| Leaf : betree.Leaf → betree.Node /- [betree_main::betree::Internal] -/ -inductive betree_internal_t := -| mk : U64 → U64 → betree_node_t → betree_node_t → betree_internal_t +inductive betree.Internal := +| mk : U64 → U64 → betree.Node → betree.Node → betree.Internal end /- [betree_main::betree::Params] -/ -structure betree_params_t where +structure betree.Params where betree_params_min_flush_size : U64 betree_params_split_size : U64 /- [betree_main::betree::NodeIdCounter] -/ -structure betree_node_id_counter_t where +structure betree.NodeIdCounter where betree_node_id_counter_next_node_id : U64 /- [betree_main::betree::BeTree] -/ -structure betree_be_tree_t where - betree_be_tree_params : betree_params_t - betree_be_tree_node_id_cnt : betree_node_id_counter_t - betree_be_tree_root : betree_node_t +structure betree.BeTree where + betree_be_tree_params : betree.Params + betree_be_tree_node_id_cnt : betree.NodeIdCounter + betree_be_tree_root : betree.Node /- The state type used in the state-error monad -/ axiom State : Type diff --git a/tests/lean/Constants.lean b/tests/lean/Constants.lean index 39c270be..7014b0d8 100644 --- a/tests/lean/Constants.lean +++ b/tests/lean/Constants.lean @@ -34,12 +34,12 @@ def mk_pair0_fwd (x : U32) (y : U32) : Result (U32 × U32) := Result.ret (x, y) /- [constants::Pair] -/ -structure pair_t (T1 T2 : Type) where +structure Pair (T1 T2 : Type) where pair_x : T1 pair_y : T2 /- [constants::mk_pair1] -/ -def mk_pair1_fwd (x : U32) (y : U32) : Result (pair_t U32 U32) := +def mk_pair1_fwd (x : U32) (y : U32) : Result (Pair U32 U32) := Result.ret { pair_x := x, pair_y := y } /- [constants::P0] -/ @@ -48,9 +48,9 @@ def p0_body : Result (U32 × U32) := def p0_c : (U32 × U32) := eval_global p0_body (by simp) /- [constants::P1] -/ -def p1_body : Result (pair_t U32 U32) := +def p1_body : Result (Pair U32 U32) := mk_pair1_fwd (U32.ofInt 0 (by intlit)) (U32.ofInt 1 (by intlit)) -def p1_c : pair_t U32 U32 := eval_global p1_body (by simp) +def p1_c : Pair U32 U32 := eval_global p1_body (by simp) /- [constants::P2] -/ def p2_body : Result (U32 × U32) := @@ -58,22 +58,22 @@ def p2_body : Result (U32 × U32) := def p2_c : (U32 × U32) := eval_global p2_body (by simp) /- [constants::P3] -/ -def p3_body : Result (pair_t U32 U32) := +def p3_body : Result (Pair U32 U32) := Result.ret { pair_x := (U32.ofInt 0 (by intlit)), pair_y := (U32.ofInt 1 (by intlit)) } -def p3_c : pair_t U32 U32 := eval_global p3_body (by simp) +def p3_c : Pair U32 U32 := eval_global p3_body (by simp) /- [constants::Wrap] -/ -structure wrap_t (T : Type) where +structure Wrap (T : Type) where wrap_val : T /- [constants::Wrap::{0}::new] -/ -def Wrap.new_fwd (T : Type) (val : T) : Result (wrap_t T) := +def Wrap.new_fwd (T : Type) (val : T) : Result (Wrap T) := Result.ret { wrap_val := val } /- [constants::Y] -/ -def y_body : Result (wrap_t I32) := Wrap.new_fwd I32 (I32.ofInt 2 (by intlit)) -def y_c : wrap_t I32 := eval_global y_body (by simp) +def y_body : Result (Wrap I32) := Wrap.new_fwd I32 (I32.ofInt 2 (by intlit)) +def y_c : Wrap I32 := eval_global y_body (by simp) /- [constants::unwrap_y] -/ def unwrap_y_fwd : Result I32 := @@ -123,12 +123,12 @@ def s2_body : Result U32 := incr_fwd s1_c def s2_c : U32 := eval_global s2_body (by simp) /- [constants::S3] -/ -def s3_body : Result (pair_t U32 U32) := Result.ret p3_c -def s3_c : pair_t U32 U32 := eval_global s3_body (by simp) +def s3_body : Result (Pair U32 U32) := Result.ret p3_c +def s3_c : Pair U32 U32 := eval_global s3_body (by simp) /- [constants::S4] -/ -def s4_body : Result (pair_t U32 U32) := +def s4_body : Result (Pair U32 U32) := mk_pair1_fwd (U32.ofInt 7 (by intlit)) (U32.ofInt 8 (by intlit)) -def s4_c : pair_t U32 U32 := eval_global s4_body (by simp) +def s4_c : Pair U32 U32 := eval_global s4_body (by simp) end constants diff --git a/tests/lean/External/Funs.lean b/tests/lean/External/Funs.lean index 6bfffc33..10efc3db 100644 --- a/tests/lean/External/Funs.lean +++ b/tests/lean/External/Funs.lean @@ -28,10 +28,10 @@ def swap_back /- [external::test_new_non_zero_u32] -/ def test_new_non_zero_u32_fwd - (x : U32) (st : State) : Result (State × core_num_nonzero_non_zero_u32_t) := + (x : U32) (st : State) : Result (State × core.num.nonzero.NonZeroU32) := do let (st0, opt) ← core.num.nonzero.NonZeroU32.new_fwd x st - core.option.Option.unwrap_fwd core_num_nonzero_non_zero_u32_t opt st0 + core.option.Option.unwrap_fwd core.num.nonzero.NonZeroU32 opt st0 /- [external::test_vec] -/ def test_vec_fwd : Result Unit := diff --git a/tests/lean/External/FunsExternal_Template.lean b/tests/lean/External/FunsExternal_Template.lean index d6c0eb1d..669fe91b 100644 --- a/tests/lean/External/FunsExternal_Template.lean +++ b/tests/lean/External/FunsExternal_Template.lean @@ -20,8 +20,7 @@ axiom core.mem.swap_back1 /- [core::num::nonzero::NonZeroU32::{14}::new] -/ axiom core.num.nonzero.NonZeroU32.new_fwd - : - U32 → State → Result (State × (Option core_num_nonzero_non_zero_u32_t)) + : U32 → State → Result (State × (Option core.num.nonzero.NonZeroU32)) /- [core::option::Option::{0}::unwrap] -/ axiom core.option.Option.unwrap_fwd diff --git a/tests/lean/External/Types.lean b/tests/lean/External/Types.lean index 316f6474..ba984e2a 100644 --- a/tests/lean/External/Types.lean +++ b/tests/lean/External/Types.lean @@ -5,7 +5,7 @@ open Primitives namespace external /- [core::num::nonzero::NonZeroU32] -/ -axiom core_num_nonzero_non_zero_u32_t : Type +axiom core.num.nonzero.NonZeroU32 : Type /- The state type used in the state-error monad -/ axiom State : Type diff --git a/tests/lean/Hashmap/Funs.lean b/tests/lean/Hashmap/Funs.lean index 8f54eca8..4f16688b 100644 --- a/tests/lean/Hashmap/Funs.lean +++ b/tests/lean/Hashmap/Funs.lean @@ -11,28 +11,28 @@ def hash_key_fwd (k : Usize) : Result Usize := /- [hashmap::HashMap::{0}::allocate_slots] -/ divergent def HashMap.allocate_slots_loop_fwd - (T : Type) (slots : Vec (list_t T)) (n : Usize) : Result (Vec (list_t T)) := + (T : Type) (slots : Vec (List T)) (n : Usize) : Result (Vec (List T)) := if n > (Usize.ofInt 0 (by intlit)) then do - let slots0 ← vec_push_back (list_t T) slots list_t.Nil + let slots0 ← vec_push_back (List T) slots List.Nil let n0 ← n - (Usize.ofInt 1 (by intlit)) HashMap.allocate_slots_loop_fwd T slots0 n0 else Result.ret slots /- [hashmap::HashMap::{0}::allocate_slots] -/ def HashMap.allocate_slots_fwd - (T : Type) (slots : Vec (list_t T)) (n : Usize) : Result (Vec (list_t T)) := + (T : Type) (slots : Vec (List T)) (n : Usize) : Result (Vec (List T)) := HashMap.allocate_slots_loop_fwd T slots n /- [hashmap::HashMap::{0}::new_with_capacity] -/ def HashMap.new_with_capacity_fwd (T : Type) (capacity : Usize) (max_load_dividend : Usize) (max_load_divisor : Usize) : - Result (hash_map_t T) + Result (HashMap T) := do - let v := vec_new (list_t T) + let v := vec_new (List T) let slots ← HashMap.allocate_slots_fwd T v capacity let i ← capacity * max_load_dividend let i0 ← i / max_load_divisor @@ -45,25 +45,25 @@ def HashMap.new_with_capacity_fwd } /- [hashmap::HashMap::{0}::new] -/ -def HashMap.new_fwd (T : Type) : Result (hash_map_t T) := +def HashMap.new_fwd (T : Type) : Result (HashMap T) := HashMap.new_with_capacity_fwd T (Usize.ofInt 32 (by intlit)) (Usize.ofInt 4 (by intlit)) (Usize.ofInt 5 (by intlit)) /- [hashmap::HashMap::{0}::clear] -/ divergent def HashMap.clear_loop_fwd_back - (T : Type) (slots : Vec (list_t T)) (i : Usize) : Result (Vec (list_t T)) := - let i0 := vec_len (list_t T) slots + (T : Type) (slots : Vec (List T)) (i : Usize) : Result (Vec (List T)) := + let i0 := vec_len (List T) slots if i < i0 then do let i1 ← i + (Usize.ofInt 1 (by intlit)) - let slots0 ← vec_index_mut_back (list_t T) slots i list_t.Nil + let slots0 ← vec_index_mut_back (List T) slots i List.Nil HashMap.clear_loop_fwd_back T slots0 i1 else Result.ret slots /- [hashmap::HashMap::{0}::clear] -/ def HashMap.clear_fwd_back - (T : Type) (self : hash_map_t T) : Result (hash_map_t T) := + (T : Type) (self : HashMap T) : Result (HashMap T) := do let v ← HashMap.clear_loop_fwd_back T self.hash_map_slots @@ -77,66 +77,66 @@ def HashMap.clear_fwd_back } /- [hashmap::HashMap::{0}::len] -/ -def HashMap.len_fwd (T : Type) (self : hash_map_t T) : Result Usize := +def HashMap.len_fwd (T : Type) (self : HashMap T) : Result Usize := Result.ret self.hash_map_num_entries /- [hashmap::HashMap::{0}::insert_in_list] -/ divergent def HashMap.insert_in_list_loop_fwd - (T : Type) (key : Usize) (value : T) (ls : list_t T) : Result Bool := + (T : Type) (key : Usize) (value : T) (ls : List T) : Result Bool := match ls with - | list_t.Cons ckey cvalue tl => + | List.Cons ckey cvalue tl => if ckey = key then Result.ret false else HashMap.insert_in_list_loop_fwd T key value tl - | list_t.Nil => Result.ret true + | List.Nil => Result.ret true /- [hashmap::HashMap::{0}::insert_in_list] -/ def HashMap.insert_in_list_fwd - (T : Type) (key : Usize) (value : T) (ls : list_t T) : Result Bool := + (T : Type) (key : Usize) (value : T) (ls : List T) : Result Bool := HashMap.insert_in_list_loop_fwd T key value ls /- [hashmap::HashMap::{0}::insert_in_list] -/ divergent def HashMap.insert_in_list_loop_back - (T : Type) (key : Usize) (value : T) (ls : list_t T) : Result (list_t T) := + (T : Type) (key : Usize) (value : T) (ls : List T) : Result (List T) := match ls with - | list_t.Cons ckey cvalue tl => + | List.Cons ckey cvalue tl => if ckey = key - then Result.ret (list_t.Cons ckey value tl) + then Result.ret (List.Cons ckey value tl) else do let tl0 ← HashMap.insert_in_list_loop_back T key value tl - Result.ret (list_t.Cons ckey cvalue tl0) - | list_t.Nil => let l := list_t.Nil - Result.ret (list_t.Cons key value l) + Result.ret (List.Cons ckey cvalue tl0) + | List.Nil => let l := List.Nil + Result.ret (List.Cons key value l) /- [hashmap::HashMap::{0}::insert_in_list] -/ def HashMap.insert_in_list_back - (T : Type) (key : Usize) (value : T) (ls : list_t T) : Result (list_t T) := + (T : Type) (key : Usize) (value : T) (ls : List T) : Result (List T) := HashMap.insert_in_list_loop_back T key value ls /- [hashmap::HashMap::{0}::insert_no_resize] -/ def HashMap.insert_no_resize_fwd_back - (T : Type) (self : hash_map_t T) (key : Usize) (value : T) : - Result (hash_map_t T) + (T : Type) (self : HashMap T) (key : Usize) (value : T) : + Result (HashMap T) := do let hash ← hash_key_fwd key - let i := vec_len (list_t T) self.hash_map_slots + let i := vec_len (List T) self.hash_map_slots let hash_mod ← hash % i - let l ← vec_index_mut_fwd (list_t T) self.hash_map_slots hash_mod + let l ← vec_index_mut_fwd (List T) self.hash_map_slots hash_mod let inserted ← HashMap.insert_in_list_fwd T key value l if inserted then do let i0 ← self.hash_map_num_entries + (Usize.ofInt 1 (by intlit)) let l0 ← HashMap.insert_in_list_back T key value l - let v ← vec_index_mut_back (list_t T) self.hash_map_slots hash_mod l0 + let v ← vec_index_mut_back (List T) self.hash_map_slots hash_mod l0 Result.ret { self with hash_map_num_entries := i0, hash_map_slots := v } else do let l0 ← HashMap.insert_in_list_back T key value l - let v ← vec_index_mut_back (list_t T) self.hash_map_slots hash_mod l0 + let v ← vec_index_mut_back (List T) self.hash_map_slots hash_mod l0 Result.ret { self with hash_map_slots := v } /- [core::num::u32::{9}::MAX] -/ @@ -146,50 +146,50 @@ def core_num_u32_max_c : U32 := eval_global core_num_u32_max_body (by simp) /- [hashmap::HashMap::{0}::move_elements_from_list] -/ divergent def HashMap.move_elements_from_list_loop_fwd_back - (T : Type) (ntable : hash_map_t T) (ls : list_t T) : Result (hash_map_t T) := + (T : Type) (ntable : HashMap T) (ls : List T) : Result (HashMap T) := match ls with - | list_t.Cons k v tl => + | List.Cons k v tl => do let ntable0 ← HashMap.insert_no_resize_fwd_back T ntable k v HashMap.move_elements_from_list_loop_fwd_back T ntable0 tl - | list_t.Nil => Result.ret ntable + | List.Nil => Result.ret ntable /- [hashmap::HashMap::{0}::move_elements_from_list] -/ def HashMap.move_elements_from_list_fwd_back - (T : Type) (ntable : hash_map_t T) (ls : list_t T) : Result (hash_map_t T) := + (T : Type) (ntable : HashMap T) (ls : List T) : Result (HashMap T) := HashMap.move_elements_from_list_loop_fwd_back T ntable ls /- [hashmap::HashMap::{0}::move_elements] -/ divergent def HashMap.move_elements_loop_fwd_back - (T : Type) (ntable : hash_map_t T) (slots : Vec (list_t T)) (i : Usize) : - Result ((hash_map_t T) × (Vec (list_t T))) + (T : Type) (ntable : HashMap T) (slots : Vec (List T)) (i : Usize) : + Result ((HashMap T) × (Vec (List T))) := - let i0 := vec_len (list_t T) slots + let i0 := vec_len (List T) slots if i < i0 then do - let l ← vec_index_mut_fwd (list_t T) slots i - let ls := mem_replace_fwd (list_t T) l list_t.Nil + let l ← vec_index_mut_fwd (List T) slots i + let ls := mem_replace_fwd (List T) l List.Nil let ntable0 ← HashMap.move_elements_from_list_fwd_back T ntable ls let i1 ← i + (Usize.ofInt 1 (by intlit)) - let l0 := mem_replace_back (list_t T) l list_t.Nil - let slots0 ← vec_index_mut_back (list_t T) slots i l0 + let l0 := mem_replace_back (List T) l List.Nil + let slots0 ← vec_index_mut_back (List T) slots i l0 HashMap.move_elements_loop_fwd_back T ntable0 slots0 i1 else Result.ret (ntable, slots) /- [hashmap::HashMap::{0}::move_elements] -/ def HashMap.move_elements_fwd_back - (T : Type) (ntable : hash_map_t T) (slots : Vec (list_t T)) (i : Usize) : - Result ((hash_map_t T) × (Vec (list_t T))) + (T : Type) (ntable : HashMap T) (slots : Vec (List T)) (i : Usize) : + Result ((HashMap T) × (Vec (List T))) := HashMap.move_elements_loop_fwd_back T ntable slots i /- [hashmap::HashMap::{0}::try_resize] -/ def HashMap.try_resize_fwd_back - (T : Type) (self : hash_map_t T) : Result (hash_map_t T) := + (T : Type) (self : HashMap T) : Result (HashMap T) := do let max_usize ← Scalar.cast .Usize core_num_u32_max_c - let capacity := vec_len (list_t T) self.hash_map_slots + let capacity := vec_len (List T) self.hash_map_slots let n1 ← max_usize / (Usize.ofInt 2 (by intlit)) let (i, i0) := self.hash_map_max_load_factor let i1 ← n1 / i @@ -212,8 +212,8 @@ def HashMap.try_resize_fwd_back /- [hashmap::HashMap::{0}::insert] -/ def HashMap.insert_fwd_back - (T : Type) (self : hash_map_t T) (key : Usize) (value : T) : - Result (hash_map_t T) + (T : Type) (self : HashMap T) (key : Usize) (value : T) : + Result (HashMap T) := do let self0 ← HashMap.insert_no_resize_fwd_back T self key value @@ -224,162 +224,159 @@ def HashMap.insert_fwd_back /- [hashmap::HashMap::{0}::contains_key_in_list] -/ divergent def HashMap.contains_key_in_list_loop_fwd - (T : Type) (key : Usize) (ls : list_t T) : Result Bool := + (T : Type) (key : Usize) (ls : List T) : Result Bool := match ls with - | list_t.Cons ckey t tl => + | List.Cons ckey t tl => if ckey = key then Result.ret true else HashMap.contains_key_in_list_loop_fwd T key tl - | list_t.Nil => Result.ret false + | List.Nil => Result.ret false /- [hashmap::HashMap::{0}::contains_key_in_list] -/ def HashMap.contains_key_in_list_fwd - (T : Type) (key : Usize) (ls : list_t T) : Result Bool := + (T : Type) (key : Usize) (ls : List T) : Result Bool := HashMap.contains_key_in_list_loop_fwd T key ls /- [hashmap::HashMap::{0}::contains_key] -/ def HashMap.contains_key_fwd - (T : Type) (self : hash_map_t T) (key : Usize) : Result Bool := + (T : Type) (self : HashMap T) (key : Usize) : Result Bool := do let hash ← hash_key_fwd key - let i := vec_len (list_t T) self.hash_map_slots + let i := vec_len (List T) self.hash_map_slots let hash_mod ← hash % i - let l ← vec_index_fwd (list_t T) self.hash_map_slots hash_mod + let l ← vec_index_fwd (List T) self.hash_map_slots hash_mod HashMap.contains_key_in_list_fwd T key l /- [hashmap::HashMap::{0}::get_in_list] -/ divergent def HashMap.get_in_list_loop_fwd - (T : Type) (key : Usize) (ls : list_t T) : Result T := + (T : Type) (key : Usize) (ls : List T) : Result T := match ls with - | list_t.Cons ckey cvalue tl => + | List.Cons ckey cvalue tl => if ckey = key then Result.ret cvalue else HashMap.get_in_list_loop_fwd T key tl - | list_t.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic /- [hashmap::HashMap::{0}::get_in_list] -/ def HashMap.get_in_list_fwd - (T : Type) (key : Usize) (ls : list_t T) : Result T := + (T : Type) (key : Usize) (ls : List T) : Result T := HashMap.get_in_list_loop_fwd T key ls /- [hashmap::HashMap::{0}::get] -/ -def HashMap.get_fwd - (T : Type) (self : hash_map_t T) (key : Usize) : Result T := +def HashMap.get_fwd (T : Type) (self : HashMap T) (key : Usize) : Result T := do let hash ← hash_key_fwd key - let i := vec_len (list_t T) self.hash_map_slots + let i := vec_len (List T) self.hash_map_slots let hash_mod ← hash % i - let l ← vec_index_fwd (list_t T) self.hash_map_slots hash_mod + let l ← vec_index_fwd (List T) self.hash_map_slots hash_mod HashMap.get_in_list_fwd T key l /- [hashmap::HashMap::{0}::get_mut_in_list] -/ divergent def HashMap.get_mut_in_list_loop_fwd - (T : Type) (ls : list_t T) (key : Usize) : Result T := + (T : Type) (ls : List T) (key : Usize) : Result T := match ls with - | list_t.Cons ckey cvalue tl => + | List.Cons ckey cvalue tl => if ckey = key then Result.ret cvalue else HashMap.get_mut_in_list_loop_fwd T tl key - | list_t.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic /- [hashmap::HashMap::{0}::get_mut_in_list] -/ def HashMap.get_mut_in_list_fwd - (T : Type) (ls : list_t T) (key : Usize) : Result T := + (T : Type) (ls : List T) (key : Usize) : Result T := HashMap.get_mut_in_list_loop_fwd T ls key /- [hashmap::HashMap::{0}::get_mut_in_list] -/ divergent def HashMap.get_mut_in_list_loop_back - (T : Type) (ls : list_t T) (key : Usize) (ret0 : T) : Result (list_t T) := + (T : Type) (ls : List T) (key : Usize) (ret0 : T) : Result (List T) := match ls with - | list_t.Cons ckey cvalue tl => + | List.Cons ckey cvalue tl => if ckey = key - then Result.ret (list_t.Cons ckey ret0 tl) + then Result.ret (List.Cons ckey ret0 tl) else do let tl0 ← HashMap.get_mut_in_list_loop_back T tl key ret0 - Result.ret (list_t.Cons ckey cvalue tl0) - | list_t.Nil => Result.fail Error.panic + Result.ret (List.Cons ckey cvalue tl0) + | List.Nil => Result.fail Error.panic /- [hashmap::HashMap::{0}::get_mut_in_list] -/ def HashMap.get_mut_in_list_back - (T : Type) (ls : list_t T) (key : Usize) (ret0 : T) : Result (list_t T) := + (T : Type) (ls : List T) (key : Usize) (ret0 : T) : Result (List T) := HashMap.get_mut_in_list_loop_back T ls key ret0 /- [hashmap::HashMap::{0}::get_mut] -/ def HashMap.get_mut_fwd - (T : Type) (self : hash_map_t T) (key : Usize) : Result T := + (T : Type) (self : HashMap T) (key : Usize) : Result T := do let hash ← hash_key_fwd key - let i := vec_len (list_t T) self.hash_map_slots + let i := vec_len (List T) self.hash_map_slots let hash_mod ← hash % i - let l ← vec_index_mut_fwd (list_t T) self.hash_map_slots hash_mod + let l ← vec_index_mut_fwd (List T) self.hash_map_slots hash_mod HashMap.get_mut_in_list_fwd T l key /- [hashmap::HashMap::{0}::get_mut] -/ def HashMap.get_mut_back - (T : Type) (self : hash_map_t T) (key : Usize) (ret0 : T) : - Result (hash_map_t T) + (T : Type) (self : HashMap T) (key : Usize) (ret0 : T) : + Result (HashMap T) := do let hash ← hash_key_fwd key - let i := vec_len (list_t T) self.hash_map_slots + let i := vec_len (List T) self.hash_map_slots let hash_mod ← hash % i - let l ← vec_index_mut_fwd (list_t T) self.hash_map_slots hash_mod + let l ← vec_index_mut_fwd (List T) self.hash_map_slots hash_mod let l0 ← HashMap.get_mut_in_list_back T l key ret0 - let v ← vec_index_mut_back (list_t T) self.hash_map_slots hash_mod l0 + let v ← vec_index_mut_back (List T) self.hash_map_slots hash_mod l0 Result.ret { self with hash_map_slots := v } /- [hashmap::HashMap::{0}::remove_from_list] -/ divergent def HashMap.remove_from_list_loop_fwd - (T : Type) (key : Usize) (ls : list_t T) : Result (Option T) := + (T : Type) (key : Usize) (ls : List T) : Result (Option T) := match ls with - | list_t.Cons ckey t tl => + | List.Cons ckey t tl => if ckey = key then - let mv_ls := - mem_replace_fwd (list_t T) (list_t.Cons ckey t tl) list_t.Nil + let mv_ls := mem_replace_fwd (List T) (List.Cons ckey t tl) List.Nil match mv_ls with - | list_t.Cons i cvalue tl0 => Result.ret (Option.some cvalue) - | list_t.Nil => Result.fail Error.panic + | List.Cons i cvalue tl0 => Result.ret (Option.some cvalue) + | List.Nil => Result.fail Error.panic else HashMap.remove_from_list_loop_fwd T key tl - | list_t.Nil => Result.ret Option.none + | List.Nil => Result.ret Option.none /- [hashmap::HashMap::{0}::remove_from_list] -/ def HashMap.remove_from_list_fwd - (T : Type) (key : Usize) (ls : list_t T) : Result (Option T) := + (T : Type) (key : Usize) (ls : List T) : Result (Option T) := HashMap.remove_from_list_loop_fwd T key ls /- [hashmap::HashMap::{0}::remove_from_list] -/ divergent def HashMap.remove_from_list_loop_back - (T : Type) (key : Usize) (ls : list_t T) : Result (list_t T) := + (T : Type) (key : Usize) (ls : List T) : Result (List T) := match ls with - | list_t.Cons ckey t tl => + | List.Cons ckey t tl => if ckey = key then - let mv_ls := - mem_replace_fwd (list_t T) (list_t.Cons ckey t tl) list_t.Nil + let mv_ls := mem_replace_fwd (List T) (List.Cons ckey t tl) List.Nil match mv_ls with - | list_t.Cons i cvalue tl0 => Result.ret tl0 - | list_t.Nil => Result.fail Error.panic + | List.Cons i cvalue tl0 => Result.ret tl0 + | List.Nil => Result.fail Error.panic else do let tl0 ← HashMap.remove_from_list_loop_back T key tl - Result.ret (list_t.Cons ckey t tl0) - | list_t.Nil => Result.ret list_t.Nil + Result.ret (List.Cons ckey t tl0) + | List.Nil => Result.ret List.Nil /- [hashmap::HashMap::{0}::remove_from_list] -/ def HashMap.remove_from_list_back - (T : Type) (key : Usize) (ls : list_t T) : Result (list_t T) := + (T : Type) (key : Usize) (ls : List T) : Result (List T) := HashMap.remove_from_list_loop_back T key ls /- [hashmap::HashMap::{0}::remove] -/ def HashMap.remove_fwd - (T : Type) (self : hash_map_t T) (key : Usize) : Result (Option T) := + (T : Type) (self : HashMap T) (key : Usize) : Result (Option T) := do let hash ← hash_key_fwd key - let i := vec_len (list_t T) self.hash_map_slots + let i := vec_len (List T) self.hash_map_slots let hash_mod ← hash % i - let l ← vec_index_mut_fwd (list_t T) self.hash_map_slots hash_mod + let l ← vec_index_mut_fwd (List T) self.hash_map_slots hash_mod let x ← HashMap.remove_from_list_fwd T key l match x with | Option.none => Result.ret Option.none @@ -390,24 +387,24 @@ def HashMap.remove_fwd /- [hashmap::HashMap::{0}::remove] -/ def HashMap.remove_back - (T : Type) (self : hash_map_t T) (key : Usize) : Result (hash_map_t T) := + (T : Type) (self : HashMap T) (key : Usize) : Result (HashMap T) := do let hash ← hash_key_fwd key - let i := vec_len (list_t T) self.hash_map_slots + let i := vec_len (List T) self.hash_map_slots let hash_mod ← hash % i - let l ← vec_index_mut_fwd (list_t T) self.hash_map_slots hash_mod + let l ← vec_index_mut_fwd (List T) self.hash_map_slots hash_mod let x ← HashMap.remove_from_list_fwd T key l match x with | Option.none => do let l0 ← HashMap.remove_from_list_back T key l - let v ← vec_index_mut_back (list_t T) self.hash_map_slots hash_mod l0 + let v ← vec_index_mut_back (List T) self.hash_map_slots hash_mod l0 Result.ret { self with hash_map_slots := v } | Option.some x0 => do let i0 ← self.hash_map_num_entries - (Usize.ofInt 1 (by intlit)) let l0 ← HashMap.remove_from_list_back T key l - let v ← vec_index_mut_back (list_t T) self.hash_map_slots hash_mod l0 + let v ← vec_index_mut_back (List T) self.hash_map_slots hash_mod l0 Result.ret { self with hash_map_num_entries := i0, hash_map_slots := v } diff --git a/tests/lean/Hashmap/Types.lean b/tests/lean/Hashmap/Types.lean index 75a6500f..7530f3b9 100644 --- a/tests/lean/Hashmap/Types.lean +++ b/tests/lean/Hashmap/Types.lean @@ -5,15 +5,15 @@ open Primitives namespace hashmap /- [hashmap::List] -/ -inductive list_t (T : Type) := -| Cons : Usize → T → list_t T → list_t T -| Nil : list_t T +inductive List (T : Type) := +| Cons : Usize → T → List T → List T +| Nil : List T /- [hashmap::HashMap] -/ -structure hash_map_t (T : Type) where +structure HashMap (T : Type) where hash_map_num_entries : Usize hash_map_max_load_factor : (Usize × Usize) hash_map_max_load : Usize - hash_map_slots : Vec (list_t T) + hash_map_slots : Vec (List T) end hashmap diff --git a/tests/lean/HashmapMain/Funs.lean b/tests/lean/HashmapMain/Funs.lean index 06929431..fd556878 100644 --- a/tests/lean/HashmapMain/Funs.lean +++ b/tests/lean/HashmapMain/Funs.lean @@ -12,21 +12,21 @@ def hashmap.hash_key_fwd (k : Usize) : Result Usize := /- [hashmap_main::hashmap::HashMap::{0}::allocate_slots] -/ divergent def hashmap.HashMap.allocate_slots_loop_fwd - (T : Type) (slots : Vec (hashmap_list_t T)) (n : Usize) : - Result (Vec (hashmap_list_t T)) + (T : Type) (slots : Vec (hashmap.List T)) (n : Usize) : + Result (Vec (hashmap.List T)) := if n > (Usize.ofInt 0 (by intlit)) then do - let slots0 ← vec_push_back (hashmap_list_t T) slots hashmap_list_t.Nil + let slots0 ← vec_push_back (hashmap.List T) slots hashmap.List.Nil let n0 ← n - (Usize.ofInt 1 (by intlit)) hashmap.HashMap.allocate_slots_loop_fwd T slots0 n0 else Result.ret slots /- [hashmap_main::hashmap::HashMap::{0}::allocate_slots] -/ def hashmap.HashMap.allocate_slots_fwd - (T : Type) (slots : Vec (hashmap_list_t T)) (n : Usize) : - Result (Vec (hashmap_list_t T)) + (T : Type) (slots : Vec (hashmap.List T)) (n : Usize) : + Result (Vec (hashmap.List T)) := hashmap.HashMap.allocate_slots_loop_fwd T slots n @@ -34,10 +34,10 @@ def hashmap.HashMap.allocate_slots_fwd def hashmap.HashMap.new_with_capacity_fwd (T : Type) (capacity : Usize) (max_load_dividend : Usize) (max_load_divisor : Usize) : - Result (hashmap_hash_map_t T) + Result (hashmap.HashMap T) := do - let v := vec_new (hashmap_list_t T) + let v := vec_new (hashmap.List T) let slots ← hashmap.HashMap.allocate_slots_fwd T v capacity let i ← capacity * max_load_dividend let i0 ← i / max_load_divisor @@ -51,28 +51,28 @@ def hashmap.HashMap.new_with_capacity_fwd } /- [hashmap_main::hashmap::HashMap::{0}::new] -/ -def hashmap.HashMap.new_fwd (T : Type) : Result (hashmap_hash_map_t T) := +def hashmap.HashMap.new_fwd (T : Type) : Result (hashmap.HashMap T) := hashmap.HashMap.new_with_capacity_fwd T (Usize.ofInt 32 (by intlit)) (Usize.ofInt 4 (by intlit)) (Usize.ofInt 5 (by intlit)) /- [hashmap_main::hashmap::HashMap::{0}::clear] -/ divergent def hashmap.HashMap.clear_loop_fwd_back - (T : Type) (slots : Vec (hashmap_list_t T)) (i : Usize) : - Result (Vec (hashmap_list_t T)) + (T : Type) (slots : Vec (hashmap.List T)) (i : Usize) : + Result (Vec (hashmap.List T)) := - let i0 := vec_len (hashmap_list_t T) slots + let i0 := vec_len (hashmap.List T) slots if i < i0 then do let i1 ← i + (Usize.ofInt 1 (by intlit)) let slots0 ← - vec_index_mut_back (hashmap_list_t T) slots i hashmap_list_t.Nil + vec_index_mut_back (hashmap.List T) slots i hashmap.List.Nil hashmap.HashMap.clear_loop_fwd_back T slots0 i1 else Result.ret slots /- [hashmap_main::hashmap::HashMap::{0}::clear] -/ def hashmap.HashMap.clear_fwd_back - (T : Type) (self : hashmap_hash_map_t T) : Result (hashmap_hash_map_t T) := + (T : Type) (self : hashmap.HashMap T) : Result (hashmap.HashMap T) := do let v ← hashmap.HashMap.clear_loop_fwd_back T self.hashmap_hash_map_slots @@ -87,59 +87,59 @@ def hashmap.HashMap.clear_fwd_back /- [hashmap_main::hashmap::HashMap::{0}::len] -/ def hashmap.HashMap.len_fwd - (T : Type) (self : hashmap_hash_map_t T) : Result Usize := + (T : Type) (self : hashmap.HashMap T) : Result Usize := Result.ret self.hashmap_hash_map_num_entries /- [hashmap_main::hashmap::HashMap::{0}::insert_in_list] -/ divergent def hashmap.HashMap.insert_in_list_loop_fwd - (T : Type) (key : Usize) (value : T) (ls : hashmap_list_t T) : Result Bool := + (T : Type) (key : Usize) (value : T) (ls : hashmap.List T) : Result Bool := match ls with - | hashmap_list_t.Cons ckey cvalue tl => + | hashmap.List.Cons ckey cvalue tl => if ckey = key then Result.ret false else hashmap.HashMap.insert_in_list_loop_fwd T key value tl - | hashmap_list_t.Nil => Result.ret true + | hashmap.List.Nil => Result.ret true /- [hashmap_main::hashmap::HashMap::{0}::insert_in_list] -/ def hashmap.HashMap.insert_in_list_fwd - (T : Type) (key : Usize) (value : T) (ls : hashmap_list_t T) : Result Bool := + (T : Type) (key : Usize) (value : T) (ls : hashmap.List T) : Result Bool := hashmap.HashMap.insert_in_list_loop_fwd T key value ls /- [hashmap_main::hashmap::HashMap::{0}::insert_in_list] -/ divergent def hashmap.HashMap.insert_in_list_loop_back - (T : Type) (key : Usize) (value : T) (ls : hashmap_list_t T) : - Result (hashmap_list_t T) + (T : Type) (key : Usize) (value : T) (ls : hashmap.List T) : + Result (hashmap.List T) := match ls with - | hashmap_list_t.Cons ckey cvalue tl => + | hashmap.List.Cons ckey cvalue tl => if ckey = key - then Result.ret (hashmap_list_t.Cons ckey value tl) + then Result.ret (hashmap.List.Cons ckey value tl) else do let tl0 ← hashmap.HashMap.insert_in_list_loop_back T key value tl - Result.ret (hashmap_list_t.Cons ckey cvalue tl0) - | hashmap_list_t.Nil => - let l := hashmap_list_t.Nil - Result.ret (hashmap_list_t.Cons key value l) + Result.ret (hashmap.List.Cons ckey cvalue tl0) + | hashmap.List.Nil => + let l := hashmap.List.Nil + Result.ret (hashmap.List.Cons key value l) /- [hashmap_main::hashmap::HashMap::{0}::insert_in_list] -/ def hashmap.HashMap.insert_in_list_back - (T : Type) (key : Usize) (value : T) (ls : hashmap_list_t T) : - Result (hashmap_list_t T) + (T : Type) (key : Usize) (value : T) (ls : hashmap.List T) : + Result (hashmap.List T) := hashmap.HashMap.insert_in_list_loop_back T key value ls /- [hashmap_main::hashmap::HashMap::{0}::insert_no_resize] -/ def hashmap.HashMap.insert_no_resize_fwd_back - (T : Type) (self : hashmap_hash_map_t T) (key : Usize) (value : T) : - Result (hashmap_hash_map_t T) + (T : Type) (self : hashmap.HashMap T) (key : Usize) (value : T) : + Result (hashmap.HashMap T) := do let hash ← hashmap.hash_key_fwd key - let i := vec_len (hashmap_list_t T) self.hashmap_hash_map_slots + let i := vec_len (hashmap.List T) self.hashmap_hash_map_slots let hash_mod ← hash % i let l ← - vec_index_mut_fwd (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod + vec_index_mut_fwd (hashmap.List T) self.hashmap_hash_map_slots hash_mod let inserted ← hashmap.HashMap.insert_in_list_fwd T key value l if inserted then @@ -148,7 +148,7 @@ def hashmap.HashMap.insert_no_resize_fwd_back (Usize.ofInt 1 (by intlit)) let l0 ← hashmap.HashMap.insert_in_list_back T key value l let v ← - vec_index_mut_back (hashmap_list_t T) self.hashmap_hash_map_slots + vec_index_mut_back (hashmap.List T) self.hashmap_hash_map_slots hash_mod l0 Result.ret { @@ -160,7 +160,7 @@ def hashmap.HashMap.insert_no_resize_fwd_back do let l0 ← hashmap.HashMap.insert_in_list_back T key value l let v ← - vec_index_mut_back (hashmap_list_t T) self.hashmap_hash_map_slots + vec_index_mut_back (hashmap.List T) self.hashmap_hash_map_slots hash_mod l0 Result.ret { self with hashmap_hash_map_slots := v } @@ -171,57 +171,57 @@ def core_num_u32_max_c : U32 := eval_global core_num_u32_max_body (by simp) /- [hashmap_main::hashmap::HashMap::{0}::move_elements_from_list] -/ divergent def hashmap.HashMap.move_elements_from_list_loop_fwd_back - (T : Type) (ntable : hashmap_hash_map_t T) (ls : hashmap_list_t T) : - Result (hashmap_hash_map_t T) + (T : Type) (ntable : hashmap.HashMap T) (ls : hashmap.List T) : + Result (hashmap.HashMap T) := match ls with - | hashmap_list_t.Cons k v tl => + | hashmap.List.Cons k v tl => do let ntable0 ← hashmap.HashMap.insert_no_resize_fwd_back T ntable k v hashmap.HashMap.move_elements_from_list_loop_fwd_back T ntable0 tl - | hashmap_list_t.Nil => Result.ret ntable + | hashmap.List.Nil => Result.ret ntable /- [hashmap_main::hashmap::HashMap::{0}::move_elements_from_list] -/ def hashmap.HashMap.move_elements_from_list_fwd_back - (T : Type) (ntable : hashmap_hash_map_t T) (ls : hashmap_list_t T) : - Result (hashmap_hash_map_t T) + (T : Type) (ntable : hashmap.HashMap T) (ls : hashmap.List T) : + Result (hashmap.HashMap T) := hashmap.HashMap.move_elements_from_list_loop_fwd_back T ntable ls /- [hashmap_main::hashmap::HashMap::{0}::move_elements] -/ divergent def hashmap.HashMap.move_elements_loop_fwd_back - (T : Type) (ntable : hashmap_hash_map_t T) (slots : Vec (hashmap_list_t T)) + (T : Type) (ntable : hashmap.HashMap T) (slots : Vec (hashmap.List T)) (i : Usize) : - Result ((hashmap_hash_map_t T) × (Vec (hashmap_list_t T))) + Result ((hashmap.HashMap T) × (Vec (hashmap.List T))) := - let i0 := vec_len (hashmap_list_t T) slots + let i0 := vec_len (hashmap.List T) slots if i < i0 then do - let l ← vec_index_mut_fwd (hashmap_list_t T) slots i - let ls := mem_replace_fwd (hashmap_list_t T) l hashmap_list_t.Nil + let l ← vec_index_mut_fwd (hashmap.List T) slots i + let ls := mem_replace_fwd (hashmap.List T) l hashmap.List.Nil let ntable0 ← hashmap.HashMap.move_elements_from_list_fwd_back T ntable ls let i1 ← i + (Usize.ofInt 1 (by intlit)) - let l0 := mem_replace_back (hashmap_list_t T) l hashmap_list_t.Nil - let slots0 ← vec_index_mut_back (hashmap_list_t T) slots i l0 + let l0 := mem_replace_back (hashmap.List T) l hashmap.List.Nil + let slots0 ← vec_index_mut_back (hashmap.List T) slots i l0 hashmap.HashMap.move_elements_loop_fwd_back T ntable0 slots0 i1 else Result.ret (ntable, slots) /- [hashmap_main::hashmap::HashMap::{0}::move_elements] -/ def hashmap.HashMap.move_elements_fwd_back - (T : Type) (ntable : hashmap_hash_map_t T) (slots : Vec (hashmap_list_t T)) + (T : Type) (ntable : hashmap.HashMap T) (slots : Vec (hashmap.List T)) (i : Usize) : - Result ((hashmap_hash_map_t T) × (Vec (hashmap_list_t T))) + Result ((hashmap.HashMap T) × (Vec (hashmap.List T))) := hashmap.HashMap.move_elements_loop_fwd_back T ntable slots i /- [hashmap_main::hashmap::HashMap::{0}::try_resize] -/ def hashmap.HashMap.try_resize_fwd_back - (T : Type) (self : hashmap_hash_map_t T) : Result (hashmap_hash_map_t T) := + (T : Type) (self : hashmap.HashMap T) : Result (hashmap.HashMap T) := do let max_usize ← Scalar.cast .Usize core_num_u32_max_c - let capacity := vec_len (hashmap_list_t T) self.hashmap_hash_map_slots + let capacity := vec_len (hashmap.List T) self.hashmap_hash_map_slots let n1 ← max_usize / (Usize.ofInt 2 (by intlit)) let (i, i0) := self.hashmap_hash_map_max_load_factor let i1 ← n1 / i @@ -244,8 +244,8 @@ def hashmap.HashMap.try_resize_fwd_back /- [hashmap_main::hashmap::HashMap::{0}::insert] -/ def hashmap.HashMap.insert_fwd_back - (T : Type) (self : hashmap_hash_map_t T) (key : Usize) (value : T) : - Result (hashmap_hash_map_t T) + (T : Type) (self : hashmap.HashMap T) (key : Usize) (value : T) : + Result (hashmap.HashMap T) := do let self0 ← hashmap.HashMap.insert_no_resize_fwd_back T self key value @@ -256,179 +256,175 @@ def hashmap.HashMap.insert_fwd_back /- [hashmap_main::hashmap::HashMap::{0}::contains_key_in_list] -/ divergent def hashmap.HashMap.contains_key_in_list_loop_fwd - (T : Type) (key : Usize) (ls : hashmap_list_t T) : Result Bool := + (T : Type) (key : Usize) (ls : hashmap.List T) : Result Bool := match ls with - | hashmap_list_t.Cons ckey t tl => + | hashmap.List.Cons ckey t tl => if ckey = key then Result.ret true else hashmap.HashMap.contains_key_in_list_loop_fwd T key tl - | hashmap_list_t.Nil => Result.ret false + | hashmap.List.Nil => Result.ret false /- [hashmap_main::hashmap::HashMap::{0}::contains_key_in_list] -/ def hashmap.HashMap.contains_key_in_list_fwd - (T : Type) (key : Usize) (ls : hashmap_list_t T) : Result Bool := + (T : Type) (key : Usize) (ls : hashmap.List T) : Result Bool := hashmap.HashMap.contains_key_in_list_loop_fwd T key ls /- [hashmap_main::hashmap::HashMap::{0}::contains_key] -/ def hashmap.HashMap.contains_key_fwd - (T : Type) (self : hashmap_hash_map_t T) (key : Usize) : Result Bool := + (T : Type) (self : hashmap.HashMap T) (key : Usize) : Result Bool := do let hash ← hashmap.hash_key_fwd key - let i := vec_len (hashmap_list_t T) self.hashmap_hash_map_slots + let i := vec_len (hashmap.List T) self.hashmap_hash_map_slots let hash_mod ← hash % i let l ← - vec_index_fwd (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod + vec_index_fwd (hashmap.List T) self.hashmap_hash_map_slots hash_mod hashmap.HashMap.contains_key_in_list_fwd T key l /- [hashmap_main::hashmap::HashMap::{0}::get_in_list] -/ divergent def hashmap.HashMap.get_in_list_loop_fwd - (T : Type) (key : Usize) (ls : hashmap_list_t T) : Result T := + (T : Type) (key : Usize) (ls : hashmap.List T) : Result T := match ls with - | hashmap_list_t.Cons ckey cvalue tl => + | hashmap.List.Cons ckey cvalue tl => if ckey = key then Result.ret cvalue else hashmap.HashMap.get_in_list_loop_fwd T key tl - | hashmap_list_t.Nil => Result.fail Error.panic + | hashmap.List.Nil => Result.fail Error.panic /- [hashmap_main::hashmap::HashMap::{0}::get_in_list] -/ def hashmap.HashMap.get_in_list_fwd - (T : Type) (key : Usize) (ls : hashmap_list_t T) : Result T := + (T : Type) (key : Usize) (ls : hashmap.List T) : Result T := hashmap.HashMap.get_in_list_loop_fwd T key ls /- [hashmap_main::hashmap::HashMap::{0}::get] -/ def hashmap.HashMap.get_fwd - (T : Type) (self : hashmap_hash_map_t T) (key : Usize) : Result T := + (T : Type) (self : hashmap.HashMap T) (key : Usize) : Result T := do let hash ← hashmap.hash_key_fwd key - let i := vec_len (hashmap_list_t T) self.hashmap_hash_map_slots + let i := vec_len (hashmap.List T) self.hashmap_hash_map_slots let hash_mod ← hash % i let l ← - vec_index_fwd (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod + vec_index_fwd (hashmap.List T) self.hashmap_hash_map_slots hash_mod hashmap.HashMap.get_in_list_fwd T key l /- [hashmap_main::hashmap::HashMap::{0}::get_mut_in_list] -/ divergent def hashmap.HashMap.get_mut_in_list_loop_fwd - (T : Type) (ls : hashmap_list_t T) (key : Usize) : Result T := + (T : Type) (ls : hashmap.List T) (key : Usize) : Result T := match ls with - | hashmap_list_t.Cons ckey cvalue tl => + | hashmap.List.Cons ckey cvalue tl => if ckey = key then Result.ret cvalue else hashmap.HashMap.get_mut_in_list_loop_fwd T tl key - | hashmap_list_t.Nil => Result.fail Error.panic + | hashmap.List.Nil => Result.fail Error.panic /- [hashmap_main::hashmap::HashMap::{0}::get_mut_in_list] -/ def hashmap.HashMap.get_mut_in_list_fwd - (T : Type) (ls : hashmap_list_t T) (key : Usize) : Result T := + (T : Type) (ls : hashmap.List T) (key : Usize) : Result T := hashmap.HashMap.get_mut_in_list_loop_fwd T ls key /- [hashmap_main::hashmap::HashMap::{0}::get_mut_in_list] -/ divergent def hashmap.HashMap.get_mut_in_list_loop_back - (T : Type) (ls : hashmap_list_t T) (key : Usize) (ret0 : T) : - Result (hashmap_list_t T) + (T : Type) (ls : hashmap.List T) (key : Usize) (ret0 : T) : + Result (hashmap.List T) := match ls with - | hashmap_list_t.Cons ckey cvalue tl => + | hashmap.List.Cons ckey cvalue tl => if ckey = key - then Result.ret (hashmap_list_t.Cons ckey ret0 tl) + then Result.ret (hashmap.List.Cons ckey ret0 tl) else do let tl0 ← hashmap.HashMap.get_mut_in_list_loop_back T tl key ret0 - Result.ret (hashmap_list_t.Cons ckey cvalue tl0) - | hashmap_list_t.Nil => Result.fail Error.panic + Result.ret (hashmap.List.Cons ckey cvalue tl0) + | hashmap.List.Nil => Result.fail Error.panic /- [hashmap_main::hashmap::HashMap::{0}::get_mut_in_list] -/ def hashmap.HashMap.get_mut_in_list_back - (T : Type) (ls : hashmap_list_t T) (key : Usize) (ret0 : T) : - Result (hashmap_list_t T) + (T : Type) (ls : hashmap.List T) (key : Usize) (ret0 : T) : + Result (hashmap.List T) := hashmap.HashMap.get_mut_in_list_loop_back T ls key ret0 /- [hashmap_main::hashmap::HashMap::{0}::get_mut] -/ def hashmap.HashMap.get_mut_fwd - (T : Type) (self : hashmap_hash_map_t T) (key : Usize) : Result T := + (T : Type) (self : hashmap.HashMap T) (key : Usize) : Result T := do let hash ← hashmap.hash_key_fwd key - let i := vec_len (hashmap_list_t T) self.hashmap_hash_map_slots + let i := vec_len (hashmap.List T) self.hashmap_hash_map_slots let hash_mod ← hash % i let l ← - vec_index_mut_fwd (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod + vec_index_mut_fwd (hashmap.List T) self.hashmap_hash_map_slots hash_mod hashmap.HashMap.get_mut_in_list_fwd T l key /- [hashmap_main::hashmap::HashMap::{0}::get_mut] -/ def hashmap.HashMap.get_mut_back - (T : Type) (self : hashmap_hash_map_t T) (key : Usize) (ret0 : T) : - Result (hashmap_hash_map_t T) + (T : Type) (self : hashmap.HashMap T) (key : Usize) (ret0 : T) : + Result (hashmap.HashMap T) := do let hash ← hashmap.hash_key_fwd key - let i := vec_len (hashmap_list_t T) self.hashmap_hash_map_slots + let i := vec_len (hashmap.List T) self.hashmap_hash_map_slots let hash_mod ← hash % i let l ← - vec_index_mut_fwd (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod + vec_index_mut_fwd (hashmap.List T) self.hashmap_hash_map_slots hash_mod let l0 ← hashmap.HashMap.get_mut_in_list_back T l key ret0 let v ← - vec_index_mut_back (hashmap_list_t T) self.hashmap_hash_map_slots - hash_mod l0 + vec_index_mut_back (hashmap.List T) self.hashmap_hash_map_slots hash_mod + l0 Result.ret { self with hashmap_hash_map_slots := v } /- [hashmap_main::hashmap::HashMap::{0}::remove_from_list] -/ divergent def hashmap.HashMap.remove_from_list_loop_fwd - (T : Type) (key : Usize) (ls : hashmap_list_t T) : Result (Option T) := + (T : Type) (key : Usize) (ls : hashmap.List T) : Result (Option T) := match ls with - | hashmap_list_t.Cons ckey t tl => + | hashmap.List.Cons ckey t tl => if ckey = key then let mv_ls := - mem_replace_fwd (hashmap_list_t T) (hashmap_list_t.Cons ckey t tl) - hashmap_list_t.Nil + mem_replace_fwd (hashmap.List T) (hashmap.List.Cons ckey t tl) + hashmap.List.Nil match mv_ls with - | hashmap_list_t.Cons i cvalue tl0 => Result.ret (Option.some cvalue) - | hashmap_list_t.Nil => Result.fail Error.panic + | hashmap.List.Cons i cvalue tl0 => Result.ret (Option.some cvalue) + | hashmap.List.Nil => Result.fail Error.panic else hashmap.HashMap.remove_from_list_loop_fwd T key tl - | hashmap_list_t.Nil => Result.ret Option.none + | hashmap.List.Nil => Result.ret Option.none /- [hashmap_main::hashmap::HashMap::{0}::remove_from_list] -/ def hashmap.HashMap.remove_from_list_fwd - (T : Type) (key : Usize) (ls : hashmap_list_t T) : Result (Option T) := + (T : Type) (key : Usize) (ls : hashmap.List T) : Result (Option T) := hashmap.HashMap.remove_from_list_loop_fwd T key ls /- [hashmap_main::hashmap::HashMap::{0}::remove_from_list] -/ divergent def hashmap.HashMap.remove_from_list_loop_back - (T : Type) (key : Usize) (ls : hashmap_list_t T) : - Result (hashmap_list_t T) - := + (T : Type) (key : Usize) (ls : hashmap.List T) : Result (hashmap.List T) := match ls with - | hashmap_list_t.Cons ckey t tl => + | hashmap.List.Cons ckey t tl => if ckey = key then let mv_ls := - mem_replace_fwd (hashmap_list_t T) (hashmap_list_t.Cons ckey t tl) - hashmap_list_t.Nil + mem_replace_fwd (hashmap.List T) (hashmap.List.Cons ckey t tl) + hashmap.List.Nil match mv_ls with - | hashmap_list_t.Cons i cvalue tl0 => Result.ret tl0 - | hashmap_list_t.Nil => Result.fail Error.panic + | hashmap.List.Cons i cvalue tl0 => Result.ret tl0 + | hashmap.List.Nil => Result.fail Error.panic else do let tl0 ← hashmap.HashMap.remove_from_list_loop_back T key tl - Result.ret (hashmap_list_t.Cons ckey t tl0) - | hashmap_list_t.Nil => Result.ret hashmap_list_t.Nil + Result.ret (hashmap.List.Cons ckey t tl0) + | hashmap.List.Nil => Result.ret hashmap.List.Nil /- [hashmap_main::hashmap::HashMap::{0}::remove_from_list] -/ def hashmap.HashMap.remove_from_list_back - (T : Type) (key : Usize) (ls : hashmap_list_t T) : - Result (hashmap_list_t T) - := + (T : Type) (key : Usize) (ls : hashmap.List T) : Result (hashmap.List T) := hashmap.HashMap.remove_from_list_loop_back T key ls /- [hashmap_main::hashmap::HashMap::{0}::remove] -/ def hashmap.HashMap.remove_fwd - (T : Type) (self : hashmap_hash_map_t T) (key : Usize) : Result (Option T) := + (T : Type) (self : hashmap.HashMap T) (key : Usize) : Result (Option T) := do let hash ← hashmap.hash_key_fwd key - let i := vec_len (hashmap_list_t T) self.hashmap_hash_map_slots + let i := vec_len (hashmap.List T) self.hashmap_hash_map_slots let hash_mod ← hash % i let l ← - vec_index_mut_fwd (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod + vec_index_mut_fwd (hashmap.List T) self.hashmap_hash_map_slots hash_mod let x ← hashmap.HashMap.remove_from_list_fwd T key l match x with | Option.none => Result.ret Option.none @@ -440,22 +436,22 @@ def hashmap.HashMap.remove_fwd /- [hashmap_main::hashmap::HashMap::{0}::remove] -/ def hashmap.HashMap.remove_back - (T : Type) (self : hashmap_hash_map_t T) (key : Usize) : - Result (hashmap_hash_map_t T) + (T : Type) (self : hashmap.HashMap T) (key : Usize) : + Result (hashmap.HashMap T) := do let hash ← hashmap.hash_key_fwd key - let i := vec_len (hashmap_list_t T) self.hashmap_hash_map_slots + let i := vec_len (hashmap.List T) self.hashmap_hash_map_slots let hash_mod ← hash % i let l ← - vec_index_mut_fwd (hashmap_list_t T) self.hashmap_hash_map_slots hash_mod + vec_index_mut_fwd (hashmap.List T) self.hashmap_hash_map_slots hash_mod let x ← hashmap.HashMap.remove_from_list_fwd T key l match x with | Option.none => do let l0 ← hashmap.HashMap.remove_from_list_back T key l let v ← - vec_index_mut_back (hashmap_list_t T) self.hashmap_hash_map_slots + vec_index_mut_back (hashmap.List T) self.hashmap_hash_map_slots hash_mod l0 Result.ret { self with hashmap_hash_map_slots := v } | Option.some x0 => @@ -464,7 +460,7 @@ def hashmap.HashMap.remove_back (Usize.ofInt 1 (by intlit)) let l0 ← hashmap.HashMap.remove_from_list_back T key l let v ← - vec_index_mut_back (hashmap_list_t T) self.hashmap_hash_map_slots + vec_index_mut_back (hashmap.List T) self.hashmap_hash_map_slots hash_mod l0 Result.ret { diff --git a/tests/lean/HashmapMain/FunsExternal.lean b/tests/lean/HashmapMain/FunsExternal.lean index d917b485..3689dd45 100644 --- a/tests/lean/HashmapMain/FunsExternal.lean +++ b/tests/lean/HashmapMain/FunsExternal.lean @@ -8,10 +8,10 @@ open hashmap_main /- [hashmap_main::hashmap_utils::deserialize] -/ def hashmap_utils.deserialize_fwd - : State → Result (State × (hashmap_hash_map_t U64)) := + : State → Result (State × (hashmap.HashMap U64)) := fun _ => .fail .panic /- [hashmap_main::hashmap_utils::serialize] -/ def hashmap_utils.serialize_fwd - : hashmap_hash_map_t U64 → State → Result (State × Unit) := + : hashmap.HashMap U64 → State → Result (State × Unit) := fun _ _ => .fail .panic diff --git a/tests/lean/HashmapMain/FunsExternal_Template.lean b/tests/lean/HashmapMain/FunsExternal_Template.lean index 86286373..8853b7fc 100644 --- a/tests/lean/HashmapMain/FunsExternal_Template.lean +++ b/tests/lean/HashmapMain/FunsExternal_Template.lean @@ -8,9 +8,9 @@ open hashmap_main /- [hashmap_main::hashmap_utils::deserialize] -/ axiom hashmap_utils.deserialize_fwd - : State → Result (State × (hashmap_hash_map_t U64)) + : State → Result (State × (hashmap.HashMap U64)) /- [hashmap_main::hashmap_utils::serialize] -/ axiom hashmap_utils.serialize_fwd - : hashmap_hash_map_t U64 → State → Result (State × Unit) + : hashmap.HashMap U64 → State → Result (State × Unit) diff --git a/tests/lean/HashmapMain/Types.lean b/tests/lean/HashmapMain/Types.lean index 16641146..ac195e3d 100644 --- a/tests/lean/HashmapMain/Types.lean +++ b/tests/lean/HashmapMain/Types.lean @@ -5,16 +5,16 @@ open Primitives namespace hashmap_main /- [hashmap_main::hashmap::List] -/ -inductive hashmap_list_t (T : Type) := -| Cons : Usize → T → hashmap_list_t T → hashmap_list_t T -| Nil : hashmap_list_t T +inductive hashmap.List (T : Type) := +| Cons : Usize → T → hashmap.List T → hashmap.List T +| Nil : hashmap.List T /- [hashmap_main::hashmap::HashMap] -/ -structure hashmap_hash_map_t (T : Type) where +structure hashmap.HashMap (T : Type) where hashmap_hash_map_num_entries : Usize hashmap_hash_map_max_load_factor : (Usize × Usize) hashmap_hash_map_max_load : Usize - hashmap_hash_map_slots : Vec (hashmap_list_t T) + hashmap_hash_map_slots : Vec (hashmap.List T) /- The state type used in the state-error monad -/ axiom State : Type diff --git a/tests/lean/Loops/Funs.lean b/tests/lean/Loops/Funs.lean index 694f5450..383bc819 100644 --- a/tests/lean/Loops/Funs.lean +++ b/tests/lean/Loops/Funs.lean @@ -68,179 +68,175 @@ def clear_fwd_back (v : Vec U32) : Result (Vec U32) := clear_loop_fwd_back v (Usize.ofInt 0 (by intlit)) /- [loops::list_mem] -/ -divergent def list_mem_loop_fwd (x : U32) (ls : list_t U32) : Result Bool := +divergent def list_mem_loop_fwd (x : U32) (ls : List U32) : Result Bool := match ls with - | list_t.Cons y tl => - if y = x - then Result.ret true - else list_mem_loop_fwd x tl - | list_t.Nil => Result.ret false + | List.Cons y tl => if y = x + then Result.ret true + else list_mem_loop_fwd x tl + | List.Nil => Result.ret false /- [loops::list_mem] -/ -def list_mem_fwd (x : U32) (ls : list_t U32) : Result Bool := +def list_mem_fwd (x : U32) (ls : List U32) : Result Bool := list_mem_loop_fwd x ls /- [loops::list_nth_mut_loop] -/ divergent def list_nth_mut_loop_loop_fwd - (T : Type) (ls : list_t T) (i : U32) : Result T := + (T : Type) (ls : List T) (i : U32) : Result T := match ls with - | list_t.Cons x tl => + | List.Cons x tl => if i = (U32.ofInt 0 (by intlit)) then Result.ret x else do let i0 ← i - (U32.ofInt 1 (by intlit)) list_nth_mut_loop_loop_fwd T tl i0 - | list_t.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic /- [loops::list_nth_mut_loop] -/ -def list_nth_mut_loop_fwd (T : Type) (ls : list_t T) (i : U32) : Result T := +def list_nth_mut_loop_fwd (T : Type) (ls : List T) (i : U32) : Result T := list_nth_mut_loop_loop_fwd T ls i /- [loops::list_nth_mut_loop] -/ divergent def list_nth_mut_loop_loop_back - (T : Type) (ls : list_t T) (i : U32) (ret0 : T) : Result (list_t T) := + (T : Type) (ls : List T) (i : U32) (ret0 : T) : Result (List T) := match ls with - | list_t.Cons x tl => + | List.Cons x tl => if i = (U32.ofInt 0 (by intlit)) - then Result.ret (list_t.Cons ret0 tl) + then Result.ret (List.Cons ret0 tl) else do let i0 ← i - (U32.ofInt 1 (by intlit)) let tl0 ← list_nth_mut_loop_loop_back T tl i0 ret0 - Result.ret (list_t.Cons x tl0) - | list_t.Nil => Result.fail Error.panic + Result.ret (List.Cons x tl0) + | List.Nil => Result.fail Error.panic /- [loops::list_nth_mut_loop] -/ def list_nth_mut_loop_back - (T : Type) (ls : list_t T) (i : U32) (ret0 : T) : Result (list_t T) := + (T : Type) (ls : List T) (i : U32) (ret0 : T) : Result (List T) := list_nth_mut_loop_loop_back T ls i ret0 /- [loops::list_nth_shared_loop] -/ divergent def list_nth_shared_loop_loop_fwd - (T : Type) (ls : list_t T) (i : U32) : Result T := + (T : Type) (ls : List T) (i : U32) : Result T := match ls with - | list_t.Cons x tl => + | List.Cons x tl => if i = (U32.ofInt 0 (by intlit)) then Result.ret x else do let i0 ← i - (U32.ofInt 1 (by intlit)) list_nth_shared_loop_loop_fwd T tl i0 - | list_t.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic /- [loops::list_nth_shared_loop] -/ -def list_nth_shared_loop_fwd (T : Type) (ls : list_t T) (i : U32) : Result T := +def list_nth_shared_loop_fwd (T : Type) (ls : List T) (i : U32) : Result T := list_nth_shared_loop_loop_fwd T ls i /- [loops::get_elem_mut] -/ divergent def get_elem_mut_loop_fwd - (x : Usize) (ls : list_t Usize) : Result Usize := + (x : Usize) (ls : List Usize) : Result Usize := match ls with - | list_t.Cons y tl => + | List.Cons y tl => if y = x then Result.ret y else get_elem_mut_loop_fwd x tl - | list_t.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic /- [loops::get_elem_mut] -/ -def get_elem_mut_fwd (slots : Vec (list_t Usize)) (x : Usize) : Result Usize := +def get_elem_mut_fwd (slots : Vec (List Usize)) (x : Usize) : Result Usize := do - let l ← - vec_index_mut_fwd (list_t Usize) slots (Usize.ofInt 0 (by intlit)) + let l ← vec_index_mut_fwd (List Usize) slots (Usize.ofInt 0 (by intlit)) get_elem_mut_loop_fwd x l /- [loops::get_elem_mut] -/ divergent def get_elem_mut_loop_back - (x : Usize) (ls : list_t Usize) (ret0 : Usize) : Result (list_t Usize) := + (x : Usize) (ls : List Usize) (ret0 : Usize) : Result (List Usize) := match ls with - | list_t.Cons y tl => + | List.Cons y tl => if y = x - then Result.ret (list_t.Cons ret0 tl) + then Result.ret (List.Cons ret0 tl) else do let tl0 ← get_elem_mut_loop_back x tl ret0 - Result.ret (list_t.Cons y tl0) - | list_t.Nil => Result.fail Error.panic + Result.ret (List.Cons y tl0) + | List.Nil => Result.fail Error.panic /- [loops::get_elem_mut] -/ def get_elem_mut_back - (slots : Vec (list_t Usize)) (x : Usize) (ret0 : Usize) : - Result (Vec (list_t Usize)) + (slots : Vec (List Usize)) (x : Usize) (ret0 : Usize) : + Result (Vec (List Usize)) := do - let l ← - vec_index_mut_fwd (list_t Usize) slots (Usize.ofInt 0 (by intlit)) + let l ← vec_index_mut_fwd (List Usize) slots (Usize.ofInt 0 (by intlit)) let l0 ← get_elem_mut_loop_back x l ret0 - vec_index_mut_back (list_t Usize) slots (Usize.ofInt 0 (by intlit)) l0 + vec_index_mut_back (List Usize) slots (Usize.ofInt 0 (by intlit)) l0 /- [loops::get_elem_shared] -/ divergent def get_elem_shared_loop_fwd - (x : Usize) (ls : list_t Usize) : Result Usize := + (x : Usize) (ls : List Usize) : Result Usize := match ls with - | list_t.Cons y tl => + | List.Cons y tl => if y = x then Result.ret y else get_elem_shared_loop_fwd x tl - | list_t.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic /- [loops::get_elem_shared] -/ def get_elem_shared_fwd - (slots : Vec (list_t Usize)) (x : Usize) : Result Usize := + (slots : Vec (List Usize)) (x : Usize) : Result Usize := do - let l ← vec_index_fwd (list_t Usize) slots (Usize.ofInt 0 (by intlit)) + let l ← vec_index_fwd (List Usize) slots (Usize.ofInt 0 (by intlit)) get_elem_shared_loop_fwd x l /- [loops::id_mut] -/ -def id_mut_fwd (T : Type) (ls : list_t T) : Result (list_t T) := +def id_mut_fwd (T : Type) (ls : List T) : Result (List T) := Result.ret ls /- [loops::id_mut] -/ -def id_mut_back - (T : Type) (ls : list_t T) (ret0 : list_t T) : Result (list_t T) := +def id_mut_back (T : Type) (ls : List T) (ret0 : List T) : Result (List T) := Result.ret ret0 /- [loops::id_shared] -/ -def id_shared_fwd (T : Type) (ls : list_t T) : Result (list_t T) := +def id_shared_fwd (T : Type) (ls : List T) : Result (List T) := Result.ret ls /- [loops::list_nth_mut_loop_with_id] -/ divergent def list_nth_mut_loop_with_id_loop_fwd - (T : Type) (i : U32) (ls : list_t T) : Result T := + (T : Type) (i : U32) (ls : List T) : Result T := match ls with - | list_t.Cons x tl => + | List.Cons x tl => if i = (U32.ofInt 0 (by intlit)) then Result.ret x else do let i0 ← i - (U32.ofInt 1 (by intlit)) list_nth_mut_loop_with_id_loop_fwd T i0 tl - | list_t.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic /- [loops::list_nth_mut_loop_with_id] -/ def list_nth_mut_loop_with_id_fwd - (T : Type) (ls : list_t T) (i : U32) : Result T := + (T : Type) (ls : List T) (i : U32) : Result T := do let ls0 ← id_mut_fwd T ls list_nth_mut_loop_with_id_loop_fwd T i ls0 /- [loops::list_nth_mut_loop_with_id] -/ divergent def list_nth_mut_loop_with_id_loop_back - (T : Type) (i : U32) (ls : list_t T) (ret0 : T) : Result (list_t T) := + (T : Type) (i : U32) (ls : List T) (ret0 : T) : Result (List T) := match ls with - | list_t.Cons x tl => + | List.Cons x tl => if i = (U32.ofInt 0 (by intlit)) - then Result.ret (list_t.Cons ret0 tl) + then Result.ret (List.Cons ret0 tl) else do let i0 ← i - (U32.ofInt 1 (by intlit)) let tl0 ← list_nth_mut_loop_with_id_loop_back T i0 tl ret0 - Result.ret (list_t.Cons x tl0) - | list_t.Nil => Result.fail Error.panic + Result.ret (List.Cons x tl0) + | List.Nil => Result.fail Error.panic /- [loops::list_nth_mut_loop_with_id] -/ def list_nth_mut_loop_with_id_back - (T : Type) (ls : list_t T) (i : U32) (ret0 : T) : Result (list_t T) := + (T : Type) (ls : List T) (i : U32) (ret0 : T) : Result (List T) := do let ls0 ← id_mut_fwd T ls let l ← list_nth_mut_loop_with_id_loop_back T i ls0 ret0 @@ -248,378 +244,377 @@ def list_nth_mut_loop_with_id_back /- [loops::list_nth_shared_loop_with_id] -/ divergent def list_nth_shared_loop_with_id_loop_fwd - (T : Type) (i : U32) (ls : list_t T) : Result T := + (T : Type) (i : U32) (ls : List T) : Result T := match ls with - | list_t.Cons x tl => + | List.Cons x tl => if i = (U32.ofInt 0 (by intlit)) then Result.ret x else do let i0 ← i - (U32.ofInt 1 (by intlit)) list_nth_shared_loop_with_id_loop_fwd T i0 tl - | list_t.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic /- [loops::list_nth_shared_loop_with_id] -/ def list_nth_shared_loop_with_id_fwd - (T : Type) (ls : list_t T) (i : U32) : Result T := + (T : Type) (ls : List T) (i : U32) : Result T := do let ls0 ← id_shared_fwd T ls list_nth_shared_loop_with_id_loop_fwd T i ls0 /- [loops::list_nth_mut_loop_pair] -/ divergent def list_nth_mut_loop_pair_loop_fwd - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := + (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) : Result (T × T) := match ls0 with - | list_t.Cons x0 tl0 => + | List.Cons x0 tl0 => match ls1 with - | list_t.Cons x1 tl1 => + | List.Cons x1 tl1 => if i = (U32.ofInt 0 (by intlit)) then Result.ret (x0, x1) else do let i0 ← i - (U32.ofInt 1 (by intlit)) list_nth_mut_loop_pair_loop_fwd T tl0 tl1 i0 - | list_t.Nil => Result.fail Error.panic - | list_t.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic /- [loops::list_nth_mut_loop_pair] -/ def list_nth_mut_loop_pair_fwd - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := + (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) : Result (T × T) := list_nth_mut_loop_pair_loop_fwd T ls0 ls1 i /- [loops::list_nth_mut_loop_pair] -/ divergent def list_nth_mut_loop_pair_loop_back'a - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : - Result (list_t T) + (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) (ret0 : T) : + Result (List T) := match ls0 with - | list_t.Cons x0 tl0 => + | List.Cons x0 tl0 => match ls1 with - | list_t.Cons x1 tl1 => + | List.Cons x1 tl1 => if i = (U32.ofInt 0 (by intlit)) - then Result.ret (list_t.Cons ret0 tl0) + then Result.ret (List.Cons ret0 tl0) else do let i0 ← i - (U32.ofInt 1 (by intlit)) let tl00 ← list_nth_mut_loop_pair_loop_back'a T tl0 tl1 i0 ret0 - Result.ret (list_t.Cons x0 tl00) - | list_t.Nil => Result.fail Error.panic - | list_t.Nil => Result.fail Error.panic + Result.ret (List.Cons x0 tl00) + | List.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic /- [loops::list_nth_mut_loop_pair] -/ def list_nth_mut_loop_pair_back'a - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : - Result (list_t T) + (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) (ret0 : T) : + Result (List T) := list_nth_mut_loop_pair_loop_back'a T ls0 ls1 i ret0 /- [loops::list_nth_mut_loop_pair] -/ divergent def list_nth_mut_loop_pair_loop_back'b - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : - Result (list_t T) + (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) (ret0 : T) : + Result (List T) := match ls0 with - | list_t.Cons x0 tl0 => + | List.Cons x0 tl0 => match ls1 with - | list_t.Cons x1 tl1 => + | List.Cons x1 tl1 => if i = (U32.ofInt 0 (by intlit)) - then Result.ret (list_t.Cons ret0 tl1) + then Result.ret (List.Cons ret0 tl1) else do let i0 ← i - (U32.ofInt 1 (by intlit)) let tl10 ← list_nth_mut_loop_pair_loop_back'b T tl0 tl1 i0 ret0 - Result.ret (list_t.Cons x1 tl10) - | list_t.Nil => Result.fail Error.panic - | list_t.Nil => Result.fail Error.panic + Result.ret (List.Cons x1 tl10) + | List.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic /- [loops::list_nth_mut_loop_pair] -/ def list_nth_mut_loop_pair_back'b - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : - Result (list_t T) + (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) (ret0 : T) : + Result (List T) := list_nth_mut_loop_pair_loop_back'b T ls0 ls1 i ret0 /- [loops::list_nth_shared_loop_pair] -/ divergent def list_nth_shared_loop_pair_loop_fwd - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := + (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) : Result (T × T) := match ls0 with - | list_t.Cons x0 tl0 => + | List.Cons x0 tl0 => match ls1 with - | list_t.Cons x1 tl1 => + | List.Cons x1 tl1 => if i = (U32.ofInt 0 (by intlit)) then Result.ret (x0, x1) else do let i0 ← i - (U32.ofInt 1 (by intlit)) list_nth_shared_loop_pair_loop_fwd T tl0 tl1 i0 - | list_t.Nil => Result.fail Error.panic - | list_t.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic /- [loops::list_nth_shared_loop_pair] -/ def list_nth_shared_loop_pair_fwd - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := + (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) : Result (T × T) := list_nth_shared_loop_pair_loop_fwd T ls0 ls1 i /- [loops::list_nth_mut_loop_pair_merge] -/ divergent def list_nth_mut_loop_pair_merge_loop_fwd - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := + (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) : Result (T × T) := match ls0 with - | list_t.Cons x0 tl0 => + | List.Cons x0 tl0 => match ls1 with - | list_t.Cons x1 tl1 => + | List.Cons x1 tl1 => if i = (U32.ofInt 0 (by intlit)) then Result.ret (x0, x1) else do let i0 ← i - (U32.ofInt 1 (by intlit)) list_nth_mut_loop_pair_merge_loop_fwd T tl0 tl1 i0 - | list_t.Nil => Result.fail Error.panic - | list_t.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic /- [loops::list_nth_mut_loop_pair_merge] -/ def list_nth_mut_loop_pair_merge_fwd - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := + (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) : Result (T × T) := list_nth_mut_loop_pair_merge_loop_fwd T ls0 ls1 i /- [loops::list_nth_mut_loop_pair_merge] -/ divergent def list_nth_mut_loop_pair_merge_loop_back - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : (T × T)) : - Result ((list_t T) × (list_t T)) + (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) (ret0 : (T × T)) : + Result ((List T) × (List T)) := match ls0 with - | list_t.Cons x0 tl0 => + | List.Cons x0 tl0 => match ls1 with - | list_t.Cons x1 tl1 => + | List.Cons x1 tl1 => if i = (U32.ofInt 0 (by intlit)) - then - let (t, t0) := ret0 - Result.ret (list_t.Cons t tl0, list_t.Cons t0 tl1) + then let (t, t0) := ret0 + Result.ret (List.Cons t tl0, List.Cons t0 tl1) else do let i0 ← i - (U32.ofInt 1 (by intlit)) let (tl00, tl10) ← list_nth_mut_loop_pair_merge_loop_back T tl0 tl1 i0 ret0 - Result.ret (list_t.Cons x0 tl00, list_t.Cons x1 tl10) - | list_t.Nil => Result.fail Error.panic - | list_t.Nil => Result.fail Error.panic + Result.ret (List.Cons x0 tl00, List.Cons x1 tl10) + | List.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic /- [loops::list_nth_mut_loop_pair_merge] -/ def list_nth_mut_loop_pair_merge_back - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : (T × T)) : - Result ((list_t T) × (list_t T)) + (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) (ret0 : (T × T)) : + Result ((List T) × (List T)) := list_nth_mut_loop_pair_merge_loop_back T ls0 ls1 i ret0 /- [loops::list_nth_shared_loop_pair_merge] -/ divergent def list_nth_shared_loop_pair_merge_loop_fwd - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := + (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) : Result (T × T) := match ls0 with - | list_t.Cons x0 tl0 => + | List.Cons x0 tl0 => match ls1 with - | list_t.Cons x1 tl1 => + | List.Cons x1 tl1 => if i = (U32.ofInt 0 (by intlit)) then Result.ret (x0, x1) else do let i0 ← i - (U32.ofInt 1 (by intlit)) list_nth_shared_loop_pair_merge_loop_fwd T tl0 tl1 i0 - | list_t.Nil => Result.fail Error.panic - | list_t.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic /- [loops::list_nth_shared_loop_pair_merge] -/ def list_nth_shared_loop_pair_merge_fwd - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := + (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) : Result (T × T) := list_nth_shared_loop_pair_merge_loop_fwd T ls0 ls1 i /- [loops::list_nth_mut_shared_loop_pair] -/ divergent def list_nth_mut_shared_loop_pair_loop_fwd - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := + (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) : Result (T × T) := match ls0 with - | list_t.Cons x0 tl0 => + | List.Cons x0 tl0 => match ls1 with - | list_t.Cons x1 tl1 => + | List.Cons x1 tl1 => if i = (U32.ofInt 0 (by intlit)) then Result.ret (x0, x1) else do let i0 ← i - (U32.ofInt 1 (by intlit)) list_nth_mut_shared_loop_pair_loop_fwd T tl0 tl1 i0 - | list_t.Nil => Result.fail Error.panic - | list_t.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic /- [loops::list_nth_mut_shared_loop_pair] -/ def list_nth_mut_shared_loop_pair_fwd - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := + (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) : Result (T × T) := list_nth_mut_shared_loop_pair_loop_fwd T ls0 ls1 i /- [loops::list_nth_mut_shared_loop_pair] -/ divergent def list_nth_mut_shared_loop_pair_loop_back - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : - Result (list_t T) + (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) (ret0 : T) : + Result (List T) := match ls0 with - | list_t.Cons x0 tl0 => + | List.Cons x0 tl0 => match ls1 with - | list_t.Cons x1 tl1 => + | List.Cons x1 tl1 => if i = (U32.ofInt 0 (by intlit)) - then Result.ret (list_t.Cons ret0 tl0) + then Result.ret (List.Cons ret0 tl0) else do let i0 ← i - (U32.ofInt 1 (by intlit)) let tl00 ← list_nth_mut_shared_loop_pair_loop_back T tl0 tl1 i0 ret0 - Result.ret (list_t.Cons x0 tl00) - | list_t.Nil => Result.fail Error.panic - | list_t.Nil => Result.fail Error.panic + Result.ret (List.Cons x0 tl00) + | List.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic /- [loops::list_nth_mut_shared_loop_pair] -/ def list_nth_mut_shared_loop_pair_back - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : - Result (list_t T) + (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) (ret0 : T) : + Result (List T) := list_nth_mut_shared_loop_pair_loop_back T ls0 ls1 i ret0 /- [loops::list_nth_mut_shared_loop_pair_merge] -/ divergent def list_nth_mut_shared_loop_pair_merge_loop_fwd - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := + (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) : Result (T × T) := match ls0 with - | list_t.Cons x0 tl0 => + | List.Cons x0 tl0 => match ls1 with - | list_t.Cons x1 tl1 => + | List.Cons x1 tl1 => if i = (U32.ofInt 0 (by intlit)) then Result.ret (x0, x1) else do let i0 ← i - (U32.ofInt 1 (by intlit)) list_nth_mut_shared_loop_pair_merge_loop_fwd T tl0 tl1 i0 - | list_t.Nil => Result.fail Error.panic - | list_t.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic /- [loops::list_nth_mut_shared_loop_pair_merge] -/ def list_nth_mut_shared_loop_pair_merge_fwd - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := + (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) : Result (T × T) := list_nth_mut_shared_loop_pair_merge_loop_fwd T ls0 ls1 i /- [loops::list_nth_mut_shared_loop_pair_merge] -/ divergent def list_nth_mut_shared_loop_pair_merge_loop_back - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : - Result (list_t T) + (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) (ret0 : T) : + Result (List T) := match ls0 with - | list_t.Cons x0 tl0 => + | List.Cons x0 tl0 => match ls1 with - | list_t.Cons x1 tl1 => + | List.Cons x1 tl1 => if i = (U32.ofInt 0 (by intlit)) - then Result.ret (list_t.Cons ret0 tl0) + then Result.ret (List.Cons ret0 tl0) else do let i0 ← i - (U32.ofInt 1 (by intlit)) let tl00 ← list_nth_mut_shared_loop_pair_merge_loop_back T tl0 tl1 i0 ret0 - Result.ret (list_t.Cons x0 tl00) - | list_t.Nil => Result.fail Error.panic - | list_t.Nil => Result.fail Error.panic + Result.ret (List.Cons x0 tl00) + | List.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic /- [loops::list_nth_mut_shared_loop_pair_merge] -/ def list_nth_mut_shared_loop_pair_merge_back - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : - Result (list_t T) + (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) (ret0 : T) : + Result (List T) := list_nth_mut_shared_loop_pair_merge_loop_back T ls0 ls1 i ret0 /- [loops::list_nth_shared_mut_loop_pair] -/ divergent def list_nth_shared_mut_loop_pair_loop_fwd - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := + (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) : Result (T × T) := match ls0 with - | list_t.Cons x0 tl0 => + | List.Cons x0 tl0 => match ls1 with - | list_t.Cons x1 tl1 => + | List.Cons x1 tl1 => if i = (U32.ofInt 0 (by intlit)) then Result.ret (x0, x1) else do let i0 ← i - (U32.ofInt 1 (by intlit)) list_nth_shared_mut_loop_pair_loop_fwd T tl0 tl1 i0 - | list_t.Nil => Result.fail Error.panic - | list_t.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic /- [loops::list_nth_shared_mut_loop_pair] -/ def list_nth_shared_mut_loop_pair_fwd - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := + (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) : Result (T × T) := list_nth_shared_mut_loop_pair_loop_fwd T ls0 ls1 i /- [loops::list_nth_shared_mut_loop_pair] -/ divergent def list_nth_shared_mut_loop_pair_loop_back - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : - Result (list_t T) + (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) (ret0 : T) : + Result (List T) := match ls0 with - | list_t.Cons x0 tl0 => + | List.Cons x0 tl0 => match ls1 with - | list_t.Cons x1 tl1 => + | List.Cons x1 tl1 => if i = (U32.ofInt 0 (by intlit)) - then Result.ret (list_t.Cons ret0 tl1) + then Result.ret (List.Cons ret0 tl1) else do let i0 ← i - (U32.ofInt 1 (by intlit)) let tl10 ← list_nth_shared_mut_loop_pair_loop_back T tl0 tl1 i0 ret0 - Result.ret (list_t.Cons x1 tl10) - | list_t.Nil => Result.fail Error.panic - | list_t.Nil => Result.fail Error.panic + Result.ret (List.Cons x1 tl10) + | List.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic /- [loops::list_nth_shared_mut_loop_pair] -/ def list_nth_shared_mut_loop_pair_back - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : - Result (list_t T) + (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) (ret0 : T) : + Result (List T) := list_nth_shared_mut_loop_pair_loop_back T ls0 ls1 i ret0 /- [loops::list_nth_shared_mut_loop_pair_merge] -/ divergent def list_nth_shared_mut_loop_pair_merge_loop_fwd - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := + (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) : Result (T × T) := match ls0 with - | list_t.Cons x0 tl0 => + | List.Cons x0 tl0 => match ls1 with - | list_t.Cons x1 tl1 => + | List.Cons x1 tl1 => if i = (U32.ofInt 0 (by intlit)) then Result.ret (x0, x1) else do let i0 ← i - (U32.ofInt 1 (by intlit)) list_nth_shared_mut_loop_pair_merge_loop_fwd T tl0 tl1 i0 - | list_t.Nil => Result.fail Error.panic - | list_t.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic /- [loops::list_nth_shared_mut_loop_pair_merge] -/ def list_nth_shared_mut_loop_pair_merge_fwd - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) : Result (T × T) := + (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) : Result (T × T) := list_nth_shared_mut_loop_pair_merge_loop_fwd T ls0 ls1 i /- [loops::list_nth_shared_mut_loop_pair_merge] -/ divergent def list_nth_shared_mut_loop_pair_merge_loop_back - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : - Result (list_t T) + (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) (ret0 : T) : + Result (List T) := match ls0 with - | list_t.Cons x0 tl0 => + | List.Cons x0 tl0 => match ls1 with - | list_t.Cons x1 tl1 => + | List.Cons x1 tl1 => if i = (U32.ofInt 0 (by intlit)) - then Result.ret (list_t.Cons ret0 tl1) + then Result.ret (List.Cons ret0 tl1) else do let i0 ← i - (U32.ofInt 1 (by intlit)) let tl10 ← list_nth_shared_mut_loop_pair_merge_loop_back T tl0 tl1 i0 ret0 - Result.ret (list_t.Cons x1 tl10) - | list_t.Nil => Result.fail Error.panic - | list_t.Nil => Result.fail Error.panic + Result.ret (List.Cons x1 tl10) + | List.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic /- [loops::list_nth_shared_mut_loop_pair_merge] -/ def list_nth_shared_mut_loop_pair_merge_back - (T : Type) (ls0 : list_t T) (ls1 : list_t T) (i : U32) (ret0 : T) : - Result (list_t T) + (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) (ret0 : T) : + Result (List T) := list_nth_shared_mut_loop_pair_merge_loop_back T ls0 ls1 i ret0 diff --git a/tests/lean/Loops/Types.lean b/tests/lean/Loops/Types.lean index 5b5ed31f..f8bc193b 100644 --- a/tests/lean/Loops/Types.lean +++ b/tests/lean/Loops/Types.lean @@ -5,8 +5,8 @@ open Primitives namespace loops /- [loops::List] -/ -inductive list_t (T : Type) := -| Cons : T → list_t T → list_t T -| Nil : list_t T +inductive List (T : Type) := +| Cons : T → List T → List T +| Nil : List T end loops diff --git a/tests/lean/NoNestedBorrows.lean b/tests/lean/NoNestedBorrows.lean index 67abc8f6..cdbbcd67 100644 --- a/tests/lean/NoNestedBorrows.lean +++ b/tests/lean/NoNestedBorrows.lean @@ -5,35 +5,35 @@ open Primitives namespace no_nested_borrows /- [no_nested_borrows::Pair] -/ -structure pair_t (T1 T2 : Type) where +structure Pair (T1 T2 : Type) where pair_x : T1 pair_y : T2 /- [no_nested_borrows::List] -/ -inductive list_t (T : Type) := -| Cons : T → list_t T → list_t T -| Nil : list_t T +inductive List (T : Type) := +| Cons : T → List T → List T +| Nil : List T /- [no_nested_borrows::One] -/ -inductive one_t (T1 : Type) := -| One : T1 → one_t T1 +inductive One (T1 : Type) := +| One : T1 → One T1 /- [no_nested_borrows::EmptyEnum] -/ -inductive empty_enum_t := -| Empty : empty_enum_t +inductive EmptyEnum := +| Empty : EmptyEnum /- [no_nested_borrows::Enum] -/ -inductive enum_t := -| Variant1 : enum_t -| Variant2 : enum_t +inductive Enum := +| Variant1 : Enum +| Variant2 : Enum /- [no_nested_borrows::EmptyStruct] -/ -structure empty_struct_t where +structure EmptyStruct where /- [no_nested_borrows::Sum] -/ -inductive sum_t (T1 T2 : Type) := -| Left : T1 → sum_t T1 T2 -| Right : T2 → sum_t T1 T2 +inductive Sum (T1 T2 : Type) := +| Left : T1 → Sum T1 T2 +| Right : T2 → Sum T1 T2 /- [no_nested_borrows::neg_test] -/ def neg_test_fwd (x : I32) : Result I32 := @@ -175,16 +175,16 @@ def test_copy_int_fwd : Result Unit := #assert (test_copy_int_fwd == .ret ()) /- [no_nested_borrows::is_cons] -/ -def is_cons_fwd (T : Type) (l : list_t T) : Result Bool := +def is_cons_fwd (T : Type) (l : List T) : Result Bool := match l with - | list_t.Cons t l0 => Result.ret true - | list_t.Nil => Result.ret false + | List.Cons t l0 => Result.ret true + | List.Nil => Result.ret false /- [no_nested_borrows::test_is_cons] -/ def test_is_cons_fwd : Result Unit := do - let l := list_t.Nil - let b ← is_cons_fwd I32 (list_t.Cons (I32.ofInt 0 (by intlit)) l) + let l := List.Nil + let b ← is_cons_fwd I32 (List.Cons (I32.ofInt 0 (by intlit)) l) if not b then Result.fail Error.panic else Result.ret () @@ -193,16 +193,16 @@ def test_is_cons_fwd : Result Unit := #assert (test_is_cons_fwd == .ret ()) /- [no_nested_borrows::split_list] -/ -def split_list_fwd (T : Type) (l : list_t T) : Result (T × (list_t T)) := +def split_list_fwd (T : Type) (l : List T) : Result (T × (List T)) := match l with - | list_t.Cons hd tl => Result.ret (hd, tl) - | list_t.Nil => Result.fail Error.panic + | List.Cons hd tl => Result.ret (hd, tl) + | List.Nil => Result.fail Error.panic /- [no_nested_borrows::test_split_list] -/ def test_split_list_fwd : Result Unit := do - let l := list_t.Nil - let p ← split_list_fwd I32 (list_t.Cons (I32.ofInt 0 (by intlit)) l) + let l := List.Nil + let p ← split_list_fwd I32 (List.Cons (I32.ofInt 0 (by intlit)) l) let (hd, _) := p if not (hd = (I32.ofInt 0 (by intlit))) then Result.fail Error.panic @@ -254,111 +254,110 @@ def test_char_fwd : Result Char := mutual /- [no_nested_borrows::NodeElem] -/ -inductive node_elem_t (T : Type) := -| Cons : tree_t T → node_elem_t T → node_elem_t T -| Nil : node_elem_t T +inductive NodeElem (T : Type) := +| Cons : Tree T → NodeElem T → NodeElem T +| Nil : NodeElem T /- [no_nested_borrows::Tree] -/ -inductive tree_t (T : Type) := -| Leaf : T → tree_t T -| Node : T → node_elem_t T → tree_t T → tree_t T +inductive Tree (T : Type) := +| Leaf : T → Tree T +| Node : T → NodeElem T → Tree T → Tree T end /- [no_nested_borrows::list_length] -/ -divergent def list_length_fwd (T : Type) (l : list_t T) : Result U32 := +divergent def list_length_fwd (T : Type) (l : List T) : Result U32 := match l with - | list_t.Cons t l1 => + | List.Cons t l1 => do let i ← list_length_fwd T l1 (U32.ofInt 1 (by intlit)) + i - | list_t.Nil => Result.ret (U32.ofInt 0 (by intlit)) + | List.Nil => Result.ret (U32.ofInt 0 (by intlit)) /- [no_nested_borrows::list_nth_shared] -/ divergent def list_nth_shared_fwd - (T : Type) (l : list_t T) (i : U32) : Result T := + (T : Type) (l : List T) (i : U32) : Result T := match l with - | list_t.Cons x tl => + | List.Cons x tl => if i = (U32.ofInt 0 (by intlit)) then Result.ret x else do let i0 ← i - (U32.ofInt 1 (by intlit)) list_nth_shared_fwd T tl i0 - | list_t.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic /- [no_nested_borrows::list_nth_mut] -/ -divergent def list_nth_mut_fwd - (T : Type) (l : list_t T) (i : U32) : Result T := +divergent def list_nth_mut_fwd (T : Type) (l : List T) (i : U32) : Result T := match l with - | list_t.Cons x tl => + | List.Cons x tl => if i = (U32.ofInt 0 (by intlit)) then Result.ret x else do let i0 ← i - (U32.ofInt 1 (by intlit)) list_nth_mut_fwd T tl i0 - | list_t.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic /- [no_nested_borrows::list_nth_mut] -/ divergent def list_nth_mut_back - (T : Type) (l : list_t T) (i : U32) (ret0 : T) : Result (list_t T) := + (T : Type) (l : List T) (i : U32) (ret0 : T) : Result (List T) := match l with - | list_t.Cons x tl => + | List.Cons x tl => if i = (U32.ofInt 0 (by intlit)) - then Result.ret (list_t.Cons ret0 tl) + then Result.ret (List.Cons ret0 tl) else do let i0 ← i - (U32.ofInt 1 (by intlit)) let tl0 ← list_nth_mut_back T tl i0 ret0 - Result.ret (list_t.Cons x tl0) - | list_t.Nil => Result.fail Error.panic + Result.ret (List.Cons x tl0) + | List.Nil => Result.fail Error.panic /- [no_nested_borrows::list_rev_aux] -/ divergent def list_rev_aux_fwd - (T : Type) (li : list_t T) (lo : list_t T) : Result (list_t T) := + (T : Type) (li : List T) (lo : List T) : Result (List T) := match li with - | list_t.Cons hd tl => list_rev_aux_fwd T tl (list_t.Cons hd lo) - | list_t.Nil => Result.ret lo + | List.Cons hd tl => list_rev_aux_fwd T tl (List.Cons hd lo) + | List.Nil => Result.ret lo /- [no_nested_borrows::list_rev] -/ -def list_rev_fwd_back (T : Type) (l : list_t T) : Result (list_t T) := - let li := mem_replace_fwd (list_t T) l list_t.Nil - list_rev_aux_fwd T li list_t.Nil +def list_rev_fwd_back (T : Type) (l : List T) : Result (List T) := + let li := mem_replace_fwd (List T) l List.Nil + list_rev_aux_fwd T li List.Nil /- [no_nested_borrows::test_list_functions] -/ def test_list_functions_fwd : Result Unit := do - let l := list_t.Nil - let l0 := list_t.Cons (I32.ofInt 2 (by intlit)) l - let l1 := list_t.Cons (I32.ofInt 1 (by intlit)) l0 - let i ← list_length_fwd I32 (list_t.Cons (I32.ofInt 0 (by intlit)) l1) + let l := List.Nil + let l0 := List.Cons (I32.ofInt 2 (by intlit)) l + let l1 := List.Cons (I32.ofInt 1 (by intlit)) l0 + let i ← list_length_fwd I32 (List.Cons (I32.ofInt 0 (by intlit)) l1) if not (i = (U32.ofInt 3 (by intlit))) then Result.fail Error.panic else do let i0 ← - list_nth_shared_fwd I32 (list_t.Cons (I32.ofInt 0 (by intlit)) l1) + list_nth_shared_fwd I32 (List.Cons (I32.ofInt 0 (by intlit)) l1) (U32.ofInt 0 (by intlit)) if not (i0 = (I32.ofInt 0 (by intlit))) then Result.fail Error.panic else do let i1 ← - list_nth_shared_fwd I32 (list_t.Cons (I32.ofInt 0 (by intlit)) - l1) (U32.ofInt 1 (by intlit)) + list_nth_shared_fwd I32 (List.Cons (I32.ofInt 0 (by intlit)) l1) + (U32.ofInt 1 (by intlit)) if not (i1 = (I32.ofInt 1 (by intlit))) then Result.fail Error.panic else do let i2 ← - list_nth_shared_fwd I32 (list_t.Cons - (I32.ofInt 0 (by intlit)) l1) (U32.ofInt 2 (by intlit)) + list_nth_shared_fwd I32 (List.Cons (I32.ofInt 0 (by intlit)) + l1) (U32.ofInt 2 (by intlit)) if not (i2 = (I32.ofInt 2 (by intlit))) then Result.fail Error.panic else do let ls ← - list_nth_mut_back I32 (list_t.Cons + list_nth_mut_back I32 (List.Cons (I32.ofInt 0 (by intlit)) l1) (U32.ofInt 1 (by intlit)) (I32.ofInt 3 (by intlit)) let i3 ← @@ -434,11 +433,11 @@ def id_mut_pair4_back'b Result.ret ret0 /- [no_nested_borrows::StructWithTuple] -/ -structure struct_with_tuple_t (T1 T2 : Type) where +structure StructWithTuple (T1 T2 : Type) where struct_with_tuple_p : (T1 × T2) /- [no_nested_borrows::new_tuple1] -/ -def new_tuple1_fwd : Result (struct_with_tuple_t U32 U32) := +def new_tuple1_fwd : Result (StructWithTuple U32 U32) := Result.ret { struct_with_tuple_p := @@ -446,7 +445,7 @@ def new_tuple1_fwd : Result (struct_with_tuple_t U32 U32) := } /- [no_nested_borrows::new_tuple2] -/ -def new_tuple2_fwd : Result (struct_with_tuple_t I16 I16) := +def new_tuple2_fwd : Result (StructWithTuple I16 I16) := Result.ret { struct_with_tuple_p := @@ -454,7 +453,7 @@ def new_tuple2_fwd : Result (struct_with_tuple_t I16 I16) := } /- [no_nested_borrows::new_tuple3] -/ -def new_tuple3_fwd : Result (struct_with_tuple_t U64 I64) := +def new_tuple3_fwd : Result (StructWithTuple U64 I64) := Result.ret { struct_with_tuple_p := @@ -462,11 +461,11 @@ def new_tuple3_fwd : Result (struct_with_tuple_t U64 I64) := } /- [no_nested_borrows::StructWithPair] -/ -structure struct_with_pair_t (T1 T2 : Type) where - struct_with_pair_p : pair_t T1 T2 +structure StructWithPair (T1 T2 : Type) where + struct_with_pair_p : Pair T1 T2 /- [no_nested_borrows::new_pair1] -/ -def new_pair1_fwd : Result (struct_with_pair_t U32 U32) := +def new_pair1_fwd : Result (StructWithPair U32 U32) := Result.ret { struct_with_pair_p := @@ -531,10 +530,10 @@ def test_shared_borrow_bool2_fwd : Result U32 := Result.ret (U32.ofInt 0 (by intlit)) /- [no_nested_borrows::test_shared_borrow_enum1] -/ -def test_shared_borrow_enum1_fwd (l : list_t U32) : Result U32 := +def test_shared_borrow_enum1_fwd (l : List U32) : Result U32 := match l with - | list_t.Cons i l0 => Result.ret (U32.ofInt 1 (by intlit)) - | list_t.Nil => Result.ret (U32.ofInt 0 (by intlit)) + | List.Cons i l0 => Result.ret (U32.ofInt 1 (by intlit)) + | List.Nil => Result.ret (U32.ofInt 0 (by intlit)) /- [no_nested_borrows::test_shared_borrow_enum2] -/ def test_shared_borrow_enum2_fwd : Result U32 := diff --git a/tests/lean/Paper.lean b/tests/lean/Paper.lean index c34941ef..9f63b460 100644 --- a/tests/lean/Paper.lean +++ b/tests/lean/Paper.lean @@ -56,56 +56,55 @@ def test_choose_fwd : Result Unit := #assert (test_choose_fwd == .ret ()) /- [paper::List] -/ -inductive list_t (T : Type) := -| Cons : T → list_t T → list_t T -| Nil : list_t T +inductive List (T : Type) := +| Cons : T → List T → List T +| Nil : List T /- [paper::list_nth_mut] -/ -divergent def list_nth_mut_fwd - (T : Type) (l : list_t T) (i : U32) : Result T := +divergent def list_nth_mut_fwd (T : Type) (l : List T) (i : U32) : Result T := match l with - | list_t.Cons x tl => + | List.Cons x tl => if i = (U32.ofInt 0 (by intlit)) then Result.ret x else do let i0 ← i - (U32.ofInt 1 (by intlit)) list_nth_mut_fwd T tl i0 - | list_t.Nil => Result.fail Error.panic + | List.Nil => Result.fail Error.panic /- [paper::list_nth_mut] -/ divergent def list_nth_mut_back - (T : Type) (l : list_t T) (i : U32) (ret0 : T) : Result (list_t T) := + (T : Type) (l : List T) (i : U32) (ret0 : T) : Result (List T) := match l with - | list_t.Cons x tl => + | List.Cons x tl => if i = (U32.ofInt 0 (by intlit)) - then Result.ret (list_t.Cons ret0 tl) + then Result.ret (List.Cons ret0 tl) else do let i0 ← i - (U32.ofInt 1 (by intlit)) let tl0 ← list_nth_mut_back T tl i0 ret0 - Result.ret (list_t.Cons x tl0) - | list_t.Nil => Result.fail Error.panic + Result.ret (List.Cons x tl0) + | List.Nil => Result.fail Error.panic /- [paper::sum] -/ -divergent def sum_fwd (l : list_t I32) : Result I32 := +divergent def sum_fwd (l : List I32) : Result I32 := match l with - | list_t.Cons x tl => do - let i ← sum_fwd tl - x + i - | list_t.Nil => Result.ret (I32.ofInt 0 (by intlit)) + | List.Cons x tl => do + let i ← sum_fwd tl + x + i + | List.Nil => Result.ret (I32.ofInt 0 (by intlit)) /- [paper::test_nth] -/ def test_nth_fwd : Result Unit := do - let l := list_t.Nil - let l0 := list_t.Cons (I32.ofInt 3 (by intlit)) l - let l1 := list_t.Cons (I32.ofInt 2 (by intlit)) l0 + let l := List.Nil + let l0 := List.Cons (I32.ofInt 3 (by intlit)) l + let l1 := List.Cons (I32.ofInt 2 (by intlit)) l0 let x ← - list_nth_mut_fwd I32 (list_t.Cons (I32.ofInt 1 (by intlit)) l1) + list_nth_mut_fwd I32 (List.Cons (I32.ofInt 1 (by intlit)) l1) (U32.ofInt 2 (by intlit)) let x0 ← x + (I32.ofInt 1 (by intlit)) let l2 ← - list_nth_mut_back I32 (list_t.Cons (I32.ofInt 1 (by intlit)) l1) + list_nth_mut_back I32 (List.Cons (I32.ofInt 1 (by intlit)) l1) (U32.ofInt 2 (by intlit)) x0 let i ← sum_fwd l2 if not (i = (I32.ofInt 7 (by intlit))) diff --git a/tests/lean/PoloniusList.lean b/tests/lean/PoloniusList.lean index 0ff01b47..1d7ec99b 100644 --- a/tests/lean/PoloniusList.lean +++ b/tests/lean/PoloniusList.lean @@ -5,31 +5,31 @@ open Primitives namespace polonius_list /- [polonius_list::List] -/ -inductive list_t (T : Type) := -| Cons : T → list_t T → list_t T -| Nil : list_t T +inductive List (T : Type) := +| Cons : T → List T → List T +| Nil : List T /- [polonius_list::get_list_at_x] -/ divergent def get_list_at_x_fwd - (ls : list_t U32) (x : U32) : Result (list_t U32) := + (ls : List U32) (x : U32) : Result (List U32) := match ls with - | list_t.Cons hd tl => + | List.Cons hd tl => if hd = x - then Result.ret (list_t.Cons hd tl) + then Result.ret (List.Cons hd tl) else get_list_at_x_fwd tl x - | list_t.Nil => Result.ret list_t.Nil + | List.Nil => Result.ret List.Nil /- [polonius_list::get_list_at_x] -/ divergent def get_list_at_x_back - (ls : list_t U32) (x : U32) (ret0 : list_t U32) : Result (list_t U32) := + (ls : List U32) (x : U32) (ret0 : List U32) : Result (List U32) := match ls with - | list_t.Cons hd tl => + | List.Cons hd tl => if hd = x then Result.ret ret0 else do let tl0 ← get_list_at_x_back tl x ret0 - Result.ret (list_t.Cons hd tl0) - | list_t.Nil => Result.ret ret0 + Result.ret (List.Cons hd tl0) + | List.Nil => Result.ret ret0 end polonius_list -- cgit v1.2.3 From 5ca36bfc50083a01af2b7ae5f75993a520757ef5 Mon Sep 17 00:00:00 2001 From: Son Ho Date: Wed, 5 Jul 2023 15:17:58 +0200 Subject: Simplify the names used in Primitives.lean --- tests/lean/BetreeMain/Funs.lean | 6 ++-- tests/lean/External/Funs.lean | 4 +-- tests/lean/Hashmap/Funs.lean | 62 +++++++++++++++++++------------------- tests/lean/HashmapMain/Funs.lean | 64 +++++++++++++++++++--------------------- tests/lean/Loops/Funs.lean | 12 ++++---- tests/lean/NoNestedBorrows.lean | 4 +-- tests/lean/lake-manifest.json | 2 +- 7 files changed, 76 insertions(+), 78 deletions(-) (limited to 'tests/lean') diff --git a/tests/lean/BetreeMain/Funs.lean b/tests/lean/BetreeMain/Funs.lean index 3a678c71..074fff5e 100644 --- a/tests/lean/BetreeMain/Funs.lean +++ b/tests/lean/BetreeMain/Funs.lean @@ -124,13 +124,13 @@ divergent def betree.List.split_at_fwd /- [betree_main::betree::List::{1}::push_front] -/ def betree.List.push_front_fwd_back (T : Type) (self : betree.List T) (x : T) : Result (betree.List T) := - let tl := mem_replace_fwd (betree.List T) self betree.List.Nil + let tl := mem.replace_fwd (betree.List T) self betree.List.Nil let l := tl Result.ret (betree.List.Cons x l) /- [betree_main::betree::List::{1}::pop_front] -/ def betree.List.pop_front_fwd (T : Type) (self : betree.List T) : Result T := - let ls := mem_replace_fwd (betree.List T) self betree.List.Nil + let ls := mem.replace_fwd (betree.List T) self betree.List.Nil match ls with | betree.List.Cons x tl => Result.ret x | betree.List.Nil => Result.fail Error.panic @@ -138,7 +138,7 @@ def betree.List.pop_front_fwd (T : Type) (self : betree.List T) : Result T := /- [betree_main::betree::List::{1}::pop_front] -/ def betree.List.pop_front_back (T : Type) (self : betree.List T) : Result (betree.List T) := - let ls := mem_replace_fwd (betree.List T) self betree.List.Nil + let ls := mem.replace_fwd (betree.List T) self betree.List.Nil match ls with | betree.List.Cons x tl => Result.ret tl | betree.List.Nil => Result.fail Error.panic diff --git a/tests/lean/External/Funs.lean b/tests/lean/External/Funs.lean index 10efc3db..3fc21d91 100644 --- a/tests/lean/External/Funs.lean +++ b/tests/lean/External/Funs.lean @@ -36,8 +36,8 @@ def test_new_non_zero_u32_fwd /- [external::test_vec] -/ def test_vec_fwd : Result Unit := do - let v := vec_new U32 - let _ ← vec_push_back U32 v (U32.ofInt 0 (by intlit)) + let v := Vec.new U32 + let _ ← Vec.push U32 v (U32.ofInt 0 (by intlit)) Result.ret () /- Unit test for [external::test_vec] -/ diff --git a/tests/lean/Hashmap/Funs.lean b/tests/lean/Hashmap/Funs.lean index 4f16688b..48038bfb 100644 --- a/tests/lean/Hashmap/Funs.lean +++ b/tests/lean/Hashmap/Funs.lean @@ -15,7 +15,7 @@ divergent def HashMap.allocate_slots_loop_fwd if n > (Usize.ofInt 0 (by intlit)) then do - let slots0 ← vec_push_back (List T) slots List.Nil + let slots0 ← Vec.push (List T) slots List.Nil let n0 ← n - (Usize.ofInt 1 (by intlit)) HashMap.allocate_slots_loop_fwd T slots0 n0 else Result.ret slots @@ -32,7 +32,7 @@ def HashMap.new_with_capacity_fwd Result (HashMap T) := do - let v := vec_new (List T) + let v := Vec.new (List T) let slots ← HashMap.allocate_slots_fwd T v capacity let i ← capacity * max_load_dividend let i0 ← i / max_load_divisor @@ -52,12 +52,12 @@ def HashMap.new_fwd (T : Type) : Result (HashMap T) := /- [hashmap::HashMap::{0}::clear] -/ divergent def HashMap.clear_loop_fwd_back (T : Type) (slots : Vec (List T)) (i : Usize) : Result (Vec (List T)) := - let i0 := vec_len (List T) slots + let i0 := Vec.len (List T) slots if i < i0 then do let i1 ← i + (Usize.ofInt 1 (by intlit)) - let slots0 ← vec_index_mut_back (List T) slots i List.Nil + let slots0 ← Vec.index_mut_back (List T) slots i List.Nil HashMap.clear_loop_fwd_back T slots0 i1 else Result.ret slots @@ -121,22 +121,22 @@ def HashMap.insert_no_resize_fwd_back := do let hash ← hash_key_fwd key - let i := vec_len (List T) self.hash_map_slots + let i := Vec.len (List T) self.hash_map_slots let hash_mod ← hash % i - let l ← vec_index_mut_fwd (List T) self.hash_map_slots hash_mod + let l ← Vec.index_mut (List T) self.hash_map_slots hash_mod let inserted ← HashMap.insert_in_list_fwd T key value l if inserted then do let i0 ← self.hash_map_num_entries + (Usize.ofInt 1 (by intlit)) let l0 ← HashMap.insert_in_list_back T key value l - let v ← vec_index_mut_back (List T) self.hash_map_slots hash_mod l0 + let v ← Vec.index_mut_back (List T) self.hash_map_slots hash_mod l0 Result.ret { self with hash_map_num_entries := i0, hash_map_slots := v } else do let l0 ← HashMap.insert_in_list_back T key value l - let v ← vec_index_mut_back (List T) self.hash_map_slots hash_mod l0 + let v ← Vec.index_mut_back (List T) self.hash_map_slots hash_mod l0 Result.ret { self with hash_map_slots := v } /- [core::num::u32::{9}::MAX] -/ @@ -164,16 +164,16 @@ divergent def HashMap.move_elements_loop_fwd_back (T : Type) (ntable : HashMap T) (slots : Vec (List T)) (i : Usize) : Result ((HashMap T) × (Vec (List T))) := - let i0 := vec_len (List T) slots + let i0 := Vec.len (List T) slots if i < i0 then do - let l ← vec_index_mut_fwd (List T) slots i - let ls := mem_replace_fwd (List T) l List.Nil + let l ← Vec.index_mut (List T) slots i + let ls := mem.replace_fwd (List T) l List.Nil let ntable0 ← HashMap.move_elements_from_list_fwd_back T ntable ls let i1 ← i + (Usize.ofInt 1 (by intlit)) - let l0 := mem_replace_back (List T) l List.Nil - let slots0 ← vec_index_mut_back (List T) slots i l0 + let l0 := mem.replace_back (List T) l List.Nil + let slots0 ← Vec.index_mut_back (List T) slots i l0 HashMap.move_elements_loop_fwd_back T ntable0 slots0 i1 else Result.ret (ntable, slots) @@ -189,7 +189,7 @@ def HashMap.try_resize_fwd_back (T : Type) (self : HashMap T) : Result (HashMap T) := do let max_usize ← Scalar.cast .Usize core_num_u32_max_c - let capacity := vec_len (List T) self.hash_map_slots + let capacity := Vec.len (List T) self.hash_map_slots let n1 ← max_usize / (Usize.ofInt 2 (by intlit)) let (i, i0) := self.hash_map_max_load_factor let i1 ← n1 / i @@ -242,9 +242,9 @@ def HashMap.contains_key_fwd (T : Type) (self : HashMap T) (key : Usize) : Result Bool := do let hash ← hash_key_fwd key - let i := vec_len (List T) self.hash_map_slots + let i := Vec.len (List T) self.hash_map_slots let hash_mod ← hash % i - let l ← vec_index_fwd (List T) self.hash_map_slots hash_mod + let l ← Vec.index (List T) self.hash_map_slots hash_mod HashMap.contains_key_in_list_fwd T key l /- [hashmap::HashMap::{0}::get_in_list] -/ @@ -266,9 +266,9 @@ def HashMap.get_in_list_fwd def HashMap.get_fwd (T : Type) (self : HashMap T) (key : Usize) : Result T := do let hash ← hash_key_fwd key - let i := vec_len (List T) self.hash_map_slots + let i := Vec.len (List T) self.hash_map_slots let hash_mod ← hash % i - let l ← vec_index_fwd (List T) self.hash_map_slots hash_mod + let l ← Vec.index (List T) self.hash_map_slots hash_mod HashMap.get_in_list_fwd T key l /- [hashmap::HashMap::{0}::get_mut_in_list] -/ @@ -309,9 +309,9 @@ def HashMap.get_mut_fwd (T : Type) (self : HashMap T) (key : Usize) : Result T := do let hash ← hash_key_fwd key - let i := vec_len (List T) self.hash_map_slots + let i := Vec.len (List T) self.hash_map_slots let hash_mod ← hash % i - let l ← vec_index_mut_fwd (List T) self.hash_map_slots hash_mod + let l ← Vec.index_mut (List T) self.hash_map_slots hash_mod HashMap.get_mut_in_list_fwd T l key /- [hashmap::HashMap::{0}::get_mut] -/ @@ -321,11 +321,11 @@ def HashMap.get_mut_back := do let hash ← hash_key_fwd key - let i := vec_len (List T) self.hash_map_slots + let i := Vec.len (List T) self.hash_map_slots let hash_mod ← hash % i - let l ← vec_index_mut_fwd (List T) self.hash_map_slots hash_mod + let l ← Vec.index_mut (List T) self.hash_map_slots hash_mod let l0 ← HashMap.get_mut_in_list_back T l key ret0 - let v ← vec_index_mut_back (List T) self.hash_map_slots hash_mod l0 + let v ← Vec.index_mut_back (List T) self.hash_map_slots hash_mod l0 Result.ret { self with hash_map_slots := v } /- [hashmap::HashMap::{0}::remove_from_list] -/ @@ -335,7 +335,7 @@ divergent def HashMap.remove_from_list_loop_fwd | List.Cons ckey t tl => if ckey = key then - let mv_ls := mem_replace_fwd (List T) (List.Cons ckey t tl) List.Nil + let mv_ls := mem.replace_fwd (List T) (List.Cons ckey t tl) List.Nil match mv_ls with | List.Cons i cvalue tl0 => Result.ret (Option.some cvalue) | List.Nil => Result.fail Error.panic @@ -354,7 +354,7 @@ divergent def HashMap.remove_from_list_loop_back | List.Cons ckey t tl => if ckey = key then - let mv_ls := mem_replace_fwd (List T) (List.Cons ckey t tl) List.Nil + let mv_ls := mem.replace_fwd (List T) (List.Cons ckey t tl) List.Nil match mv_ls with | List.Cons i cvalue tl0 => Result.ret tl0 | List.Nil => Result.fail Error.panic @@ -374,9 +374,9 @@ def HashMap.remove_fwd (T : Type) (self : HashMap T) (key : Usize) : Result (Option T) := do let hash ← hash_key_fwd key - let i := vec_len (List T) self.hash_map_slots + let i := Vec.len (List T) self.hash_map_slots let hash_mod ← hash % i - let l ← vec_index_mut_fwd (List T) self.hash_map_slots hash_mod + let l ← Vec.index_mut (List T) self.hash_map_slots hash_mod let x ← HashMap.remove_from_list_fwd T key l match x with | Option.none => Result.ret Option.none @@ -390,21 +390,21 @@ def HashMap.remove_back (T : Type) (self : HashMap T) (key : Usize) : Result (HashMap T) := do let hash ← hash_key_fwd key - let i := vec_len (List T) self.hash_map_slots + let i := Vec.len (List T) self.hash_map_slots let hash_mod ← hash % i - let l ← vec_index_mut_fwd (List T) self.hash_map_slots hash_mod + let l ← Vec.index_mut (List T) self.hash_map_slots hash_mod let x ← HashMap.remove_from_list_fwd T key l match x with | Option.none => do let l0 ← HashMap.remove_from_list_back T key l - let v ← vec_index_mut_back (List T) self.hash_map_slots hash_mod l0 + let v ← Vec.index_mut_back (List T) self.hash_map_slots hash_mod l0 Result.ret { self with hash_map_slots := v } | Option.some x0 => do let i0 ← self.hash_map_num_entries - (Usize.ofInt 1 (by intlit)) let l0 ← HashMap.remove_from_list_back T key l - let v ← vec_index_mut_back (List T) self.hash_map_slots hash_mod l0 + let v ← Vec.index_mut_back (List T) self.hash_map_slots hash_mod l0 Result.ret { self with hash_map_num_entries := i0, hash_map_slots := v } diff --git a/tests/lean/HashmapMain/Funs.lean b/tests/lean/HashmapMain/Funs.lean index fd556878..1a741a2d 100644 --- a/tests/lean/HashmapMain/Funs.lean +++ b/tests/lean/HashmapMain/Funs.lean @@ -18,7 +18,7 @@ divergent def hashmap.HashMap.allocate_slots_loop_fwd if n > (Usize.ofInt 0 (by intlit)) then do - let slots0 ← vec_push_back (hashmap.List T) slots hashmap.List.Nil + let slots0 ← Vec.push (hashmap.List T) slots hashmap.List.Nil let n0 ← n - (Usize.ofInt 1 (by intlit)) hashmap.HashMap.allocate_slots_loop_fwd T slots0 n0 else Result.ret slots @@ -37,7 +37,7 @@ def hashmap.HashMap.new_with_capacity_fwd Result (hashmap.HashMap T) := do - let v := vec_new (hashmap.List T) + let v := Vec.new (hashmap.List T) let slots ← hashmap.HashMap.allocate_slots_fwd T v capacity let i ← capacity * max_load_dividend let i0 ← i / max_load_divisor @@ -60,13 +60,13 @@ divergent def hashmap.HashMap.clear_loop_fwd_back (T : Type) (slots : Vec (hashmap.List T)) (i : Usize) : Result (Vec (hashmap.List T)) := - let i0 := vec_len (hashmap.List T) slots + let i0 := Vec.len (hashmap.List T) slots if i < i0 then do let i1 ← i + (Usize.ofInt 1 (by intlit)) let slots0 ← - vec_index_mut_back (hashmap.List T) slots i hashmap.List.Nil + Vec.index_mut_back (hashmap.List T) slots i hashmap.List.Nil hashmap.HashMap.clear_loop_fwd_back T slots0 i1 else Result.ret slots @@ -136,10 +136,10 @@ def hashmap.HashMap.insert_no_resize_fwd_back := do let hash ← hashmap.hash_key_fwd key - let i := vec_len (hashmap.List T) self.hashmap_hash_map_slots + let i := Vec.len (hashmap.List T) self.hashmap_hash_map_slots let hash_mod ← hash % i let l ← - vec_index_mut_fwd (hashmap.List T) self.hashmap_hash_map_slots hash_mod + Vec.index_mut (hashmap.List T) self.hashmap_hash_map_slots hash_mod let inserted ← hashmap.HashMap.insert_in_list_fwd T key value l if inserted then @@ -148,7 +148,7 @@ def hashmap.HashMap.insert_no_resize_fwd_back (Usize.ofInt 1 (by intlit)) let l0 ← hashmap.HashMap.insert_in_list_back T key value l let v ← - vec_index_mut_back (hashmap.List T) self.hashmap_hash_map_slots + Vec.index_mut_back (hashmap.List T) self.hashmap_hash_map_slots hash_mod l0 Result.ret { @@ -160,7 +160,7 @@ def hashmap.HashMap.insert_no_resize_fwd_back do let l0 ← hashmap.HashMap.insert_in_list_back T key value l let v ← - vec_index_mut_back (hashmap.List T) self.hashmap_hash_map_slots + Vec.index_mut_back (hashmap.List T) self.hashmap_hash_map_slots hash_mod l0 Result.ret { self with hashmap_hash_map_slots := v } @@ -194,17 +194,17 @@ divergent def hashmap.HashMap.move_elements_loop_fwd_back (i : Usize) : Result ((hashmap.HashMap T) × (Vec (hashmap.List T))) := - let i0 := vec_len (hashmap.List T) slots + let i0 := Vec.len (hashmap.List T) slots if i < i0 then do - let l ← vec_index_mut_fwd (hashmap.List T) slots i - let ls := mem_replace_fwd (hashmap.List T) l hashmap.List.Nil + let l ← Vec.index_mut (hashmap.List T) slots i + let ls := mem.replace_fwd (hashmap.List T) l hashmap.List.Nil let ntable0 ← hashmap.HashMap.move_elements_from_list_fwd_back T ntable ls let i1 ← i + (Usize.ofInt 1 (by intlit)) - let l0 := mem_replace_back (hashmap.List T) l hashmap.List.Nil - let slots0 ← vec_index_mut_back (hashmap.List T) slots i l0 + let l0 := mem.replace_back (hashmap.List T) l hashmap.List.Nil + let slots0 ← Vec.index_mut_back (hashmap.List T) slots i l0 hashmap.HashMap.move_elements_loop_fwd_back T ntable0 slots0 i1 else Result.ret (ntable, slots) @@ -221,7 +221,7 @@ def hashmap.HashMap.try_resize_fwd_back (T : Type) (self : hashmap.HashMap T) : Result (hashmap.HashMap T) := do let max_usize ← Scalar.cast .Usize core_num_u32_max_c - let capacity := vec_len (hashmap.List T) self.hashmap_hash_map_slots + let capacity := Vec.len (hashmap.List T) self.hashmap_hash_map_slots let n1 ← max_usize / (Usize.ofInt 2 (by intlit)) let (i, i0) := self.hashmap_hash_map_max_load_factor let i1 ← n1 / i @@ -274,10 +274,9 @@ def hashmap.HashMap.contains_key_fwd (T : Type) (self : hashmap.HashMap T) (key : Usize) : Result Bool := do let hash ← hashmap.hash_key_fwd key - let i := vec_len (hashmap.List T) self.hashmap_hash_map_slots + let i := Vec.len (hashmap.List T) self.hashmap_hash_map_slots let hash_mod ← hash % i - let l ← - vec_index_fwd (hashmap.List T) self.hashmap_hash_map_slots hash_mod + let l ← Vec.index (hashmap.List T) self.hashmap_hash_map_slots hash_mod hashmap.HashMap.contains_key_in_list_fwd T key l /- [hashmap_main::hashmap::HashMap::{0}::get_in_list] -/ @@ -300,10 +299,9 @@ def hashmap.HashMap.get_fwd (T : Type) (self : hashmap.HashMap T) (key : Usize) : Result T := do let hash ← hashmap.hash_key_fwd key - let i := vec_len (hashmap.List T) self.hashmap_hash_map_slots + let i := Vec.len (hashmap.List T) self.hashmap_hash_map_slots let hash_mod ← hash % i - let l ← - vec_index_fwd (hashmap.List T) self.hashmap_hash_map_slots hash_mod + let l ← Vec.index (hashmap.List T) self.hashmap_hash_map_slots hash_mod hashmap.HashMap.get_in_list_fwd T key l /- [hashmap_main::hashmap::HashMap::{0}::get_mut_in_list] -/ @@ -348,10 +346,10 @@ def hashmap.HashMap.get_mut_fwd (T : Type) (self : hashmap.HashMap T) (key : Usize) : Result T := do let hash ← hashmap.hash_key_fwd key - let i := vec_len (hashmap.List T) self.hashmap_hash_map_slots + let i := Vec.len (hashmap.List T) self.hashmap_hash_map_slots let hash_mod ← hash % i let l ← - vec_index_mut_fwd (hashmap.List T) self.hashmap_hash_map_slots hash_mod + Vec.index_mut (hashmap.List T) self.hashmap_hash_map_slots hash_mod hashmap.HashMap.get_mut_in_list_fwd T l key /- [hashmap_main::hashmap::HashMap::{0}::get_mut] -/ @@ -361,13 +359,13 @@ def hashmap.HashMap.get_mut_back := do let hash ← hashmap.hash_key_fwd key - let i := vec_len (hashmap.List T) self.hashmap_hash_map_slots + let i := Vec.len (hashmap.List T) self.hashmap_hash_map_slots let hash_mod ← hash % i let l ← - vec_index_mut_fwd (hashmap.List T) self.hashmap_hash_map_slots hash_mod + Vec.index_mut (hashmap.List T) self.hashmap_hash_map_slots hash_mod let l0 ← hashmap.HashMap.get_mut_in_list_back T l key ret0 let v ← - vec_index_mut_back (hashmap.List T) self.hashmap_hash_map_slots hash_mod + Vec.index_mut_back (hashmap.List T) self.hashmap_hash_map_slots hash_mod l0 Result.ret { self with hashmap_hash_map_slots := v } @@ -379,7 +377,7 @@ divergent def hashmap.HashMap.remove_from_list_loop_fwd if ckey = key then let mv_ls := - mem_replace_fwd (hashmap.List T) (hashmap.List.Cons ckey t tl) + mem.replace_fwd (hashmap.List T) (hashmap.List.Cons ckey t tl) hashmap.List.Nil match mv_ls with | hashmap.List.Cons i cvalue tl0 => Result.ret (Option.some cvalue) @@ -400,7 +398,7 @@ divergent def hashmap.HashMap.remove_from_list_loop_back if ckey = key then let mv_ls := - mem_replace_fwd (hashmap.List T) (hashmap.List.Cons ckey t tl) + mem.replace_fwd (hashmap.List T) (hashmap.List.Cons ckey t tl) hashmap.List.Nil match mv_ls with | hashmap.List.Cons i cvalue tl0 => Result.ret tl0 @@ -421,10 +419,10 @@ def hashmap.HashMap.remove_fwd (T : Type) (self : hashmap.HashMap T) (key : Usize) : Result (Option T) := do let hash ← hashmap.hash_key_fwd key - let i := vec_len (hashmap.List T) self.hashmap_hash_map_slots + let i := Vec.len (hashmap.List T) self.hashmap_hash_map_slots let hash_mod ← hash % i let l ← - vec_index_mut_fwd (hashmap.List T) self.hashmap_hash_map_slots hash_mod + Vec.index_mut (hashmap.List T) self.hashmap_hash_map_slots hash_mod let x ← hashmap.HashMap.remove_from_list_fwd T key l match x with | Option.none => Result.ret Option.none @@ -441,17 +439,17 @@ def hashmap.HashMap.remove_back := do let hash ← hashmap.hash_key_fwd key - let i := vec_len (hashmap.List T) self.hashmap_hash_map_slots + let i := Vec.len (hashmap.List T) self.hashmap_hash_map_slots let hash_mod ← hash % i let l ← - vec_index_mut_fwd (hashmap.List T) self.hashmap_hash_map_slots hash_mod + Vec.index_mut (hashmap.List T) self.hashmap_hash_map_slots hash_mod let x ← hashmap.HashMap.remove_from_list_fwd T key l match x with | Option.none => do let l0 ← hashmap.HashMap.remove_from_list_back T key l let v ← - vec_index_mut_back (hashmap.List T) self.hashmap_hash_map_slots + Vec.index_mut_back (hashmap.List T) self.hashmap_hash_map_slots hash_mod l0 Result.ret { self with hashmap_hash_map_slots := v } | Option.some x0 => @@ -460,7 +458,7 @@ def hashmap.HashMap.remove_back (Usize.ofInt 1 (by intlit)) let l0 ← hashmap.HashMap.remove_from_list_back T key l let v ← - vec_index_mut_back (hashmap.List T) self.hashmap_hash_map_slots + Vec.index_mut_back (hashmap.List T) self.hashmap_hash_map_slots hash_mod l0 Result.ret { diff --git a/tests/lean/Loops/Funs.lean b/tests/lean/Loops/Funs.lean index 383bc819..6e6eef3b 100644 --- a/tests/lean/Loops/Funs.lean +++ b/tests/lean/Loops/Funs.lean @@ -54,12 +54,12 @@ def sum_with_shared_borrows_fwd (max : U32) : Result U32 := /- [loops::clear] -/ divergent def clear_loop_fwd_back (v : Vec U32) (i : Usize) : Result (Vec U32) := - let i0 := vec_len U32 v + let i0 := Vec.len U32 v if i < i0 then do let i1 ← i + (Usize.ofInt 1 (by intlit)) - let v0 ← vec_index_mut_back U32 v i (U32.ofInt 0 (by intlit)) + let v0 ← Vec.index_mut_back U32 v i (U32.ofInt 0 (by intlit)) clear_loop_fwd_back v0 i1 else Result.ret v @@ -145,7 +145,7 @@ divergent def get_elem_mut_loop_fwd /- [loops::get_elem_mut] -/ def get_elem_mut_fwd (slots : Vec (List Usize)) (x : Usize) : Result Usize := do - let l ← vec_index_mut_fwd (List Usize) slots (Usize.ofInt 0 (by intlit)) + let l ← Vec.index_mut (List Usize) slots (Usize.ofInt 0 (by intlit)) get_elem_mut_loop_fwd x l /- [loops::get_elem_mut] -/ @@ -167,9 +167,9 @@ def get_elem_mut_back Result (Vec (List Usize)) := do - let l ← vec_index_mut_fwd (List Usize) slots (Usize.ofInt 0 (by intlit)) + let l ← Vec.index_mut (List Usize) slots (Usize.ofInt 0 (by intlit)) let l0 ← get_elem_mut_loop_back x l ret0 - vec_index_mut_back (List Usize) slots (Usize.ofInt 0 (by intlit)) l0 + Vec.index_mut_back (List Usize) slots (Usize.ofInt 0 (by intlit)) l0 /- [loops::get_elem_shared] -/ divergent def get_elem_shared_loop_fwd @@ -185,7 +185,7 @@ divergent def get_elem_shared_loop_fwd def get_elem_shared_fwd (slots : Vec (List Usize)) (x : Usize) : Result Usize := do - let l ← vec_index_fwd (List Usize) slots (Usize.ofInt 0 (by intlit)) + let l ← Vec.index (List Usize) slots (Usize.ofInt 0 (by intlit)) get_elem_shared_loop_fwd x l /- [loops::id_mut] -/ diff --git a/tests/lean/NoNestedBorrows.lean b/tests/lean/NoNestedBorrows.lean index cdbbcd67..e4fa7612 100644 --- a/tests/lean/NoNestedBorrows.lean +++ b/tests/lean/NoNestedBorrows.lean @@ -321,7 +321,7 @@ divergent def list_rev_aux_fwd /- [no_nested_borrows::list_rev] -/ def list_rev_fwd_back (T : Type) (l : List T) : Result (List T) := - let li := mem_replace_fwd (List T) l List.Nil + let li := mem.replace_fwd (List T) l List.Nil list_rev_aux_fwd T li List.Nil /- [no_nested_borrows::test_list_functions] -/ @@ -514,7 +514,7 @@ def test_weird_borrows1_fwd : Result Unit := /- [no_nested_borrows::test_mem_replace] -/ def test_mem_replace_fwd_back (px : U32) : Result U32 := - let y := mem_replace_fwd U32 px (U32.ofInt 1 (by intlit)) + let y := mem.replace_fwd U32 px (U32.ofInt 1 (by intlit)) if not (y = (U32.ofInt 0 (by intlit))) then Result.fail Error.panic else Result.ret (U32.ofInt 2 (by intlit)) diff --git a/tests/lean/lake-manifest.json b/tests/lean/lake-manifest.json index 637bda23..75f7010a 100644 --- a/tests/lean/lake-manifest.json +++ b/tests/lean/lake-manifest.json @@ -11,7 +11,7 @@ {"git": {"url": "https://github.com/leanprover-community/mathlib4.git", "subDir?": null, - "rev": "cc5d11f24e1b92db65ec3389bb5142f4b2d7670e", + "rev": "bb4fb766e41dd3a64197263ec132c7f9c4b50065", "name": "mathlib", "inputRev?": null}}, {"git": -- cgit v1.2.3 From 7c95800cefc87fad894f8bf855cfc047e713b3a7 Mon Sep 17 00:00:00 2001 From: Son Ho Date: Thu, 6 Jul 2023 12:20:28 +0200 Subject: Improve the generated comments --- tests/lean/BetreeMain/Funs.lean | 472 +++++++++++----------- tests/lean/BetreeMain/FunsExternal.lean | 10 +- tests/lean/BetreeMain/FunsExternal_Template.lean | 20 +- tests/lean/Constants.lean | 54 +-- tests/lean/External/Funs.lean | 49 ++- tests/lean/External/FunsExternal.lean | 6 +- tests/lean/External/FunsExternal_Template.lean | 17 +- tests/lean/Hashmap/Funs.lean | 262 ++++++------ tests/lean/HashmapMain/Funs.lean | 276 ++++++------- tests/lean/HashmapMain/FunsExternal.lean | 4 +- tests/lean/HashmapMain/FunsExternal_Template.lean | 8 +- tests/lean/Loops/Funs.lean | 300 +++++++------- tests/lean/NoNestedBorrows.lean | 292 +++++++------ tests/lean/Paper.lean | 57 +-- tests/lean/PoloniusList.lean | 9 +- 15 files changed, 916 insertions(+), 920 deletions(-) (limited to 'tests/lean') diff --git a/tests/lean/BetreeMain/Funs.lean b/tests/lean/BetreeMain/Funs.lean index 074fff5e..41e4349e 100644 --- a/tests/lean/BetreeMain/Funs.lean +++ b/tests/lean/BetreeMain/Funs.lean @@ -6,60 +6,59 @@ import BetreeMain.FunsExternal open Primitives namespace betree_main -/- [betree_main::betree::load_internal_node] -/ -def betree.load_internal_node_fwd +/- [betree_main::betree::load_internal_node]: forward function -/ +def betree.load_internal_node (id : U64) (st : State) : Result (State × (betree.List (U64 × betree.Message))) := - betree_utils.load_internal_node_fwd id st + betree_utils.load_internal_node id st -/- [betree_main::betree::store_internal_node] -/ -def betree.store_internal_node_fwd +/- [betree_main::betree::store_internal_node]: forward function -/ +def betree.store_internal_node (id : U64) (content : betree.List (U64 × betree.Message)) (st : State) : Result (State × Unit) := do - let (st0, _) ← betree_utils.store_internal_node_fwd id content st + let (st0, _) ← betree_utils.store_internal_node id content st Result.ret (st0, ()) -/- [betree_main::betree::load_leaf_node] -/ -def betree.load_leaf_node_fwd +/- [betree_main::betree::load_leaf_node]: forward function -/ +def betree.load_leaf_node (id : U64) (st : State) : Result (State × (betree.List (U64 × U64))) := - betree_utils.load_leaf_node_fwd id st + betree_utils.load_leaf_node id st -/- [betree_main::betree::store_leaf_node] -/ -def betree.store_leaf_node_fwd +/- [betree_main::betree::store_leaf_node]: forward function -/ +def betree.store_leaf_node (id : U64) (content : betree.List (U64 × U64)) (st : State) : Result (State × Unit) := do - let (st0, _) ← betree_utils.store_leaf_node_fwd id content st + let (st0, _) ← betree_utils.store_leaf_node id content st Result.ret (st0, ()) -/- [betree_main::betree::fresh_node_id] -/ -def betree.fresh_node_id_fwd (counter : U64) : Result U64 := +/- [betree_main::betree::fresh_node_id]: forward function -/ +def betree.fresh_node_id (counter : U64) : Result U64 := do let _ ← counter + (U64.ofInt 1 (by intlit)) Result.ret counter -/- [betree_main::betree::fresh_node_id] -/ +/- [betree_main::betree::fresh_node_id]: backward function 0 -/ def betree.fresh_node_id_back (counter : U64) : Result U64 := counter + (U64.ofInt 1 (by intlit)) -/- [betree_main::betree::NodeIdCounter::{0}::new] -/ -def betree.NodeIdCounter.new_fwd : Result betree.NodeIdCounter := +/- [betree_main::betree::NodeIdCounter::{0}::new]: forward function -/ +def betree.NodeIdCounter.new : Result betree.NodeIdCounter := Result.ret { betree_node_id_counter_next_node_id := (U64.ofInt 0 (by intlit)) } -/- [betree_main::betree::NodeIdCounter::{0}::fresh_id] -/ -def betree.NodeIdCounter.fresh_id_fwd - (self : betree.NodeIdCounter) : Result U64 := +/- [betree_main::betree::NodeIdCounter::{0}::fresh_id]: forward function -/ +def betree.NodeIdCounter.fresh_id (self : betree.NodeIdCounter) : Result U64 := do let _ ← self.betree_node_id_counter_next_node_id + (U64.ofInt 1 (by intlit)) Result.ret self.betree_node_id_counter_next_node_id -/- [betree_main::betree::NodeIdCounter::{0}::fresh_id] -/ +/- [betree_main::betree::NodeIdCounter::{0}::fresh_id]: backward function 0 -/ def betree.NodeIdCounter.fresh_id_back (self : betree.NodeIdCounter) : Result betree.NodeIdCounter := do @@ -72,8 +71,8 @@ def core_num_u64_max_body : Result U64 := Result.ret (U64.ofInt 18446744073709551615 (by intlit)) def core_num_u64_max_c : U64 := eval_global core_num_u64_max_body (by simp) -/- [betree_main::betree::upsert_update] -/ -def betree.upsert_update_fwd +/- [betree_main::betree::upsert_update]: forward function -/ +def betree.upsert_update (prev : Option U64) (st : betree.UpsertFunState) : Result U64 := match prev with | Option.none => @@ -93,18 +92,17 @@ def betree.upsert_update_fwd then prev0 - v else Result.ret (U64.ofInt 0 (by intlit)) -/- [betree_main::betree::List::{1}::len] -/ -divergent def betree.List.len_fwd - (T : Type) (self : betree.List T) : Result U64 := +/- [betree_main::betree::List::{1}::len]: forward function -/ +divergent def betree.List.len (T : Type) (self : betree.List T) : Result U64 := match self with | betree.List.Cons t tl => do - let i ← betree.List.len_fwd T tl + let i ← betree.List.len T tl (U64.ofInt 1 (by intlit)) + i | betree.List.Nil => Result.ret (U64.ofInt 0 (by intlit)) -/- [betree_main::betree::List::{1}::split_at] -/ -divergent def betree.List.split_at_fwd +/- [betree_main::betree::List::{1}::split_at]: forward function -/ +divergent def betree.List.split_at (T : Type) (self : betree.List T) (n : U64) : Result ((betree.List T) × (betree.List T)) := @@ -115,50 +113,51 @@ divergent def betree.List.split_at_fwd | betree.List.Cons hd tl => do let i ← n - (U64.ofInt 1 (by intlit)) - let p ← betree.List.split_at_fwd T tl i + let p ← betree.List.split_at T tl i let (ls0, ls1) := p let l := ls0 Result.ret (betree.List.Cons hd l, ls1) | betree.List.Nil => Result.fail Error.panic -/- [betree_main::betree::List::{1}::push_front] -/ -def betree.List.push_front_fwd_back +/- [betree_main::betree::List::{1}::push_front]: merged forward/backward function + (there is a single backward function, and the forward function returns ()) -/ +def betree.List.push_front (T : Type) (self : betree.List T) (x : T) : Result (betree.List T) := - let tl := mem.replace_fwd (betree.List T) self betree.List.Nil + let tl := mem.replace (betree.List T) self betree.List.Nil let l := tl Result.ret (betree.List.Cons x l) -/- [betree_main::betree::List::{1}::pop_front] -/ -def betree.List.pop_front_fwd (T : Type) (self : betree.List T) : Result T := - let ls := mem.replace_fwd (betree.List T) self betree.List.Nil +/- [betree_main::betree::List::{1}::pop_front]: forward function -/ +def betree.List.pop_front (T : Type) (self : betree.List T) : Result T := + let ls := mem.replace (betree.List T) self betree.List.Nil match ls with | betree.List.Cons x tl => Result.ret x | betree.List.Nil => Result.fail Error.panic -/- [betree_main::betree::List::{1}::pop_front] -/ +/- [betree_main::betree::List::{1}::pop_front]: backward function 0 -/ def betree.List.pop_front_back (T : Type) (self : betree.List T) : Result (betree.List T) := - let ls := mem.replace_fwd (betree.List T) self betree.List.Nil + let ls := mem.replace (betree.List T) self betree.List.Nil match ls with | betree.List.Cons x tl => Result.ret tl | betree.List.Nil => Result.fail Error.panic -/- [betree_main::betree::List::{1}::hd] -/ -def betree.List.hd_fwd (T : Type) (self : betree.List T) : Result T := +/- [betree_main::betree::List::{1}::hd]: forward function -/ +def betree.List.hd (T : Type) (self : betree.List T) : Result T := match self with | betree.List.Cons hd l => Result.ret hd | betree.List.Nil => Result.fail Error.panic -/- [betree_main::betree::List::{2}::head_has_key] -/ -def betree.List.head_has_key_fwd +/- [betree_main::betree::List::{2}::head_has_key]: forward function -/ +def betree.List.head_has_key (T : Type) (self : betree.List (U64 × T)) (key : U64) : Result Bool := match self with | betree.List.Cons hd l => let (i, _) := hd Result.ret (i = key) | betree.List.Nil => Result.ret false -/- [betree_main::betree::List::{2}::partition_at_pivot] -/ -divergent def betree.List.partition_at_pivot_fwd +/- [betree_main::betree::List::{2}::partition_at_pivot]: forward function -/ +divergent def betree.List.partition_at_pivot (T : Type) (self : betree.List (U64 × T)) (pivot : U64) : Result ((betree.List (U64 × T)) × (betree.List (U64 × T))) := @@ -169,30 +168,29 @@ divergent def betree.List.partition_at_pivot_fwd then Result.ret (betree.List.Nil, betree.List.Cons (i, t) tl) else do - let p ← betree.List.partition_at_pivot_fwd T tl pivot + let p ← betree.List.partition_at_pivot T tl pivot let (ls0, ls1) := p let l := ls0 Result.ret (betree.List.Cons (i, t) l, ls1) | betree.List.Nil => Result.ret (betree.List.Nil, betree.List.Nil) -/- [betree_main::betree::Leaf::{3}::split] -/ -def betree.Leaf.split_fwd +/- [betree_main::betree::Leaf::{3}::split]: forward function -/ +def betree.Leaf.split (self : betree.Leaf) (content : betree.List (U64 × U64)) (params : betree.Params) (node_id_cnt : betree.NodeIdCounter) (st : State) : Result (State × betree.Internal) := do let p ← - betree.List.split_at_fwd (U64 × U64) content - params.betree_params_split_size + betree.List.split_at (U64 × U64) content params.betree_params_split_size let (content0, content1) := p - let p0 ← betree.List.hd_fwd (U64 × U64) content1 + let p0 ← betree.List.hd (U64 × U64) content1 let (pivot, _) := p0 - let id0 ← betree.NodeIdCounter.fresh_id_fwd node_id_cnt + let id0 ← betree.NodeIdCounter.fresh_id node_id_cnt let node_id_cnt0 ← betree.NodeIdCounter.fresh_id_back node_id_cnt - let id1 ← betree.NodeIdCounter.fresh_id_fwd node_id_cnt0 - let (st0, _) ← betree.store_leaf_node_fwd id0 content0 st - let (st1, _) ← betree.store_leaf_node_fwd id1 content1 st0 + let id1 ← betree.NodeIdCounter.fresh_id node_id_cnt0 + let (st0, _) ← betree.store_leaf_node id0 content0 st + let (st1, _) ← betree.store_leaf_node id1 content1 st0 let n := betree.Node.Leaf { betree_leaf_id := id0, @@ -205,7 +203,7 @@ def betree.Leaf.split_fwd } Result.ret (st1, betree.Internal.mk self.betree_leaf_id pivot n n0) -/- [betree_main::betree::Leaf::{3}::split] -/ +/- [betree_main::betree::Leaf::{3}::split]: backward function 2 -/ def betree.Leaf.split_back (self : betree.Leaf) (content : betree.List (U64 × U64)) (params : betree.Params) (node_id_cnt : betree.NodeIdCounter) (st : State) : @@ -213,19 +211,18 @@ def betree.Leaf.split_back := do let p ← - betree.List.split_at_fwd (U64 × U64) content - params.betree_params_split_size + betree.List.split_at (U64 × U64) content params.betree_params_split_size let (content0, content1) := p - let _ ← betree.List.hd_fwd (U64 × U64) content1 - let id0 ← betree.NodeIdCounter.fresh_id_fwd node_id_cnt + let _ ← betree.List.hd (U64 × U64) content1 + let id0 ← betree.NodeIdCounter.fresh_id node_id_cnt let node_id_cnt0 ← betree.NodeIdCounter.fresh_id_back node_id_cnt - let id1 ← betree.NodeIdCounter.fresh_id_fwd node_id_cnt0 - let (st0, _) ← betree.store_leaf_node_fwd id0 content0 st - let _ ← betree.store_leaf_node_fwd id1 content1 st0 + let id1 ← betree.NodeIdCounter.fresh_id node_id_cnt0 + let (st0, _) ← betree.store_leaf_node id0 content0 st + let _ ← betree.store_leaf_node id1 content1 st0 betree.NodeIdCounter.fresh_id_back node_id_cnt0 -/- [betree_main::betree::Node::{5}::lookup_in_bindings] -/ -divergent def betree.Node.lookup_in_bindings_fwd +/- [betree_main::betree::Node::{5}::lookup_in_bindings]: forward function -/ +divergent def betree.Node.lookup_in_bindings (key : U64) (bindings : betree.List (U64 × U64)) : Result (Option U64) := match bindings with | betree.List.Cons hd tl => @@ -235,11 +232,11 @@ divergent def betree.Node.lookup_in_bindings_fwd else if i > key then Result.ret Option.none - else betree.Node.lookup_in_bindings_fwd key tl + else betree.Node.lookup_in_bindings key tl | betree.List.Nil => Result.ret Option.none -/- [betree_main::betree::Node::{5}::lookup_first_message_for_key] -/ -divergent def betree.Node.lookup_first_message_for_key_fwd +/- [betree_main::betree::Node::{5}::lookup_first_message_for_key]: forward function -/ +divergent def betree.Node.lookup_first_message_for_key (key : U64) (msgs : betree.List (U64 × betree.Message)) : Result (betree.List (U64 × betree.Message)) := @@ -248,10 +245,10 @@ divergent def betree.Node.lookup_first_message_for_key_fwd let (i, m) := x if i >= key then Result.ret (betree.List.Cons (i, m) next_msgs) - else betree.Node.lookup_first_message_for_key_fwd key next_msgs + else betree.Node.lookup_first_message_for_key key next_msgs | betree.List.Nil => Result.ret betree.List.Nil -/- [betree_main::betree::Node::{5}::lookup_first_message_for_key] -/ +/- [betree_main::betree::Node::{5}::lookup_first_message_for_key]: backward function 0 -/ divergent def betree.Node.lookup_first_message_for_key_back (key : U64) (msgs : betree.List (U64 × betree.Message)) (ret0 : betree.List (U64 × betree.Message)) : @@ -269,66 +266,66 @@ divergent def betree.Node.lookup_first_message_for_key_back Result.ret (betree.List.Cons (i, m) next_msgs0) | betree.List.Nil => Result.ret ret0 -/- [betree_main::betree::Node::{5}::apply_upserts] -/ -divergent def betree.Node.apply_upserts_fwd +/- [betree_main::betree::Node::{5}::apply_upserts]: forward function -/ +divergent def betree.Node.apply_upserts (msgs : betree.List (U64 × betree.Message)) (prev : Option U64) (key : U64) (st : State) : Result (State × U64) := do - let b ← betree.List.head_has_key_fwd betree.Message msgs key + let b ← betree.List.head_has_key betree.Message msgs key if b then do - let msg ← betree.List.pop_front_fwd (U64 × betree.Message) msgs + let msg ← betree.List.pop_front (U64 × betree.Message) msgs let (_, m) := msg match m with | betree.Message.Insert i => Result.fail Error.panic | betree.Message.Delete => Result.fail Error.panic | betree.Message.Upsert s => do - let v ← betree.upsert_update_fwd prev s + let v ← betree.upsert_update prev s let msgs0 ← betree.List.pop_front_back (U64 × betree.Message) msgs - betree.Node.apply_upserts_fwd msgs0 (Option.some v) key st + betree.Node.apply_upserts msgs0 (Option.some v) key st else do - let (st0, v) ← core.option.Option.unwrap_fwd U64 prev st + let (st0, v) ← core.option.Option.unwrap U64 prev st let _ ← - betree.List.push_front_fwd_back (U64 × betree.Message) msgs (key, + betree.List.push_front (U64 × betree.Message) msgs (key, betree.Message.Insert v) Result.ret (st0, v) -/- [betree_main::betree::Node::{5}::apply_upserts] -/ +/- [betree_main::betree::Node::{5}::apply_upserts]: backward function 0 -/ divergent def betree.Node.apply_upserts_back (msgs : betree.List (U64 × betree.Message)) (prev : Option U64) (key : U64) (st : State) : Result (betree.List (U64 × betree.Message)) := do - let b ← betree.List.head_has_key_fwd betree.Message msgs key + let b ← betree.List.head_has_key betree.Message msgs key if b then do - let msg ← betree.List.pop_front_fwd (U64 × betree.Message) msgs + let msg ← betree.List.pop_front (U64 × betree.Message) msgs let (_, m) := msg match m with | betree.Message.Insert i => Result.fail Error.panic | betree.Message.Delete => Result.fail Error.panic | betree.Message.Upsert s => do - let v ← betree.upsert_update_fwd prev s + let v ← betree.upsert_update prev s let msgs0 ← betree.List.pop_front_back (U64 × betree.Message) msgs betree.Node.apply_upserts_back msgs0 (Option.some v) key st else do - let (_, v) ← core.option.Option.unwrap_fwd U64 prev st - betree.List.push_front_fwd_back (U64 × betree.Message) msgs (key, + let (_, v) ← core.option.Option.unwrap U64 prev st + betree.List.push_front (U64 × betree.Message) msgs (key, betree.Message.Insert v) -/- [betree_main::betree::Node::{5}::lookup] -/ -mutual divergent def betree.Node.lookup_fwd +/- [betree_main::betree::Node::{5}::lookup]: forward function -/ +mutual divergent def betree.Node.lookup (self : betree.Node) (key : U64) (st : State) : Result (State × (Option U64)) := @@ -336,8 +333,8 @@ mutual divergent def betree.Node.lookup_fwd | betree.Node.Internal node => do let ⟨ i, i0, n, n0 ⟩ := node - let (st0, msgs) ← betree.load_internal_node_fwd i st - let pending ← betree.Node.lookup_first_message_for_key_fwd key msgs + let (st0, msgs) ← betree.load_internal_node i st + let pending ← betree.Node.lookup_first_message_for_key key msgs match pending with | betree.List.Cons p l => let (k, msg) := p @@ -345,8 +342,8 @@ mutual divergent def betree.Node.lookup_fwd then do let (st1, opt) ← - betree.Internal.lookup_in_children_fwd (betree.Internal.mk i i0 n - n0) key st0 + betree.Internal.lookup_in_children (betree.Internal.mk i i0 n n0) + key st0 let _ ← betree.Node.lookup_first_message_for_key_back key msgs (betree.List.Cons (k, msg) l) @@ -368,10 +365,10 @@ mutual divergent def betree.Node.lookup_fwd | betree.Message.Upsert ufs => do let (st1, v) ← - betree.Internal.lookup_in_children_fwd (betree.Internal.mk i i0 - n n0) key st0 + betree.Internal.lookup_in_children (betree.Internal.mk i i0 n + n0) key st0 let (st2, v0) ← - betree.Node.apply_upserts_fwd (betree.List.Cons (k, + betree.Node.apply_upserts (betree.List.Cons (k, betree.Message.Upsert ufs) l) v key st1 let node0 ← betree.Internal.lookup_in_children_back (betree.Internal.mk i @@ -382,32 +379,32 @@ mutual divergent def betree.Node.lookup_fwd betree.Message.Upsert ufs) l) v key st1 let msgs0 ← betree.Node.lookup_first_message_for_key_back key msgs pending0 - let (st3, _) ← betree.store_internal_node_fwd i1 msgs0 st2 + let (st3, _) ← betree.store_internal_node i1 msgs0 st2 Result.ret (st3, Option.some v0) | betree.List.Nil => do let (st1, opt) ← - betree.Internal.lookup_in_children_fwd (betree.Internal.mk i i0 n - n0) key st0 + betree.Internal.lookup_in_children (betree.Internal.mk i i0 n n0) + key st0 let _ ← betree.Node.lookup_first_message_for_key_back key msgs betree.List.Nil Result.ret (st1, opt) | betree.Node.Leaf node => do - let (st0, bindings) ← betree.load_leaf_node_fwd node.betree_leaf_id st - let opt ← betree.Node.lookup_in_bindings_fwd key bindings + let (st0, bindings) ← betree.load_leaf_node node.betree_leaf_id st + let opt ← betree.Node.lookup_in_bindings key bindings Result.ret (st0, opt) -/- [betree_main::betree::Node::{5}::lookup] -/ +/- [betree_main::betree::Node::{5}::lookup]: backward function 0 -/ divergent def betree.Node.lookup_back (self : betree.Node) (key : U64) (st : State) : Result betree.Node := match self with | betree.Node.Internal node => do let ⟨ i, i0, n, n0 ⟩ := node - let (st0, msgs) ← betree.load_internal_node_fwd i st - let pending ← betree.Node.lookup_first_message_for_key_fwd key msgs + let (st0, msgs) ← betree.load_internal_node i st + let pending ← betree.Node.lookup_first_message_for_key key msgs match pending with | betree.List.Cons p l => let (k, msg) := p @@ -438,10 +435,10 @@ divergent def betree.Node.lookup_back | betree.Message.Upsert ufs => do let (st1, v) ← - betree.Internal.lookup_in_children_fwd (betree.Internal.mk i i0 - n n0) key st0 + betree.Internal.lookup_in_children (betree.Internal.mk i i0 n + n0) key st0 let (st2, _) ← - betree.Node.apply_upserts_fwd (betree.List.Cons (k, + betree.Node.apply_upserts (betree.List.Cons (k, betree.Message.Upsert ufs) l) v key st1 let node0 ← betree.Internal.lookup_in_children_back (betree.Internal.mk i @@ -452,7 +449,7 @@ divergent def betree.Node.lookup_back betree.Message.Upsert ufs) l) v key st1 let msgs0 ← betree.Node.lookup_first_message_for_key_back key msgs pending0 - let _ ← betree.store_internal_node_fwd i1 msgs0 st2 + let _ ← betree.store_internal_node i1 msgs0 st2 Result.ret (betree.Node.Internal (betree.Internal.mk i1 i2 n1 n2)) | betree.List.Nil => @@ -466,21 +463,21 @@ divergent def betree.Node.lookup_back Result.ret (betree.Node.Internal node0) | betree.Node.Leaf node => do - let (_, bindings) ← betree.load_leaf_node_fwd node.betree_leaf_id st - let _ ← betree.Node.lookup_in_bindings_fwd key bindings + let (_, bindings) ← betree.load_leaf_node node.betree_leaf_id st + let _ ← betree.Node.lookup_in_bindings key bindings Result.ret (betree.Node.Leaf node) -/- [betree_main::betree::Internal::{4}::lookup_in_children] -/ -divergent def betree.Internal.lookup_in_children_fwd +/- [betree_main::betree::Internal::{4}::lookup_in_children]: forward function -/ +divergent def betree.Internal.lookup_in_children (self : betree.Internal) (key : U64) (st : State) : Result (State × (Option U64)) := let ⟨ _, i, n, n0 ⟩ := self if key < i - then betree.Node.lookup_fwd n key st - else betree.Node.lookup_fwd n0 key st + then betree.Node.lookup n key st + else betree.Node.lookup n0 key st -/- [betree_main::betree::Internal::{4}::lookup_in_children] -/ +/- [betree_main::betree::Internal::{4}::lookup_in_children]: backward function 0 -/ divergent def betree.Internal.lookup_in_children_back (self : betree.Internal) (key : U64) (st : State) : Result betree.Internal := let ⟨ i, i0, n, n0 ⟩ := self @@ -496,8 +493,8 @@ divergent def betree.Internal.lookup_in_children_back end -/- [betree_main::betree::Node::{5}::lookup_mut_in_bindings] -/ -divergent def betree.Node.lookup_mut_in_bindings_fwd +/- [betree_main::betree::Node::{5}::lookup_mut_in_bindings]: forward function -/ +divergent def betree.Node.lookup_mut_in_bindings (key : U64) (bindings : betree.List (U64 × U64)) : Result (betree.List (U64 × U64)) := @@ -506,10 +503,10 @@ divergent def betree.Node.lookup_mut_in_bindings_fwd let (i, i0) := hd if i >= key then Result.ret (betree.List.Cons (i, i0) tl) - else betree.Node.lookup_mut_in_bindings_fwd key tl + else betree.Node.lookup_mut_in_bindings key tl | betree.List.Nil => Result.ret betree.List.Nil -/- [betree_main::betree::Node::{5}::lookup_mut_in_bindings] -/ +/- [betree_main::betree::Node::{5}::lookup_mut_in_bindings]: backward function 0 -/ divergent def betree.Node.lookup_mut_in_bindings_back (key : U64) (bindings : betree.List (U64 × U64)) (ret0 : betree.List (U64 × U64)) : @@ -526,25 +523,26 @@ divergent def betree.Node.lookup_mut_in_bindings_back Result.ret (betree.List.Cons (i, i0) tl0) | betree.List.Nil => Result.ret ret0 -/- [betree_main::betree::Node::{5}::apply_to_leaf] -/ -def betree.Node.apply_to_leaf_fwd_back +/- [betree_main::betree::Node::{5}::apply_to_leaf]: merged forward/backward function + (there is a single backward function, and the forward function returns ()) -/ +def betree.Node.apply_to_leaf (bindings : betree.List (U64 × U64)) (key : U64) (new_msg : betree.Message) : Result (betree.List (U64 × U64)) := do - let bindings0 ← betree.Node.lookup_mut_in_bindings_fwd key bindings - let b ← betree.List.head_has_key_fwd U64 bindings0 key + let bindings0 ← betree.Node.lookup_mut_in_bindings key bindings + let b ← betree.List.head_has_key U64 bindings0 key if b then do - let hd ← betree.List.pop_front_fwd (U64 × U64) bindings0 + let hd ← betree.List.pop_front (U64 × U64) bindings0 match new_msg with | betree.Message.Insert v => do let bindings1 ← betree.List.pop_front_back (U64 × U64) bindings0 let bindings2 ← - betree.List.push_front_fwd_back (U64 × U64) bindings1 (key, v) + betree.List.push_front (U64 × U64) bindings1 (key, v) betree.Node.lookup_mut_in_bindings_back key bindings bindings2 | betree.Message.Delete => do @@ -553,29 +551,30 @@ def betree.Node.apply_to_leaf_fwd_back | betree.Message.Upsert s => do let (_, i) := hd - let v ← betree.upsert_update_fwd (Option.some i) s + let v ← betree.upsert_update (Option.some i) s let bindings1 ← betree.List.pop_front_back (U64 × U64) bindings0 let bindings2 ← - betree.List.push_front_fwd_back (U64 × U64) bindings1 (key, v) + betree.List.push_front (U64 × U64) bindings1 (key, v) betree.Node.lookup_mut_in_bindings_back key bindings bindings2 else match new_msg with | betree.Message.Insert v => do let bindings1 ← - betree.List.push_front_fwd_back (U64 × U64) bindings0 (key, v) + betree.List.push_front (U64 × U64) bindings0 (key, v) betree.Node.lookup_mut_in_bindings_back key bindings bindings1 | betree.Message.Delete => betree.Node.lookup_mut_in_bindings_back key bindings bindings0 | betree.Message.Upsert s => do - let v ← betree.upsert_update_fwd Option.none s + let v ← betree.upsert_update Option.none s let bindings1 ← - betree.List.push_front_fwd_back (U64 × U64) bindings0 (key, v) + betree.List.push_front (U64 × U64) bindings0 (key, v) betree.Node.lookup_mut_in_bindings_back key bindings bindings1 -/- [betree_main::betree::Node::{5}::apply_messages_to_leaf] -/ -divergent def betree.Node.apply_messages_to_leaf_fwd_back +/- [betree_main::betree::Node::{5}::apply_messages_to_leaf]: merged forward/backward function + (there is a single backward function, and the forward function returns ()) -/ +divergent def betree.Node.apply_messages_to_leaf (bindings : betree.List (U64 × U64)) (new_msgs : betree.List (U64 × betree.Message)) : Result (betree.List (U64 × U64)) @@ -584,12 +583,13 @@ divergent def betree.Node.apply_messages_to_leaf_fwd_back | betree.List.Cons new_msg new_msgs_tl => do let (i, m) := new_msg - let bindings0 ← betree.Node.apply_to_leaf_fwd_back bindings i m - betree.Node.apply_messages_to_leaf_fwd_back bindings0 new_msgs_tl + let bindings0 ← betree.Node.apply_to_leaf bindings i m + betree.Node.apply_messages_to_leaf bindings0 new_msgs_tl | betree.List.Nil => Result.ret bindings -/- [betree_main::betree::Node::{5}::filter_messages_for_key] -/ -divergent def betree.Node.filter_messages_for_key_fwd_back +/- [betree_main::betree::Node::{5}::filter_messages_for_key]: merged forward/backward function + (there is a single backward function, and the forward function returns ()) -/ +divergent def betree.Node.filter_messages_for_key (key : U64) (msgs : betree.List (U64 × betree.Message)) : Result (betree.List (U64 × betree.Message)) := @@ -602,12 +602,12 @@ divergent def betree.Node.filter_messages_for_key_fwd_back let msgs0 ← betree.List.pop_front_back (U64 × betree.Message) (betree.List.Cons (k, m) l) - betree.Node.filter_messages_for_key_fwd_back key msgs0 + betree.Node.filter_messages_for_key key msgs0 else Result.ret (betree.List.Cons (k, m) l) | betree.List.Nil => Result.ret betree.List.Nil -/- [betree_main::betree::Node::{5}::lookup_first_message_after_key] -/ -divergent def betree.Node.lookup_first_message_after_key_fwd +/- [betree_main::betree::Node::{5}::lookup_first_message_after_key]: forward function -/ +divergent def betree.Node.lookup_first_message_after_key (key : U64) (msgs : betree.List (U64 × betree.Message)) : Result (betree.List (U64 × betree.Message)) := @@ -615,11 +615,11 @@ divergent def betree.Node.lookup_first_message_after_key_fwd | betree.List.Cons p next_msgs => let (k, m) := p if k = key - then betree.Node.lookup_first_message_after_key_fwd key next_msgs + then betree.Node.lookup_first_message_after_key key next_msgs else Result.ret (betree.List.Cons (k, m) next_msgs) | betree.List.Nil => Result.ret betree.List.Nil -/- [betree_main::betree::Node::{5}::lookup_first_message_after_key] -/ +/- [betree_main::betree::Node::{5}::lookup_first_message_after_key]: backward function 0 -/ divergent def betree.Node.lookup_first_message_after_key_back (key : U64) (msgs : betree.List (U64 × betree.Message)) (ret0 : betree.List (U64 × betree.Message)) : @@ -637,74 +637,75 @@ divergent def betree.Node.lookup_first_message_after_key_back else Result.ret ret0 | betree.List.Nil => Result.ret ret0 -/- [betree_main::betree::Node::{5}::apply_to_internal] -/ -def betree.Node.apply_to_internal_fwd_back +/- [betree_main::betree::Node::{5}::apply_to_internal]: merged forward/backward function + (there is a single backward function, and the forward function returns ()) -/ +def betree.Node.apply_to_internal (msgs : betree.List (U64 × betree.Message)) (key : U64) (new_msg : betree.Message) : Result (betree.List (U64 × betree.Message)) := do - let msgs0 ← betree.Node.lookup_first_message_for_key_fwd key msgs - let b ← betree.List.head_has_key_fwd betree.Message msgs0 key + let msgs0 ← betree.Node.lookup_first_message_for_key key msgs + let b ← betree.List.head_has_key betree.Message msgs0 key if b then match new_msg with | betree.Message.Insert i => do - let msgs1 ← betree.Node.filter_messages_for_key_fwd_back key msgs0 + let msgs1 ← betree.Node.filter_messages_for_key key msgs0 let msgs2 ← - betree.List.push_front_fwd_back (U64 × betree.Message) msgs1 (key, + betree.List.push_front (U64 × betree.Message) msgs1 (key, betree.Message.Insert i) betree.Node.lookup_first_message_for_key_back key msgs msgs2 | betree.Message.Delete => do - let msgs1 ← betree.Node.filter_messages_for_key_fwd_back key msgs0 + let msgs1 ← betree.Node.filter_messages_for_key key msgs0 let msgs2 ← - betree.List.push_front_fwd_back (U64 × betree.Message) msgs1 (key, + betree.List.push_front (U64 × betree.Message) msgs1 (key, betree.Message.Delete) betree.Node.lookup_first_message_for_key_back key msgs msgs2 | betree.Message.Upsert s => do - let p ← betree.List.hd_fwd (U64 × betree.Message) msgs0 + let p ← betree.List.hd (U64 × betree.Message) msgs0 let (_, m) := p match m with | betree.Message.Insert prev => do - let v ← betree.upsert_update_fwd (Option.some prev) s + let v ← betree.upsert_update (Option.some prev) s let msgs1 ← betree.List.pop_front_back (U64 × betree.Message) msgs0 let msgs2 ← - betree.List.push_front_fwd_back (U64 × betree.Message) msgs1 - (key, betree.Message.Insert v) + betree.List.push_front (U64 × betree.Message) msgs1 (key, + betree.Message.Insert v) betree.Node.lookup_first_message_for_key_back key msgs msgs2 | betree.Message.Delete => do - let v ← betree.upsert_update_fwd Option.none s + let v ← betree.upsert_update Option.none s let msgs1 ← betree.List.pop_front_back (U64 × betree.Message) msgs0 let msgs2 ← - betree.List.push_front_fwd_back (U64 × betree.Message) msgs1 - (key, betree.Message.Insert v) + betree.List.push_front (U64 × betree.Message) msgs1 (key, + betree.Message.Insert v) betree.Node.lookup_first_message_for_key_back key msgs msgs2 | betree.Message.Upsert ufs => do let msgs1 ← - betree.Node.lookup_first_message_after_key_fwd key msgs0 + betree.Node.lookup_first_message_after_key key msgs0 let msgs2 ← - betree.List.push_front_fwd_back (U64 × betree.Message) msgs1 - (key, betree.Message.Upsert s) + betree.List.push_front (U64 × betree.Message) msgs1 (key, + betree.Message.Upsert s) let msgs3 ← betree.Node.lookup_first_message_after_key_back key msgs0 msgs2 betree.Node.lookup_first_message_for_key_back key msgs msgs3 else do let msgs1 ← - betree.List.push_front_fwd_back (U64 × betree.Message) msgs0 (key, - new_msg) + betree.List.push_front (U64 × betree.Message) msgs0 (key, new_msg) betree.Node.lookup_first_message_for_key_back key msgs msgs1 -/- [betree_main::betree::Node::{5}::apply_messages_to_internal] -/ -divergent def betree.Node.apply_messages_to_internal_fwd_back +/- [betree_main::betree::Node::{5}::apply_messages_to_internal]: merged forward/backward function + (there is a single backward function, and the forward function returns ()) -/ +divergent def betree.Node.apply_messages_to_internal (msgs : betree.List (U64 × betree.Message)) (new_msgs : betree.List (U64 × betree.Message)) : Result (betree.List (U64 × betree.Message)) @@ -713,12 +714,12 @@ divergent def betree.Node.apply_messages_to_internal_fwd_back | betree.List.Cons new_msg new_msgs_tl => do let (i, m) := new_msg - let msgs0 ← betree.Node.apply_to_internal_fwd_back msgs i m - betree.Node.apply_messages_to_internal_fwd_back msgs0 new_msgs_tl + let msgs0 ← betree.Node.apply_to_internal msgs i m + betree.Node.apply_messages_to_internal msgs0 new_msgs_tl | betree.List.Nil => Result.ret msgs -/- [betree_main::betree::Node::{5}::apply_messages] -/ -mutual divergent def betree.Node.apply_messages_fwd +/- [betree_main::betree::Node::{5}::apply_messages]: forward function -/ +mutual divergent def betree.Node.apply_messages (self : betree.Node) (params : betree.Params) (node_id_cnt : betree.NodeIdCounter) (msgs : betree.List (U64 × betree.Message)) (st : State) : @@ -728,47 +729,46 @@ mutual divergent def betree.Node.apply_messages_fwd | betree.Node.Internal node => do let ⟨ i, i0, n, n0 ⟩ := node - let (st0, content) ← betree.load_internal_node_fwd i st - let content0 ← - betree.Node.apply_messages_to_internal_fwd_back content msgs - let num_msgs ← betree.List.len_fwd (U64 × betree.Message) content0 + let (st0, content) ← betree.load_internal_node i st + let content0 ← betree.Node.apply_messages_to_internal content msgs + let num_msgs ← betree.List.len (U64 × betree.Message) content0 if num_msgs >= params.betree_params_min_flush_size then do let (st1, content1) ← - betree.Internal.flush_fwd (betree.Internal.mk i i0 n n0) params + betree.Internal.flush (betree.Internal.mk i i0 n n0) params node_id_cnt content0 st0 let (node0, _) ← betree.Internal.flush_back (betree.Internal.mk i i0 n n0) params node_id_cnt content0 st0 let ⟨ i1, _, _, _ ⟩ := node0 - let (st2, _) ← betree.store_internal_node_fwd i1 content1 st1 + let (st2, _) ← betree.store_internal_node i1 content1 st1 Result.ret (st2, ()) else do - let (st1, _) ← betree.store_internal_node_fwd i content0 st0 + let (st1, _) ← betree.store_internal_node i content0 st0 Result.ret (st1, ()) | betree.Node.Leaf node => do - let (st0, content) ← betree.load_leaf_node_fwd node.betree_leaf_id st - let content0 ← betree.Node.apply_messages_to_leaf_fwd_back content msgs - let len ← betree.List.len_fwd (U64 × U64) content0 + let (st0, content) ← betree.load_leaf_node node.betree_leaf_id st + let content0 ← betree.Node.apply_messages_to_leaf content msgs + let len ← betree.List.len (U64 × U64) content0 let i ← (U64.ofInt 2 (by intlit)) * params.betree_params_split_size if len >= i then do let (st1, _) ← - betree.Leaf.split_fwd node content0 params node_id_cnt st0 + betree.Leaf.split node content0 params node_id_cnt st0 let (st2, _) ← - betree.store_leaf_node_fwd node.betree_leaf_id betree.List.Nil st1 + betree.store_leaf_node node.betree_leaf_id betree.List.Nil st1 Result.ret (st2, ()) else do let (st1, _) ← - betree.store_leaf_node_fwd node.betree_leaf_id content0 st0 + betree.store_leaf_node node.betree_leaf_id content0 st0 Result.ret (st1, ()) -/- [betree_main::betree::Node::{5}::apply_messages] -/ +/- [betree_main::betree::Node::{5}::apply_messages]: backward function 0 -/ divergent def betree.Node.apply_messages_back (self : betree.Node) (params : betree.Params) (node_id_cnt : betree.NodeIdCounter) @@ -779,52 +779,51 @@ divergent def betree.Node.apply_messages_back | betree.Node.Internal node => do let ⟨ i, i0, n, n0 ⟩ := node - let (st0, content) ← betree.load_internal_node_fwd i st - let content0 ← - betree.Node.apply_messages_to_internal_fwd_back content msgs - let num_msgs ← betree.List.len_fwd (U64 × betree.Message) content0 + let (st0, content) ← betree.load_internal_node i st + let content0 ← betree.Node.apply_messages_to_internal content msgs + let num_msgs ← betree.List.len (U64 × betree.Message) content0 if num_msgs >= params.betree_params_min_flush_size then do let (st1, content1) ← - betree.Internal.flush_fwd (betree.Internal.mk i i0 n n0) params + betree.Internal.flush (betree.Internal.mk i i0 n n0) params node_id_cnt content0 st0 let (node0, node_id_cnt0) ← betree.Internal.flush_back (betree.Internal.mk i i0 n n0) params node_id_cnt content0 st0 let ⟨ i1, i2, n1, n2 ⟩ := node0 - let _ ← betree.store_internal_node_fwd i1 content1 st1 + let _ ← betree.store_internal_node i1 content1 st1 Result.ret (betree.Node.Internal (betree.Internal.mk i1 i2 n1 n2), node_id_cnt0) else do - let _ ← betree.store_internal_node_fwd i content0 st0 + let _ ← betree.store_internal_node i content0 st0 Result.ret (betree.Node.Internal (betree.Internal.mk i i0 n n0), node_id_cnt) | betree.Node.Leaf node => do - let (st0, content) ← betree.load_leaf_node_fwd node.betree_leaf_id st - let content0 ← betree.Node.apply_messages_to_leaf_fwd_back content msgs - let len ← betree.List.len_fwd (U64 × U64) content0 + let (st0, content) ← betree.load_leaf_node node.betree_leaf_id st + let content0 ← betree.Node.apply_messages_to_leaf content msgs + let len ← betree.List.len (U64 × U64) content0 let i ← (U64.ofInt 2 (by intlit)) * params.betree_params_split_size if len >= i then do let (st1, new_node) ← - betree.Leaf.split_fwd node content0 params node_id_cnt st0 + betree.Leaf.split node content0 params node_id_cnt st0 let _ ← - betree.store_leaf_node_fwd node.betree_leaf_id betree.List.Nil st1 + betree.store_leaf_node node.betree_leaf_id betree.List.Nil st1 let node_id_cnt0 ← betree.Leaf.split_back node content0 params node_id_cnt st0 Result.ret (betree.Node.Internal new_node, node_id_cnt0) else do - let _ ← betree.store_leaf_node_fwd node.betree_leaf_id content0 st0 + let _ ← betree.store_leaf_node node.betree_leaf_id content0 st0 Result.ret (betree.Node.Leaf { node with betree_leaf_size := len }, node_id_cnt) -/- [betree_main::betree::Internal::{4}::flush] -/ -divergent def betree.Internal.flush_fwd +/- [betree_main::betree::Internal::{4}::flush]: forward function -/ +divergent def betree.Internal.flush (self : betree.Internal) (params : betree.Params) (node_id_cnt : betree.NodeIdCounter) (content : betree.List (U64 × betree.Message)) (st : State) : @@ -832,24 +831,22 @@ divergent def betree.Internal.flush_fwd := do let ⟨ _, i, n, n0 ⟩ := self - let p ← betree.List.partition_at_pivot_fwd betree.Message content i + let p ← betree.List.partition_at_pivot betree.Message content i let (msgs_left, msgs_right) := p - let len_left ← betree.List.len_fwd (U64 × betree.Message) msgs_left + let len_left ← betree.List.len (U64 × betree.Message) msgs_left if len_left >= params.betree_params_min_flush_size then do let (st0, _) ← - betree.Node.apply_messages_fwd n params node_id_cnt msgs_left st + betree.Node.apply_messages n params node_id_cnt msgs_left st let (_, node_id_cnt0) ← betree.Node.apply_messages_back n params node_id_cnt msgs_left st - let len_right ← - betree.List.len_fwd (U64 × betree.Message) msgs_right + let len_right ← betree.List.len (U64 × betree.Message) msgs_right if len_right >= params.betree_params_min_flush_size then do let (st1, _) ← - betree.Node.apply_messages_fwd n0 params node_id_cnt0 msgs_right - st0 + betree.Node.apply_messages n0 params node_id_cnt0 msgs_right st0 let _ ← betree.Node.apply_messages_back n0 params node_id_cnt0 msgs_right st0 @@ -858,12 +855,12 @@ divergent def betree.Internal.flush_fwd else do let (st0, _) ← - betree.Node.apply_messages_fwd n0 params node_id_cnt msgs_right st + betree.Node.apply_messages n0 params node_id_cnt msgs_right st let _ ← betree.Node.apply_messages_back n0 params node_id_cnt msgs_right st Result.ret (st0, msgs_left) -/- [betree_main::betree::Internal::{4}::flush] -/ +/- [betree_main::betree::Internal::{4}::flush]: backward function 0 -/ divergent def betree.Internal.flush_back (self : betree.Internal) (params : betree.Params) (node_id_cnt : betree.NodeIdCounter) @@ -872,18 +869,17 @@ divergent def betree.Internal.flush_back := do let ⟨ i, i0, n, n0 ⟩ := self - let p ← betree.List.partition_at_pivot_fwd betree.Message content i0 + let p ← betree.List.partition_at_pivot betree.Message content i0 let (msgs_left, msgs_right) := p - let len_left ← betree.List.len_fwd (U64 × betree.Message) msgs_left + let len_left ← betree.List.len (U64 × betree.Message) msgs_left if len_left >= params.betree_params_min_flush_size then do let (st0, _) ← - betree.Node.apply_messages_fwd n params node_id_cnt msgs_left st + betree.Node.apply_messages n params node_id_cnt msgs_left st let (n1, node_id_cnt0) ← betree.Node.apply_messages_back n params node_id_cnt msgs_left st - let len_right ← - betree.List.len_fwd (U64 × betree.Message) msgs_right + let len_right ← betree.List.len (U64 × betree.Message) msgs_right if len_right >= params.betree_params_min_flush_size then do @@ -900,8 +896,8 @@ divergent def betree.Internal.flush_back end -/- [betree_main::betree::Node::{5}::apply] -/ -def betree.Node.apply_fwd +/- [betree_main::betree::Node::{5}::apply]: forward function -/ +def betree.Node.apply (self : betree.Node) (params : betree.Params) (node_id_cnt : betree.NodeIdCounter) (key : U64) (new_msg : betree.Message) (st : State) : @@ -910,14 +906,14 @@ def betree.Node.apply_fwd do let l := betree.List.Nil let (st0, _) ← - betree.Node.apply_messages_fwd self params node_id_cnt (betree.List.Cons + betree.Node.apply_messages self params node_id_cnt (betree.List.Cons (key, new_msg) l) st let _ ← betree.Node.apply_messages_back self params node_id_cnt (betree.List.Cons (key, new_msg) l) st Result.ret (st0, ()) -/- [betree_main::betree::Node::{5}::apply] -/ +/- [betree_main::betree::Node::{5}::apply]: backward function 0 -/ def betree.Node.apply_back (self : betree.Node) (params : betree.Params) (node_id_cnt : betree.NodeIdCounter) (key : U64) (new_msg : betree.Message) @@ -928,15 +924,15 @@ def betree.Node.apply_back betree.Node.apply_messages_back self params node_id_cnt (betree.List.Cons (key, new_msg) l) st -/- [betree_main::betree::BeTree::{6}::new] -/ -def betree.BeTree.new_fwd +/- [betree_main::betree::BeTree::{6}::new]: forward function -/ +def betree.BeTree.new (min_flush_size : U64) (split_size : U64) (st : State) : Result (State × betree.BeTree) := do - let node_id_cnt ← betree.NodeIdCounter.new_fwd - let id ← betree.NodeIdCounter.fresh_id_fwd node_id_cnt - let (st0, _) ← betree.store_leaf_node_fwd id betree.List.Nil st + let node_id_cnt ← betree.NodeIdCounter.new + let id ← betree.NodeIdCounter.fresh_id node_id_cnt + let (st0, _) ← betree.store_leaf_node id betree.List.Nil st let node_id_cnt0 ← betree.NodeIdCounter.fresh_id_back node_id_cnt Result.ret (st0, { @@ -954,21 +950,21 @@ def betree.BeTree.new_fwd }) }) -/- [betree_main::betree::BeTree::{6}::apply] -/ -def betree.BeTree.apply_fwd +/- [betree_main::betree::BeTree::{6}::apply]: forward function -/ +def betree.BeTree.apply (self : betree.BeTree) (key : U64) (msg : betree.Message) (st : State) : Result (State × Unit) := do let (st0, _) ← - betree.Node.apply_fwd self.betree_be_tree_root self.betree_be_tree_params + betree.Node.apply self.betree_be_tree_root self.betree_be_tree_params self.betree_be_tree_node_id_cnt key msg st let _ ← betree.Node.apply_back self.betree_be_tree_root self.betree_be_tree_params self.betree_be_tree_node_id_cnt key msg st Result.ret (st0, ()) -/- [betree_main::betree::BeTree::{6}::apply] -/ +/- [betree_main::betree::BeTree::{6}::apply]: backward function 0 -/ def betree.BeTree.apply_back (self : betree.BeTree) (key : U64) (msg : betree.Message) (st : State) : Result betree.BeTree @@ -980,51 +976,51 @@ def betree.BeTree.apply_back Result.ret { self with betree_be_tree_node_id_cnt := nic, betree_be_tree_root := n } -/- [betree_main::betree::BeTree::{6}::insert] -/ -def betree.BeTree.insert_fwd +/- [betree_main::betree::BeTree::{6}::insert]: forward function -/ +def betree.BeTree.insert (self : betree.BeTree) (key : U64) (value : U64) (st : State) : Result (State × Unit) := do let (st0, _) ← - betree.BeTree.apply_fwd self key (betree.Message.Insert value) st + betree.BeTree.apply self key (betree.Message.Insert value) st let _ ← betree.BeTree.apply_back self key (betree.Message.Insert value) st Result.ret (st0, ()) -/- [betree_main::betree::BeTree::{6}::insert] -/ +/- [betree_main::betree::BeTree::{6}::insert]: backward function 0 -/ def betree.BeTree.insert_back (self : betree.BeTree) (key : U64) (value : U64) (st : State) : Result betree.BeTree := betree.BeTree.apply_back self key (betree.Message.Insert value) st -/- [betree_main::betree::BeTree::{6}::delete] -/ -def betree.BeTree.delete_fwd +/- [betree_main::betree::BeTree::{6}::delete]: forward function -/ +def betree.BeTree.delete (self : betree.BeTree) (key : U64) (st : State) : Result (State × Unit) := do - let (st0, _) ← betree.BeTree.apply_fwd self key betree.Message.Delete st + let (st0, _) ← betree.BeTree.apply self key betree.Message.Delete st let _ ← betree.BeTree.apply_back self key betree.Message.Delete st Result.ret (st0, ()) -/- [betree_main::betree::BeTree::{6}::delete] -/ +/- [betree_main::betree::BeTree::{6}::delete]: backward function 0 -/ def betree.BeTree.delete_back (self : betree.BeTree) (key : U64) (st : State) : Result betree.BeTree := betree.BeTree.apply_back self key betree.Message.Delete st -/- [betree_main::betree::BeTree::{6}::upsert] -/ -def betree.BeTree.upsert_fwd +/- [betree_main::betree::BeTree::{6}::upsert]: forward function -/ +def betree.BeTree.upsert (self : betree.BeTree) (key : U64) (upd : betree.UpsertFunState) (st : State) : Result (State × Unit) := do let (st0, _) ← - betree.BeTree.apply_fwd self key (betree.Message.Upsert upd) st + betree.BeTree.apply self key (betree.Message.Upsert upd) st let _ ← betree.BeTree.apply_back self key (betree.Message.Upsert upd) st Result.ret (st0, ()) -/- [betree_main::betree::BeTree::{6}::upsert] -/ +/- [betree_main::betree::BeTree::{6}::upsert]: backward function 0 -/ def betree.BeTree.upsert_back (self : betree.BeTree) (key : U64) (upd : betree.UpsertFunState) (st : State) : @@ -1032,25 +1028,25 @@ def betree.BeTree.upsert_back := betree.BeTree.apply_back self key (betree.Message.Upsert upd) st -/- [betree_main::betree::BeTree::{6}::lookup] -/ -def betree.BeTree.lookup_fwd +/- [betree_main::betree::BeTree::{6}::lookup]: forward function -/ +def betree.BeTree.lookup (self : betree.BeTree) (key : U64) (st : State) : Result (State × (Option U64)) := - betree.Node.lookup_fwd self.betree_be_tree_root key st + betree.Node.lookup self.betree_be_tree_root key st -/- [betree_main::betree::BeTree::{6}::lookup] -/ +/- [betree_main::betree::BeTree::{6}::lookup]: backward function 0 -/ def betree.BeTree.lookup_back (self : betree.BeTree) (key : U64) (st : State) : Result betree.BeTree := do let n ← betree.Node.lookup_back self.betree_be_tree_root key st Result.ret { self with betree_be_tree_root := n } -/- [betree_main::main] -/ -def main_fwd : Result Unit := +/- [betree_main::main]: forward function -/ +def main : Result Unit := Result.ret () /- Unit test for [betree_main::main] -/ -#assert (main_fwd == .ret ()) +#assert (main == .ret ()) end betree_main diff --git a/tests/lean/BetreeMain/FunsExternal.lean b/tests/lean/BetreeMain/FunsExternal.lean index 7bcba380..71d26da4 100644 --- a/tests/lean/BetreeMain/FunsExternal.lean +++ b/tests/lean/BetreeMain/FunsExternal.lean @@ -7,29 +7,29 @@ open betree_main -- TODO: fill those bodies /- [betree_main::betree_utils::load_internal_node] -/ -def betree_utils.load_internal_node_fwd +def betree_utils.load_internal_node : U64 → State → Result (State × (betree.List (U64 × betree.Message))) := fun _ _ => .fail .panic /- [betree_main::betree_utils::store_internal_node] -/ -def betree_utils.store_internal_node_fwd +def betree_utils.store_internal_node : U64 → betree.List (U64 × betree.Message) → State → Result (State × Unit) := fun _ _ _ => .fail .panic /- [betree_main::betree_utils::load_leaf_node] -/ -def betree_utils.load_leaf_node_fwd +def betree_utils.load_leaf_node : U64 → State → Result (State × (betree.List (U64 × U64))) := fun _ _ => .fail .panic /- [betree_main::betree_utils::store_leaf_node] -/ -def betree_utils.store_leaf_node_fwd +def betree_utils.store_leaf_node : U64 → betree.List (U64 × U64) → State → Result (State × Unit) := fun _ _ _ => .fail .panic /- [core::option::Option::{0}::unwrap] -/ -def core.option.Option.unwrap_fwd +def core.option.Option.unwrap (T : Type) : Option T → State → Result (State × T) := fun _ _ => .fail .panic diff --git a/tests/lean/BetreeMain/FunsExternal_Template.lean b/tests/lean/BetreeMain/FunsExternal_Template.lean index d768bb20..430d2dda 100644 --- a/tests/lean/BetreeMain/FunsExternal_Template.lean +++ b/tests/lean/BetreeMain/FunsExternal_Template.lean @@ -6,25 +6,25 @@ import BetreeMain.Types open Primitives open betree_main -/- [betree_main::betree_utils::load_internal_node] -/ -axiom betree_utils.load_internal_node_fwd +/- [betree_main::betree_utils::load_internal_node]: forward function -/ +axiom betree_utils.load_internal_node : U64 → State → Result (State × (betree.List (U64 × betree.Message))) -/- [betree_main::betree_utils::store_internal_node] -/ -axiom betree_utils.store_internal_node_fwd +/- [betree_main::betree_utils::store_internal_node]: forward function -/ +axiom betree_utils.store_internal_node : U64 → betree.List (U64 × betree.Message) → State → Result (State × Unit) -/- [betree_main::betree_utils::load_leaf_node] -/ -axiom betree_utils.load_leaf_node_fwd +/- [betree_main::betree_utils::load_leaf_node]: forward function -/ +axiom betree_utils.load_leaf_node : U64 → State → Result (State × (betree.List (U64 × U64))) -/- [betree_main::betree_utils::store_leaf_node] -/ -axiom betree_utils.store_leaf_node_fwd +/- [betree_main::betree_utils::store_leaf_node]: forward function -/ +axiom betree_utils.store_leaf_node : U64 → betree.List (U64 × U64) → State → Result (State × Unit) -/- [core::option::Option::{0}::unwrap] -/ -axiom core.option.Option.unwrap_fwd +/- [core::option::Option::{0}::unwrap]: forward function -/ +axiom core.option.Option.unwrap (T : Type) : Option T → State → Result (State × T) diff --git a/tests/lean/Constants.lean b/tests/lean/Constants.lean index 7014b0d8..f37c9204 100644 --- a/tests/lean/Constants.lean +++ b/tests/lean/Constants.lean @@ -21,16 +21,16 @@ def x1_c : U32 := eval_global x1_body (by simp) def x2_body : Result U32 := Result.ret (U32.ofInt 3 (by intlit)) def x2_c : U32 := eval_global x2_body (by simp) -/- [constants::incr] -/ -def incr_fwd (n : U32) : Result U32 := +/- [constants::incr]: forward function -/ +def incr (n : U32) : Result U32 := n + (U32.ofInt 1 (by intlit)) /- [constants::X3] -/ -def x3_body : Result U32 := incr_fwd (U32.ofInt 32 (by intlit)) +def x3_body : Result U32 := incr (U32.ofInt 32 (by intlit)) def x3_c : U32 := eval_global x3_body (by simp) -/- [constants::mk_pair0] -/ -def mk_pair0_fwd (x : U32) (y : U32) : Result (U32 × U32) := +/- [constants::mk_pair0]: forward function -/ +def mk_pair0 (x : U32) (y : U32) : Result (U32 × U32) := Result.ret (x, y) /- [constants::Pair] -/ @@ -38,18 +38,18 @@ structure Pair (T1 T2 : Type) where pair_x : T1 pair_y : T2 -/- [constants::mk_pair1] -/ -def mk_pair1_fwd (x : U32) (y : U32) : Result (Pair U32 U32) := +/- [constants::mk_pair1]: forward function -/ +def mk_pair1 (x : U32) (y : U32) : Result (Pair U32 U32) := Result.ret { pair_x := x, pair_y := y } /- [constants::P0] -/ def p0_body : Result (U32 × U32) := - mk_pair0_fwd (U32.ofInt 0 (by intlit)) (U32.ofInt 1 (by intlit)) + mk_pair0 (U32.ofInt 0 (by intlit)) (U32.ofInt 1 (by intlit)) def p0_c : (U32 × U32) := eval_global p0_body (by simp) /- [constants::P1] -/ def p1_body : Result (Pair U32 U32) := - mk_pair1_fwd (U32.ofInt 0 (by intlit)) (U32.ofInt 1 (by intlit)) + mk_pair1 (U32.ofInt 0 (by intlit)) (U32.ofInt 1 (by intlit)) def p1_c : Pair U32 U32 := eval_global p1_body (by simp) /- [constants::P2] -/ @@ -67,32 +67,32 @@ def p3_c : Pair U32 U32 := eval_global p3_body (by simp) structure Wrap (T : Type) where wrap_val : T -/- [constants::Wrap::{0}::new] -/ -def Wrap.new_fwd (T : Type) (val : T) : Result (Wrap T) := +/- [constants::Wrap::{0}::new]: forward function -/ +def Wrap.new (T : Type) (val : T) : Result (Wrap T) := Result.ret { wrap_val := val } /- [constants::Y] -/ -def y_body : Result (Wrap I32) := Wrap.new_fwd I32 (I32.ofInt 2 (by intlit)) +def y_body : Result (Wrap I32) := Wrap.new I32 (I32.ofInt 2 (by intlit)) def y_c : Wrap I32 := eval_global y_body (by simp) -/- [constants::unwrap_y] -/ -def unwrap_y_fwd : Result I32 := +/- [constants::unwrap_y]: forward function -/ +def unwrap_y : Result I32 := Result.ret y_c.wrap_val /- [constants::YVAL] -/ -def yval_body : Result I32 := unwrap_y_fwd +def yval_body : Result I32 := unwrap_y def yval_c : I32 := eval_global yval_body (by simp) /- [constants::get_z1::Z1] -/ def get_z1_z1_body : Result I32 := Result.ret (I32.ofInt 3 (by intlit)) def get_z1_z1_c : I32 := eval_global get_z1_z1_body (by simp) -/- [constants::get_z1] -/ -def get_z1_fwd : Result I32 := +/- [constants::get_z1]: forward function -/ +def get_z1 : Result I32 := Result.ret get_z1_z1_c -/- [constants::add] -/ -def add_fwd (a : I32) (b : I32) : Result I32 := +/- [constants::add]: forward function -/ +def add (a : I32) (b : I32) : Result I32 := a + b /- [constants::Q1] -/ @@ -104,22 +104,22 @@ def q2_body : Result I32 := Result.ret q1_c def q2_c : I32 := eval_global q2_body (by simp) /- [constants::Q3] -/ -def q3_body : Result I32 := add_fwd q2_c (I32.ofInt 3 (by intlit)) +def q3_body : Result I32 := add q2_c (I32.ofInt 3 (by intlit)) def q3_c : I32 := eval_global q3_body (by simp) -/- [constants::get_z2] -/ -def get_z2_fwd : Result I32 := +/- [constants::get_z2]: forward function -/ +def get_z2 : Result I32 := do - let i ← get_z1_fwd - let i0 ← add_fwd i q3_c - add_fwd q1_c i0 + let i ← get_z1 + let i0 ← add i q3_c + add q1_c i0 /- [constants::S1] -/ def s1_body : Result U32 := Result.ret (U32.ofInt 6 (by intlit)) def s1_c : U32 := eval_global s1_body (by simp) /- [constants::S2] -/ -def s2_body : Result U32 := incr_fwd s1_c +def s2_body : Result U32 := incr s1_c def s2_c : U32 := eval_global s2_body (by simp) /- [constants::S3] -/ @@ -128,7 +128,7 @@ def s3_c : Pair U32 U32 := eval_global s3_body (by simp) /- [constants::S4] -/ def s4_body : Result (Pair U32 U32) := - mk_pair1_fwd (U32.ofInt 7 (by intlit)) (U32.ofInt 8 (by intlit)) + mk_pair1 (U32.ofInt 7 (by intlit)) (U32.ofInt 8 (by intlit)) def s4_c : Pair U32 U32 := eval_global s4_body (by simp) end constants diff --git a/tests/lean/External/Funs.lean b/tests/lean/External/Funs.lean index 3fc21d91..122f94da 100644 --- a/tests/lean/External/Funs.lean +++ b/tests/lean/External/Funs.lean @@ -6,81 +6,80 @@ import External.FunsExternal open Primitives namespace external -/- [external::swap] -/ -def swap_fwd - (T : Type) (x : T) (y : T) (st : State) : Result (State × Unit) := +/- [external::swap]: forward function -/ +def swap (T : Type) (x : T) (y : T) (st : State) : Result (State × Unit) := do - let (st0, _) ← core.mem.swap_fwd T x y st + let (st0, _) ← core.mem.swap T x y st let (st1, _) ← core.mem.swap_back0 T x y st st0 let (st2, _) ← core.mem.swap_back1 T x y st st1 Result.ret (st2, ()) -/- [external::swap] -/ +/- [external::swap]: backward function 0 -/ def swap_back (T : Type) (x : T) (y : T) (st : State) (st0 : State) : Result (State × (T × T)) := do - let (st1, _) ← core.mem.swap_fwd T x y st + let (st1, _) ← core.mem.swap T x y st let (st2, x0) ← core.mem.swap_back0 T x y st st1 let (_, y0) ← core.mem.swap_back1 T x y st st2 Result.ret (st0, (x0, y0)) -/- [external::test_new_non_zero_u32] -/ -def test_new_non_zero_u32_fwd +/- [external::test_new_non_zero_u32]: forward function -/ +def test_new_non_zero_u32 (x : U32) (st : State) : Result (State × core.num.nonzero.NonZeroU32) := do - let (st0, opt) ← core.num.nonzero.NonZeroU32.new_fwd x st - core.option.Option.unwrap_fwd core.num.nonzero.NonZeroU32 opt st0 + let (st0, opt) ← core.num.nonzero.NonZeroU32.new x st + core.option.Option.unwrap core.num.nonzero.NonZeroU32 opt st0 -/- [external::test_vec] -/ -def test_vec_fwd : Result Unit := +/- [external::test_vec]: forward function -/ +def test_vec : Result Unit := do let v := Vec.new U32 let _ ← Vec.push U32 v (U32.ofInt 0 (by intlit)) Result.ret () /- Unit test for [external::test_vec] -/ -#assert (test_vec_fwd == .ret ()) +#assert (test_vec == .ret ()) -/- [external::custom_swap] -/ -def custom_swap_fwd +/- [external::custom_swap]: forward function -/ +def custom_swap (T : Type) (x : T) (y : T) (st : State) : Result (State × T) := do - let (st0, _) ← core.mem.swap_fwd T x y st + let (st0, _) ← core.mem.swap T x y st let (st1, x0) ← core.mem.swap_back0 T x y st st0 let (st2, _) ← core.mem.swap_back1 T x y st st1 Result.ret (st2, x0) -/- [external::custom_swap] -/ +/- [external::custom_swap]: backward function 0 -/ def custom_swap_back (T : Type) (x : T) (y : T) (st : State) (ret0 : T) (st0 : State) : Result (State × (T × T)) := do - let (st1, _) ← core.mem.swap_fwd T x y st + let (st1, _) ← core.mem.swap T x y st let (st2, _) ← core.mem.swap_back0 T x y st st1 let (_, y0) ← core.mem.swap_back1 T x y st st2 Result.ret (st0, (ret0, y0)) -/- [external::test_custom_swap] -/ -def test_custom_swap_fwd +/- [external::test_custom_swap]: forward function -/ +def test_custom_swap (x : U32) (y : U32) (st : State) : Result (State × Unit) := do - let (st0, _) ← custom_swap_fwd U32 x y st + let (st0, _) ← custom_swap U32 x y st Result.ret (st0, ()) -/- [external::test_custom_swap] -/ +/- [external::test_custom_swap]: backward function 0 -/ def test_custom_swap_back (x : U32) (y : U32) (st : State) (st0 : State) : Result (State × (U32 × U32)) := custom_swap_back U32 x y st (U32.ofInt 1 (by intlit)) st0 -/- [external::test_swap_non_zero] -/ -def test_swap_non_zero_fwd (x : U32) (st : State) : Result (State × U32) := +/- [external::test_swap_non_zero]: forward function -/ +def test_swap_non_zero (x : U32) (st : State) : Result (State × U32) := do - let (st0, _) ← swap_fwd U32 x (U32.ofInt 0 (by intlit)) st + let (st0, _) ← swap U32 x (U32.ofInt 0 (by intlit)) st let (st1, (x0, _)) ← swap_back U32 x (U32.ofInt 0 (by intlit)) st st0 if x0 = (U32.ofInt 0 (by intlit)) then Result.fail Error.panic diff --git a/tests/lean/External/FunsExternal.lean b/tests/lean/External/FunsExternal.lean index 5326dd77..aae11ba1 100644 --- a/tests/lean/External/FunsExternal.lean +++ b/tests/lean/External/FunsExternal.lean @@ -7,7 +7,7 @@ open external -- TODO: fill those bodies /- [core::mem::swap] -/ -def core.mem.swap_fwd +def core.mem.swap (T : Type) : T → T → State → Result (State × Unit) := fun _x _y s => .ret (s, ()) @@ -22,12 +22,12 @@ def core.mem.swap_back1 fun x _y _s0 s1 => .ret (s1, x) /- [core::num::nonzero::NonZeroU32::{14}::new] -/ -def core.num.nonzero.NonZeroU32.new_fwd +def core.num.nonzero.NonZeroU32.new : U32 → State → Result (State × (Option core_num_nonzero_non_zero_u32_t)) := fun _ _ => .fail .panic /- [core::option::Option::{0}::unwrap] -/ -def core.option.Option.unwrap_fwd +def core.option.Option.unwrap (T : Type) : Option T → State → Result (State × T) := fun _ _ => .fail .panic diff --git a/tests/lean/External/FunsExternal_Template.lean b/tests/lean/External/FunsExternal_Template.lean index 669fe91b..c8bc397f 100644 --- a/tests/lean/External/FunsExternal_Template.lean +++ b/tests/lean/External/FunsExternal_Template.lean @@ -6,23 +6,22 @@ import External.Types open Primitives open external -/- [core::mem::swap] -/ -axiom core.mem.swap_fwd - (T : Type) : T → T → State → Result (State × Unit) +/- [core::mem::swap]: forward function -/ +axiom core.mem.swap (T : Type) : T → T → State → Result (State × Unit) -/- [core::mem::swap] -/ +/- [core::mem::swap]: backward function 0 -/ axiom core.mem.swap_back0 (T : Type) : T → T → State → State → Result (State × T) -/- [core::mem::swap] -/ +/- [core::mem::swap]: backward function 1 -/ axiom core.mem.swap_back1 (T : Type) : T → T → State → State → Result (State × T) -/- [core::num::nonzero::NonZeroU32::{14}::new] -/ -axiom core.num.nonzero.NonZeroU32.new_fwd +/- [core::num::nonzero::NonZeroU32::{14}::new]: forward function -/ +axiom core.num.nonzero.NonZeroU32.new : U32 → State → Result (State × (Option core.num.nonzero.NonZeroU32)) -/- [core::option::Option::{0}::unwrap] -/ -axiom core.option.Option.unwrap_fwd +/- [core::option::Option::{0}::unwrap]: forward function -/ +axiom core.option.Option.unwrap (T : Type) : Option T → State → Result (State × T) diff --git a/tests/lean/Hashmap/Funs.lean b/tests/lean/Hashmap/Funs.lean index 48038bfb..34ff1379 100644 --- a/tests/lean/Hashmap/Funs.lean +++ b/tests/lean/Hashmap/Funs.lean @@ -5,35 +5,35 @@ import Hashmap.Types open Primitives namespace hashmap -/- [hashmap::hash_key] -/ -def hash_key_fwd (k : Usize) : Result Usize := +/- [hashmap::hash_key]: forward function -/ +def hash_key (k : Usize) : Result Usize := Result.ret k -/- [hashmap::HashMap::{0}::allocate_slots] -/ -divergent def HashMap.allocate_slots_loop_fwd +/- [hashmap::HashMap::{0}::allocate_slots]: loop 0: forward function -/ +divergent def HashMap.allocate_slots_loop (T : Type) (slots : Vec (List T)) (n : Usize) : Result (Vec (List T)) := if n > (Usize.ofInt 0 (by intlit)) then do let slots0 ← Vec.push (List T) slots List.Nil let n0 ← n - (Usize.ofInt 1 (by intlit)) - HashMap.allocate_slots_loop_fwd T slots0 n0 + HashMap.allocate_slots_loop T slots0 n0 else Result.ret slots -/- [hashmap::HashMap::{0}::allocate_slots] -/ -def HashMap.allocate_slots_fwd +/- [hashmap::HashMap::{0}::allocate_slots]: forward function -/ +def HashMap.allocate_slots (T : Type) (slots : Vec (List T)) (n : Usize) : Result (Vec (List T)) := - HashMap.allocate_slots_loop_fwd T slots n + HashMap.allocate_slots_loop T slots n -/- [hashmap::HashMap::{0}::new_with_capacity] -/ -def HashMap.new_with_capacity_fwd +/- [hashmap::HashMap::{0}::new_with_capacity]: forward function -/ +def HashMap.new_with_capacity (T : Type) (capacity : Usize) (max_load_dividend : Usize) (max_load_divisor : Usize) : Result (HashMap T) := do let v := Vec.new (List T) - let slots ← HashMap.allocate_slots_fwd T v capacity + let slots ← HashMap.allocate_slots T v capacity let i ← capacity * max_load_dividend let i0 ← i / max_load_divisor Result.ret @@ -44,13 +44,14 @@ def HashMap.new_with_capacity_fwd hash_map_slots := slots } -/- [hashmap::HashMap::{0}::new] -/ -def HashMap.new_fwd (T : Type) : Result (HashMap T) := - HashMap.new_with_capacity_fwd T (Usize.ofInt 32 (by intlit)) +/- [hashmap::HashMap::{0}::new]: forward function -/ +def HashMap.new (T : Type) : Result (HashMap T) := + HashMap.new_with_capacity T (Usize.ofInt 32 (by intlit)) (Usize.ofInt 4 (by intlit)) (Usize.ofInt 5 (by intlit)) -/- [hashmap::HashMap::{0}::clear] -/ -divergent def HashMap.clear_loop_fwd_back +/- [hashmap::HashMap::{0}::clear]: loop 0: merged forward/backward function + (there is a single backward function, and the forward function returns ()) -/ +divergent def HashMap.clear_loop (T : Type) (slots : Vec (List T)) (i : Usize) : Result (Vec (List T)) := let i0 := Vec.len (List T) slots if i < i0 @@ -58,16 +59,15 @@ divergent def HashMap.clear_loop_fwd_back do let i1 ← i + (Usize.ofInt 1 (by intlit)) let slots0 ← Vec.index_mut_back (List T) slots i List.Nil - HashMap.clear_loop_fwd_back T slots0 i1 + HashMap.clear_loop T slots0 i1 else Result.ret slots -/- [hashmap::HashMap::{0}::clear] -/ -def HashMap.clear_fwd_back - (T : Type) (self : HashMap T) : Result (HashMap T) := +/- [hashmap::HashMap::{0}::clear]: merged forward/backward function + (there is a single backward function, and the forward function returns ()) -/ +def HashMap.clear (T : Type) (self : HashMap T) : Result (HashMap T) := do let v ← - HashMap.clear_loop_fwd_back T self.hash_map_slots - (Usize.ofInt 0 (by intlit)) + HashMap.clear_loop T self.hash_map_slots (Usize.ofInt 0 (by intlit)) Result.ret { self @@ -76,26 +76,26 @@ def HashMap.clear_fwd_back hash_map_slots := v } -/- [hashmap::HashMap::{0}::len] -/ -def HashMap.len_fwd (T : Type) (self : HashMap T) : Result Usize := +/- [hashmap::HashMap::{0}::len]: forward function -/ +def HashMap.len (T : Type) (self : HashMap T) : Result Usize := Result.ret self.hash_map_num_entries -/- [hashmap::HashMap::{0}::insert_in_list] -/ -divergent def HashMap.insert_in_list_loop_fwd +/- [hashmap::HashMap::{0}::insert_in_list]: loop 0: forward function -/ +divergent def HashMap.insert_in_list_loop (T : Type) (key : Usize) (value : T) (ls : List T) : Result Bool := match ls with | List.Cons ckey cvalue tl => if ckey = key then Result.ret false - else HashMap.insert_in_list_loop_fwd T key value tl + else HashMap.insert_in_list_loop T key value tl | List.Nil => Result.ret true -/- [hashmap::HashMap::{0}::insert_in_list] -/ -def HashMap.insert_in_list_fwd +/- [hashmap::HashMap::{0}::insert_in_list]: forward function -/ +def HashMap.insert_in_list (T : Type) (key : Usize) (value : T) (ls : List T) : Result Bool := - HashMap.insert_in_list_loop_fwd T key value ls + HashMap.insert_in_list_loop T key value ls -/- [hashmap::HashMap::{0}::insert_in_list] -/ +/- [hashmap::HashMap::{0}::insert_in_list]: loop 0: backward function 0 -/ divergent def HashMap.insert_in_list_loop_back (T : Type) (key : Usize) (value : T) (ls : List T) : Result (List T) := match ls with @@ -109,22 +109,23 @@ divergent def HashMap.insert_in_list_loop_back | List.Nil => let l := List.Nil Result.ret (List.Cons key value l) -/- [hashmap::HashMap::{0}::insert_in_list] -/ +/- [hashmap::HashMap::{0}::insert_in_list]: backward function 0 -/ def HashMap.insert_in_list_back (T : Type) (key : Usize) (value : T) (ls : List T) : Result (List T) := HashMap.insert_in_list_loop_back T key value ls -/- [hashmap::HashMap::{0}::insert_no_resize] -/ -def HashMap.insert_no_resize_fwd_back +/- [hashmap::HashMap::{0}::insert_no_resize]: merged forward/backward function + (there is a single backward function, and the forward function returns ()) -/ +def HashMap.insert_no_resize (T : Type) (self : HashMap T) (key : Usize) (value : T) : Result (HashMap T) := do - let hash ← hash_key_fwd key + let hash ← hash_key key let i := Vec.len (List T) self.hash_map_slots let hash_mod ← hash % i let l ← Vec.index_mut (List T) self.hash_map_slots hash_mod - let inserted ← HashMap.insert_in_list_fwd T key value l + let inserted ← HashMap.insert_in_list T key value l if inserted then do @@ -144,23 +145,26 @@ def core_num_u32_max_body : Result U32 := Result.ret (U32.ofInt 4294967295 (by intlit)) def core_num_u32_max_c : U32 := eval_global core_num_u32_max_body (by simp) -/- [hashmap::HashMap::{0}::move_elements_from_list] -/ -divergent def HashMap.move_elements_from_list_loop_fwd_back +/- [hashmap::HashMap::{0}::move_elements_from_list]: loop 0: merged forward/backward function + (there is a single backward function, and the forward function returns ()) -/ +divergent def HashMap.move_elements_from_list_loop (T : Type) (ntable : HashMap T) (ls : List T) : Result (HashMap T) := match ls with | List.Cons k v tl => do - let ntable0 ← HashMap.insert_no_resize_fwd_back T ntable k v - HashMap.move_elements_from_list_loop_fwd_back T ntable0 tl + let ntable0 ← HashMap.insert_no_resize T ntable k v + HashMap.move_elements_from_list_loop T ntable0 tl | List.Nil => Result.ret ntable -/- [hashmap::HashMap::{0}::move_elements_from_list] -/ -def HashMap.move_elements_from_list_fwd_back +/- [hashmap::HashMap::{0}::move_elements_from_list]: merged forward/backward function + (there is a single backward function, and the forward function returns ()) -/ +def HashMap.move_elements_from_list (T : Type) (ntable : HashMap T) (ls : List T) : Result (HashMap T) := - HashMap.move_elements_from_list_loop_fwd_back T ntable ls + HashMap.move_elements_from_list_loop T ntable ls -/- [hashmap::HashMap::{0}::move_elements] -/ -divergent def HashMap.move_elements_loop_fwd_back +/- [hashmap::HashMap::{0}::move_elements]: loop 0: merged forward/backward function + (there is a single backward function, and the forward function returns ()) -/ +divergent def HashMap.move_elements_loop (T : Type) (ntable : HashMap T) (slots : Vec (List T)) (i : Usize) : Result ((HashMap T) × (Vec (List T))) := @@ -169,24 +173,25 @@ divergent def HashMap.move_elements_loop_fwd_back then do let l ← Vec.index_mut (List T) slots i - let ls := mem.replace_fwd (List T) l List.Nil - let ntable0 ← HashMap.move_elements_from_list_fwd_back T ntable ls + let ls := mem.replace (List T) l List.Nil + let ntable0 ← HashMap.move_elements_from_list T ntable ls let i1 ← i + (Usize.ofInt 1 (by intlit)) let l0 := mem.replace_back (List T) l List.Nil let slots0 ← Vec.index_mut_back (List T) slots i l0 - HashMap.move_elements_loop_fwd_back T ntable0 slots0 i1 + HashMap.move_elements_loop T ntable0 slots0 i1 else Result.ret (ntable, slots) -/- [hashmap::HashMap::{0}::move_elements] -/ -def HashMap.move_elements_fwd_back +/- [hashmap::HashMap::{0}::move_elements]: merged forward/backward function + (there is a single backward function, and the forward function returns ()) -/ +def HashMap.move_elements (T : Type) (ntable : HashMap T) (slots : Vec (List T)) (i : Usize) : Result ((HashMap T) × (Vec (List T))) := - HashMap.move_elements_loop_fwd_back T ntable slots i + HashMap.move_elements_loop T ntable slots i -/- [hashmap::HashMap::{0}::try_resize] -/ -def HashMap.try_resize_fwd_back - (T : Type) (self : HashMap T) : Result (HashMap T) := +/- [hashmap::HashMap::{0}::try_resize]: merged forward/backward function + (there is a single backward function, and the forward function returns ()) -/ +def HashMap.try_resize (T : Type) (self : HashMap T) : Result (HashMap T) := do let max_usize ← Scalar.cast .Usize core_num_u32_max_c let capacity := Vec.len (List T) self.hash_map_slots @@ -197,9 +202,9 @@ def HashMap.try_resize_fwd_back then do let i2 ← capacity * (Usize.ofInt 2 (by intlit)) - let ntable ← HashMap.new_with_capacity_fwd T i2 i i0 + let ntable ← HashMap.new_with_capacity T i2 i i0 let (ntable0, _) ← - HashMap.move_elements_fwd_back T ntable self.hash_map_slots + HashMap.move_elements T ntable self.hash_map_slots (Usize.ofInt 0 (by intlit)) Result.ret { @@ -210,83 +215,83 @@ def HashMap.try_resize_fwd_back } else Result.ret { self with hash_map_max_load_factor := (i, i0) } -/- [hashmap::HashMap::{0}::insert] -/ -def HashMap.insert_fwd_back +/- [hashmap::HashMap::{0}::insert]: merged forward/backward function + (there is a single backward function, and the forward function returns ()) -/ +def HashMap.insert (T : Type) (self : HashMap T) (key : Usize) (value : T) : Result (HashMap T) := do - let self0 ← HashMap.insert_no_resize_fwd_back T self key value - let i ← HashMap.len_fwd T self0 + let self0 ← HashMap.insert_no_resize T self key value + let i ← HashMap.len T self0 if i > self0.hash_map_max_load - then HashMap.try_resize_fwd_back T self0 + then HashMap.try_resize T self0 else Result.ret self0 -/- [hashmap::HashMap::{0}::contains_key_in_list] -/ -divergent def HashMap.contains_key_in_list_loop_fwd +/- [hashmap::HashMap::{0}::contains_key_in_list]: loop 0: forward function -/ +divergent def HashMap.contains_key_in_list_loop (T : Type) (key : Usize) (ls : List T) : Result Bool := match ls with | List.Cons ckey t tl => if ckey = key then Result.ret true - else HashMap.contains_key_in_list_loop_fwd T key tl + else HashMap.contains_key_in_list_loop T key tl | List.Nil => Result.ret false -/- [hashmap::HashMap::{0}::contains_key_in_list] -/ -def HashMap.contains_key_in_list_fwd +/- [hashmap::HashMap::{0}::contains_key_in_list]: forward function -/ +def HashMap.contains_key_in_list (T : Type) (key : Usize) (ls : List T) : Result Bool := - HashMap.contains_key_in_list_loop_fwd T key ls + HashMap.contains_key_in_list_loop T key ls -/- [hashmap::HashMap::{0}::contains_key] -/ -def HashMap.contains_key_fwd +/- [hashmap::HashMap::{0}::contains_key]: forward function -/ +def HashMap.contains_key (T : Type) (self : HashMap T) (key : Usize) : Result Bool := do - let hash ← hash_key_fwd key + let hash ← hash_key key let i := Vec.len (List T) self.hash_map_slots let hash_mod ← hash % i let l ← Vec.index (List T) self.hash_map_slots hash_mod - HashMap.contains_key_in_list_fwd T key l + HashMap.contains_key_in_list T key l -/- [hashmap::HashMap::{0}::get_in_list] -/ -divergent def HashMap.get_in_list_loop_fwd +/- [hashmap::HashMap::{0}::get_in_list]: loop 0: forward function -/ +divergent def HashMap.get_in_list_loop (T : Type) (key : Usize) (ls : List T) : Result T := match ls with | List.Cons ckey cvalue tl => if ckey = key then Result.ret cvalue - else HashMap.get_in_list_loop_fwd T key tl + else HashMap.get_in_list_loop T key tl | List.Nil => Result.fail Error.panic -/- [hashmap::HashMap::{0}::get_in_list] -/ -def HashMap.get_in_list_fwd - (T : Type) (key : Usize) (ls : List T) : Result T := - HashMap.get_in_list_loop_fwd T key ls +/- [hashmap::HashMap::{0}::get_in_list]: forward function -/ +def HashMap.get_in_list (T : Type) (key : Usize) (ls : List T) : Result T := + HashMap.get_in_list_loop T key ls -/- [hashmap::HashMap::{0}::get] -/ -def HashMap.get_fwd (T : Type) (self : HashMap T) (key : Usize) : Result T := +/- [hashmap::HashMap::{0}::get]: forward function -/ +def HashMap.get (T : Type) (self : HashMap T) (key : Usize) : Result T := do - let hash ← hash_key_fwd key + let hash ← hash_key key let i := Vec.len (List T) self.hash_map_slots let hash_mod ← hash % i let l ← Vec.index (List T) self.hash_map_slots hash_mod - HashMap.get_in_list_fwd T key l + HashMap.get_in_list T key l -/- [hashmap::HashMap::{0}::get_mut_in_list] -/ -divergent def HashMap.get_mut_in_list_loop_fwd +/- [hashmap::HashMap::{0}::get_mut_in_list]: loop 0: forward function -/ +divergent def HashMap.get_mut_in_list_loop (T : Type) (ls : List T) (key : Usize) : Result T := match ls with | List.Cons ckey cvalue tl => if ckey = key then Result.ret cvalue - else HashMap.get_mut_in_list_loop_fwd T tl key + else HashMap.get_mut_in_list_loop T tl key | List.Nil => Result.fail Error.panic -/- [hashmap::HashMap::{0}::get_mut_in_list] -/ -def HashMap.get_mut_in_list_fwd +/- [hashmap::HashMap::{0}::get_mut_in_list]: forward function -/ +def HashMap.get_mut_in_list (T : Type) (ls : List T) (key : Usize) : Result T := - HashMap.get_mut_in_list_loop_fwd T ls key + HashMap.get_mut_in_list_loop T ls key -/- [hashmap::HashMap::{0}::get_mut_in_list] -/ +/- [hashmap::HashMap::{0}::get_mut_in_list]: loop 0: backward function 0 -/ divergent def HashMap.get_mut_in_list_loop_back (T : Type) (ls : List T) (key : Usize) (ret0 : T) : Result (List T) := match ls with @@ -299,28 +304,27 @@ divergent def HashMap.get_mut_in_list_loop_back Result.ret (List.Cons ckey cvalue tl0) | List.Nil => Result.fail Error.panic -/- [hashmap::HashMap::{0}::get_mut_in_list] -/ +/- [hashmap::HashMap::{0}::get_mut_in_list]: backward function 0 -/ def HashMap.get_mut_in_list_back (T : Type) (ls : List T) (key : Usize) (ret0 : T) : Result (List T) := HashMap.get_mut_in_list_loop_back T ls key ret0 -/- [hashmap::HashMap::{0}::get_mut] -/ -def HashMap.get_mut_fwd - (T : Type) (self : HashMap T) (key : Usize) : Result T := +/- [hashmap::HashMap::{0}::get_mut]: forward function -/ +def HashMap.get_mut (T : Type) (self : HashMap T) (key : Usize) : Result T := do - let hash ← hash_key_fwd key + let hash ← hash_key key let i := Vec.len (List T) self.hash_map_slots let hash_mod ← hash % i let l ← Vec.index_mut (List T) self.hash_map_slots hash_mod - HashMap.get_mut_in_list_fwd T l key + HashMap.get_mut_in_list T l key -/- [hashmap::HashMap::{0}::get_mut] -/ +/- [hashmap::HashMap::{0}::get_mut]: backward function 0 -/ def HashMap.get_mut_back (T : Type) (self : HashMap T) (key : Usize) (ret0 : T) : Result (HashMap T) := do - let hash ← hash_key_fwd key + let hash ← hash_key key let i := Vec.len (List T) self.hash_map_slots let hash_mod ← hash % i let l ← Vec.index_mut (List T) self.hash_map_slots hash_mod @@ -328,33 +332,33 @@ def HashMap.get_mut_back let v ← Vec.index_mut_back (List T) self.hash_map_slots hash_mod l0 Result.ret { self with hash_map_slots := v } -/- [hashmap::HashMap::{0}::remove_from_list] -/ -divergent def HashMap.remove_from_list_loop_fwd +/- [hashmap::HashMap::{0}::remove_from_list]: loop 0: forward function -/ +divergent def HashMap.remove_from_list_loop (T : Type) (key : Usize) (ls : List T) : Result (Option T) := match ls with | List.Cons ckey t tl => if ckey = key then - let mv_ls := mem.replace_fwd (List T) (List.Cons ckey t tl) List.Nil + let mv_ls := mem.replace (List T) (List.Cons ckey t tl) List.Nil match mv_ls with | List.Cons i cvalue tl0 => Result.ret (Option.some cvalue) | List.Nil => Result.fail Error.panic - else HashMap.remove_from_list_loop_fwd T key tl + else HashMap.remove_from_list_loop T key tl | List.Nil => Result.ret Option.none -/- [hashmap::HashMap::{0}::remove_from_list] -/ -def HashMap.remove_from_list_fwd +/- [hashmap::HashMap::{0}::remove_from_list]: forward function -/ +def HashMap.remove_from_list (T : Type) (key : Usize) (ls : List T) : Result (Option T) := - HashMap.remove_from_list_loop_fwd T key ls + HashMap.remove_from_list_loop T key ls -/- [hashmap::HashMap::{0}::remove_from_list] -/ +/- [hashmap::HashMap::{0}::remove_from_list]: loop 0: backward function 1 -/ divergent def HashMap.remove_from_list_loop_back (T : Type) (key : Usize) (ls : List T) : Result (List T) := match ls with | List.Cons ckey t tl => if ckey = key then - let mv_ls := mem.replace_fwd (List T) (List.Cons ckey t tl) List.Nil + let mv_ls := mem.replace (List T) (List.Cons ckey t tl) List.Nil match mv_ls with | List.Cons i cvalue tl0 => Result.ret tl0 | List.Nil => Result.fail Error.panic @@ -364,20 +368,20 @@ divergent def HashMap.remove_from_list_loop_back Result.ret (List.Cons ckey t tl0) | List.Nil => Result.ret List.Nil -/- [hashmap::HashMap::{0}::remove_from_list] -/ +/- [hashmap::HashMap::{0}::remove_from_list]: backward function 1 -/ def HashMap.remove_from_list_back (T : Type) (key : Usize) (ls : List T) : Result (List T) := HashMap.remove_from_list_loop_back T key ls -/- [hashmap::HashMap::{0}::remove] -/ -def HashMap.remove_fwd +/- [hashmap::HashMap::{0}::remove]: forward function -/ +def HashMap.remove (T : Type) (self : HashMap T) (key : Usize) : Result (Option T) := do - let hash ← hash_key_fwd key + let hash ← hash_key key let i := Vec.len (List T) self.hash_map_slots let hash_mod ← hash % i let l ← Vec.index_mut (List T) self.hash_map_slots hash_mod - let x ← HashMap.remove_from_list_fwd T key l + let x ← HashMap.remove_from_list T key l match x with | Option.none => Result.ret Option.none | Option.some x0 => @@ -385,15 +389,15 @@ def HashMap.remove_fwd let _ ← self.hash_map_num_entries - (Usize.ofInt 1 (by intlit)) Result.ret (Option.some x0) -/- [hashmap::HashMap::{0}::remove] -/ +/- [hashmap::HashMap::{0}::remove]: backward function 0 -/ def HashMap.remove_back (T : Type) (self : HashMap T) (key : Usize) : Result (HashMap T) := do - let hash ← hash_key_fwd key + let hash ← hash_key key let i := Vec.len (List T) self.hash_map_slots let hash_mod ← hash % i let l ← Vec.index_mut (List T) self.hash_map_slots hash_mod - let x ← HashMap.remove_from_list_fwd T key l + let x ← HashMap.remove_from_list T key l match x with | Option.none => do @@ -408,23 +412,23 @@ def HashMap.remove_back Result.ret { self with hash_map_num_entries := i0, hash_map_slots := v } -/- [hashmap::test1] -/ -def test1_fwd : Result Unit := +/- [hashmap::test1]: forward function -/ +def test1 : Result Unit := do - let hm ← HashMap.new_fwd U64 + let hm ← HashMap.new U64 let hm0 ← - HashMap.insert_fwd_back U64 hm (Usize.ofInt 0 (by intlit)) + HashMap.insert U64 hm (Usize.ofInt 0 (by intlit)) (U64.ofInt 42 (by intlit)) let hm1 ← - HashMap.insert_fwd_back U64 hm0 (Usize.ofInt 128 (by intlit)) + HashMap.insert U64 hm0 (Usize.ofInt 128 (by intlit)) (U64.ofInt 18 (by intlit)) let hm2 ← - HashMap.insert_fwd_back U64 hm1 (Usize.ofInt 1024 (by intlit)) + HashMap.insert U64 hm1 (Usize.ofInt 1024 (by intlit)) (U64.ofInt 138 (by intlit)) let hm3 ← - HashMap.insert_fwd_back U64 hm2 (Usize.ofInt 1056 (by intlit)) + HashMap.insert U64 hm2 (Usize.ofInt 1056 (by intlit)) (U64.ofInt 256 (by intlit)) - let i ← HashMap.get_fwd U64 hm3 (Usize.ofInt 128 (by intlit)) + let i ← HashMap.get U64 hm3 (Usize.ofInt 128 (by intlit)) if not (i = (U64.ofInt 18 (by intlit))) then Result.fail Error.panic else @@ -432,12 +436,12 @@ def test1_fwd : Result Unit := let hm4 ← HashMap.get_mut_back U64 hm3 (Usize.ofInt 1024 (by intlit)) (U64.ofInt 56 (by intlit)) - let i0 ← HashMap.get_fwd U64 hm4 (Usize.ofInt 1024 (by intlit)) + let i0 ← HashMap.get U64 hm4 (Usize.ofInt 1024 (by intlit)) if not (i0 = (U64.ofInt 56 (by intlit))) then Result.fail Error.panic else do - let x ← HashMap.remove_fwd U64 hm4 (Usize.ofInt 1024 (by intlit)) + let x ← HashMap.remove U64 hm4 (Usize.ofInt 1024 (by intlit)) match x with | Option.none => Result.fail Error.panic | Option.some x0 => @@ -447,26 +451,24 @@ def test1_fwd : Result Unit := do let hm5 ← HashMap.remove_back U64 hm4 (Usize.ofInt 1024 (by intlit)) - let i1 ← - HashMap.get_fwd U64 hm5 (Usize.ofInt 0 (by intlit)) + let i1 ← HashMap.get U64 hm5 (Usize.ofInt 0 (by intlit)) if not (i1 = (U64.ofInt 42 (by intlit))) then Result.fail Error.panic else do let i2 ← - HashMap.get_fwd U64 hm5 (Usize.ofInt 128 (by intlit)) + HashMap.get U64 hm5 (Usize.ofInt 128 (by intlit)) if not (i2 = (U64.ofInt 18 (by intlit))) then Result.fail Error.panic else do let i3 ← - HashMap.get_fwd U64 hm5 - (Usize.ofInt 1056 (by intlit)) + HashMap.get U64 hm5 (Usize.ofInt 1056 (by intlit)) if not (i3 = (U64.ofInt 256 (by intlit))) then Result.fail Error.panic else Result.ret () /- Unit test for [hashmap::test1] -/ -#assert (test1_fwd == .ret ()) +#assert (test1 == .ret ()) end hashmap diff --git a/tests/lean/HashmapMain/Funs.lean b/tests/lean/HashmapMain/Funs.lean index 1a741a2d..b1afcd44 100644 --- a/tests/lean/HashmapMain/Funs.lean +++ b/tests/lean/HashmapMain/Funs.lean @@ -6,12 +6,12 @@ import HashmapMain.FunsExternal open Primitives namespace hashmap_main -/- [hashmap_main::hashmap::hash_key] -/ -def hashmap.hash_key_fwd (k : Usize) : Result Usize := +/- [hashmap_main::hashmap::hash_key]: forward function -/ +def hashmap.hash_key (k : Usize) : Result Usize := Result.ret k -/- [hashmap_main::hashmap::HashMap::{0}::allocate_slots] -/ -divergent def hashmap.HashMap.allocate_slots_loop_fwd +/- [hashmap_main::hashmap::HashMap::{0}::allocate_slots]: loop 0: forward function -/ +divergent def hashmap.HashMap.allocate_slots_loop (T : Type) (slots : Vec (hashmap.List T)) (n : Usize) : Result (Vec (hashmap.List T)) := @@ -20,25 +20,25 @@ divergent def hashmap.HashMap.allocate_slots_loop_fwd do let slots0 ← Vec.push (hashmap.List T) slots hashmap.List.Nil let n0 ← n - (Usize.ofInt 1 (by intlit)) - hashmap.HashMap.allocate_slots_loop_fwd T slots0 n0 + hashmap.HashMap.allocate_slots_loop T slots0 n0 else Result.ret slots -/- [hashmap_main::hashmap::HashMap::{0}::allocate_slots] -/ -def hashmap.HashMap.allocate_slots_fwd +/- [hashmap_main::hashmap::HashMap::{0}::allocate_slots]: forward function -/ +def hashmap.HashMap.allocate_slots (T : Type) (slots : Vec (hashmap.List T)) (n : Usize) : Result (Vec (hashmap.List T)) := - hashmap.HashMap.allocate_slots_loop_fwd T slots n + hashmap.HashMap.allocate_slots_loop T slots n -/- [hashmap_main::hashmap::HashMap::{0}::new_with_capacity] -/ -def hashmap.HashMap.new_with_capacity_fwd +/- [hashmap_main::hashmap::HashMap::{0}::new_with_capacity]: forward function -/ +def hashmap.HashMap.new_with_capacity (T : Type) (capacity : Usize) (max_load_dividend : Usize) (max_load_divisor : Usize) : Result (hashmap.HashMap T) := do let v := Vec.new (hashmap.List T) - let slots ← hashmap.HashMap.allocate_slots_fwd T v capacity + let slots ← hashmap.HashMap.allocate_slots T v capacity let i ← capacity * max_load_dividend let i0 ← i / max_load_divisor Result.ret @@ -50,13 +50,14 @@ def hashmap.HashMap.new_with_capacity_fwd hashmap_hash_map_slots := slots } -/- [hashmap_main::hashmap::HashMap::{0}::new] -/ -def hashmap.HashMap.new_fwd (T : Type) : Result (hashmap.HashMap T) := - hashmap.HashMap.new_with_capacity_fwd T (Usize.ofInt 32 (by intlit)) +/- [hashmap_main::hashmap::HashMap::{0}::new]: forward function -/ +def hashmap.HashMap.new (T : Type) : Result (hashmap.HashMap T) := + hashmap.HashMap.new_with_capacity T (Usize.ofInt 32 (by intlit)) (Usize.ofInt 4 (by intlit)) (Usize.ofInt 5 (by intlit)) -/- [hashmap_main::hashmap::HashMap::{0}::clear] -/ -divergent def hashmap.HashMap.clear_loop_fwd_back +/- [hashmap_main::hashmap::HashMap::{0}::clear]: loop 0: merged forward/backward function + (there is a single backward function, and the forward function returns ()) -/ +divergent def hashmap.HashMap.clear_loop (T : Type) (slots : Vec (hashmap.List T)) (i : Usize) : Result (Vec (hashmap.List T)) := @@ -67,15 +68,16 @@ divergent def hashmap.HashMap.clear_loop_fwd_back let i1 ← i + (Usize.ofInt 1 (by intlit)) let slots0 ← Vec.index_mut_back (hashmap.List T) slots i hashmap.List.Nil - hashmap.HashMap.clear_loop_fwd_back T slots0 i1 + hashmap.HashMap.clear_loop T slots0 i1 else Result.ret slots -/- [hashmap_main::hashmap::HashMap::{0}::clear] -/ -def hashmap.HashMap.clear_fwd_back +/- [hashmap_main::hashmap::HashMap::{0}::clear]: merged forward/backward function + (there is a single backward function, and the forward function returns ()) -/ +def hashmap.HashMap.clear (T : Type) (self : hashmap.HashMap T) : Result (hashmap.HashMap T) := do let v ← - hashmap.HashMap.clear_loop_fwd_back T self.hashmap_hash_map_slots + hashmap.HashMap.clear_loop T self.hashmap_hash_map_slots (Usize.ofInt 0 (by intlit)) Result.ret { @@ -85,27 +87,26 @@ def hashmap.HashMap.clear_fwd_back hashmap_hash_map_slots := v } -/- [hashmap_main::hashmap::HashMap::{0}::len] -/ -def hashmap.HashMap.len_fwd - (T : Type) (self : hashmap.HashMap T) : Result Usize := +/- [hashmap_main::hashmap::HashMap::{0}::len]: forward function -/ +def hashmap.HashMap.len (T : Type) (self : hashmap.HashMap T) : Result Usize := Result.ret self.hashmap_hash_map_num_entries -/- [hashmap_main::hashmap::HashMap::{0}::insert_in_list] -/ -divergent def hashmap.HashMap.insert_in_list_loop_fwd +/- [hashmap_main::hashmap::HashMap::{0}::insert_in_list]: loop 0: forward function -/ +divergent def hashmap.HashMap.insert_in_list_loop (T : Type) (key : Usize) (value : T) (ls : hashmap.List T) : Result Bool := match ls with | hashmap.List.Cons ckey cvalue tl => if ckey = key then Result.ret false - else hashmap.HashMap.insert_in_list_loop_fwd T key value tl + else hashmap.HashMap.insert_in_list_loop T key value tl | hashmap.List.Nil => Result.ret true -/- [hashmap_main::hashmap::HashMap::{0}::insert_in_list] -/ -def hashmap.HashMap.insert_in_list_fwd +/- [hashmap_main::hashmap::HashMap::{0}::insert_in_list]: forward function -/ +def hashmap.HashMap.insert_in_list (T : Type) (key : Usize) (value : T) (ls : hashmap.List T) : Result Bool := - hashmap.HashMap.insert_in_list_loop_fwd T key value ls + hashmap.HashMap.insert_in_list_loop T key value ls -/- [hashmap_main::hashmap::HashMap::{0}::insert_in_list] -/ +/- [hashmap_main::hashmap::HashMap::{0}::insert_in_list]: loop 0: backward function 0 -/ divergent def hashmap.HashMap.insert_in_list_loop_back (T : Type) (key : Usize) (value : T) (ls : hashmap.List T) : Result (hashmap.List T) @@ -122,25 +123,26 @@ divergent def hashmap.HashMap.insert_in_list_loop_back let l := hashmap.List.Nil Result.ret (hashmap.List.Cons key value l) -/- [hashmap_main::hashmap::HashMap::{0}::insert_in_list] -/ +/- [hashmap_main::hashmap::HashMap::{0}::insert_in_list]: backward function 0 -/ def hashmap.HashMap.insert_in_list_back (T : Type) (key : Usize) (value : T) (ls : hashmap.List T) : Result (hashmap.List T) := hashmap.HashMap.insert_in_list_loop_back T key value ls -/- [hashmap_main::hashmap::HashMap::{0}::insert_no_resize] -/ -def hashmap.HashMap.insert_no_resize_fwd_back +/- [hashmap_main::hashmap::HashMap::{0}::insert_no_resize]: merged forward/backward function + (there is a single backward function, and the forward function returns ()) -/ +def hashmap.HashMap.insert_no_resize (T : Type) (self : hashmap.HashMap T) (key : Usize) (value : T) : Result (hashmap.HashMap T) := do - let hash ← hashmap.hash_key_fwd key + let hash ← hashmap.hash_key key let i := Vec.len (hashmap.List T) self.hashmap_hash_map_slots let hash_mod ← hash % i let l ← Vec.index_mut (hashmap.List T) self.hashmap_hash_map_slots hash_mod - let inserted ← hashmap.HashMap.insert_in_list_fwd T key value l + let inserted ← hashmap.HashMap.insert_in_list T key value l if inserted then do @@ -169,27 +171,30 @@ def core_num_u32_max_body : Result U32 := Result.ret (U32.ofInt 4294967295 (by intlit)) def core_num_u32_max_c : U32 := eval_global core_num_u32_max_body (by simp) -/- [hashmap_main::hashmap::HashMap::{0}::move_elements_from_list] -/ -divergent def hashmap.HashMap.move_elements_from_list_loop_fwd_back +/- [hashmap_main::hashmap::HashMap::{0}::move_elements_from_list]: loop 0: merged forward/backward function + (there is a single backward function, and the forward function returns ()) -/ +divergent def hashmap.HashMap.move_elements_from_list_loop (T : Type) (ntable : hashmap.HashMap T) (ls : hashmap.List T) : Result (hashmap.HashMap T) := match ls with | hashmap.List.Cons k v tl => do - let ntable0 ← hashmap.HashMap.insert_no_resize_fwd_back T ntable k v - hashmap.HashMap.move_elements_from_list_loop_fwd_back T ntable0 tl + let ntable0 ← hashmap.HashMap.insert_no_resize T ntable k v + hashmap.HashMap.move_elements_from_list_loop T ntable0 tl | hashmap.List.Nil => Result.ret ntable -/- [hashmap_main::hashmap::HashMap::{0}::move_elements_from_list] -/ -def hashmap.HashMap.move_elements_from_list_fwd_back +/- [hashmap_main::hashmap::HashMap::{0}::move_elements_from_list]: merged forward/backward function + (there is a single backward function, and the forward function returns ()) -/ +def hashmap.HashMap.move_elements_from_list (T : Type) (ntable : hashmap.HashMap T) (ls : hashmap.List T) : Result (hashmap.HashMap T) := - hashmap.HashMap.move_elements_from_list_loop_fwd_back T ntable ls + hashmap.HashMap.move_elements_from_list_loop T ntable ls -/- [hashmap_main::hashmap::HashMap::{0}::move_elements] -/ -divergent def hashmap.HashMap.move_elements_loop_fwd_back +/- [hashmap_main::hashmap::HashMap::{0}::move_elements]: loop 0: merged forward/backward function + (there is a single backward function, and the forward function returns ()) -/ +divergent def hashmap.HashMap.move_elements_loop (T : Type) (ntable : hashmap.HashMap T) (slots : Vec (hashmap.List T)) (i : Usize) : Result ((hashmap.HashMap T) × (Vec (hashmap.List T))) @@ -199,25 +204,26 @@ divergent def hashmap.HashMap.move_elements_loop_fwd_back then do let l ← Vec.index_mut (hashmap.List T) slots i - let ls := mem.replace_fwd (hashmap.List T) l hashmap.List.Nil - let ntable0 ← - hashmap.HashMap.move_elements_from_list_fwd_back T ntable ls + let ls := mem.replace (hashmap.List T) l hashmap.List.Nil + let ntable0 ← hashmap.HashMap.move_elements_from_list T ntable ls let i1 ← i + (Usize.ofInt 1 (by intlit)) let l0 := mem.replace_back (hashmap.List T) l hashmap.List.Nil let slots0 ← Vec.index_mut_back (hashmap.List T) slots i l0 - hashmap.HashMap.move_elements_loop_fwd_back T ntable0 slots0 i1 + hashmap.HashMap.move_elements_loop T ntable0 slots0 i1 else Result.ret (ntable, slots) -/- [hashmap_main::hashmap::HashMap::{0}::move_elements] -/ -def hashmap.HashMap.move_elements_fwd_back +/- [hashmap_main::hashmap::HashMap::{0}::move_elements]: merged forward/backward function + (there is a single backward function, and the forward function returns ()) -/ +def hashmap.HashMap.move_elements (T : Type) (ntable : hashmap.HashMap T) (slots : Vec (hashmap.List T)) (i : Usize) : Result ((hashmap.HashMap T) × (Vec (hashmap.List T))) := - hashmap.HashMap.move_elements_loop_fwd_back T ntable slots i + hashmap.HashMap.move_elements_loop T ntable slots i -/- [hashmap_main::hashmap::HashMap::{0}::try_resize] -/ -def hashmap.HashMap.try_resize_fwd_back +/- [hashmap_main::hashmap::HashMap::{0}::try_resize]: merged forward/backward function + (there is a single backward function, and the forward function returns ()) -/ +def hashmap.HashMap.try_resize (T : Type) (self : hashmap.HashMap T) : Result (hashmap.HashMap T) := do let max_usize ← Scalar.cast .Usize core_num_u32_max_c @@ -229,10 +235,10 @@ def hashmap.HashMap.try_resize_fwd_back then do let i2 ← capacity * (Usize.ofInt 2 (by intlit)) - let ntable ← hashmap.HashMap.new_with_capacity_fwd T i2 i i0 + let ntable ← hashmap.HashMap.new_with_capacity T i2 i i0 let (ntable0, _) ← - hashmap.HashMap.move_elements_fwd_back T ntable - self.hashmap_hash_map_slots (Usize.ofInt 0 (by intlit)) + hashmap.HashMap.move_elements T ntable self.hashmap_hash_map_slots + (Usize.ofInt 0 (by intlit)) Result.ret { ntable0 @@ -242,84 +248,85 @@ def hashmap.HashMap.try_resize_fwd_back } else Result.ret { self with hashmap_hash_map_max_load_factor := (i, i0) } -/- [hashmap_main::hashmap::HashMap::{0}::insert] -/ -def hashmap.HashMap.insert_fwd_back +/- [hashmap_main::hashmap::HashMap::{0}::insert]: merged forward/backward function + (there is a single backward function, and the forward function returns ()) -/ +def hashmap.HashMap.insert (T : Type) (self : hashmap.HashMap T) (key : Usize) (value : T) : Result (hashmap.HashMap T) := do - let self0 ← hashmap.HashMap.insert_no_resize_fwd_back T self key value - let i ← hashmap.HashMap.len_fwd T self0 + let self0 ← hashmap.HashMap.insert_no_resize T self key value + let i ← hashmap.HashMap.len T self0 if i > self0.hashmap_hash_map_max_load - then hashmap.HashMap.try_resize_fwd_back T self0 + then hashmap.HashMap.try_resize T self0 else Result.ret self0 -/- [hashmap_main::hashmap::HashMap::{0}::contains_key_in_list] -/ -divergent def hashmap.HashMap.contains_key_in_list_loop_fwd +/- [hashmap_main::hashmap::HashMap::{0}::contains_key_in_list]: loop 0: forward function -/ +divergent def hashmap.HashMap.contains_key_in_list_loop (T : Type) (key : Usize) (ls : hashmap.List T) : Result Bool := match ls with | hashmap.List.Cons ckey t tl => if ckey = key then Result.ret true - else hashmap.HashMap.contains_key_in_list_loop_fwd T key tl + else hashmap.HashMap.contains_key_in_list_loop T key tl | hashmap.List.Nil => Result.ret false -/- [hashmap_main::hashmap::HashMap::{0}::contains_key_in_list] -/ -def hashmap.HashMap.contains_key_in_list_fwd +/- [hashmap_main::hashmap::HashMap::{0}::contains_key_in_list]: forward function -/ +def hashmap.HashMap.contains_key_in_list (T : Type) (key : Usize) (ls : hashmap.List T) : Result Bool := - hashmap.HashMap.contains_key_in_list_loop_fwd T key ls + hashmap.HashMap.contains_key_in_list_loop T key ls -/- [hashmap_main::hashmap::HashMap::{0}::contains_key] -/ -def hashmap.HashMap.contains_key_fwd +/- [hashmap_main::hashmap::HashMap::{0}::contains_key]: forward function -/ +def hashmap.HashMap.contains_key (T : Type) (self : hashmap.HashMap T) (key : Usize) : Result Bool := do - let hash ← hashmap.hash_key_fwd key + let hash ← hashmap.hash_key key let i := Vec.len (hashmap.List T) self.hashmap_hash_map_slots let hash_mod ← hash % i let l ← Vec.index (hashmap.List T) self.hashmap_hash_map_slots hash_mod - hashmap.HashMap.contains_key_in_list_fwd T key l + hashmap.HashMap.contains_key_in_list T key l -/- [hashmap_main::hashmap::HashMap::{0}::get_in_list] -/ -divergent def hashmap.HashMap.get_in_list_loop_fwd +/- [hashmap_main::hashmap::HashMap::{0}::get_in_list]: loop 0: forward function -/ +divergent def hashmap.HashMap.get_in_list_loop (T : Type) (key : Usize) (ls : hashmap.List T) : Result T := match ls with | hashmap.List.Cons ckey cvalue tl => if ckey = key then Result.ret cvalue - else hashmap.HashMap.get_in_list_loop_fwd T key tl + else hashmap.HashMap.get_in_list_loop T key tl | hashmap.List.Nil => Result.fail Error.panic -/- [hashmap_main::hashmap::HashMap::{0}::get_in_list] -/ -def hashmap.HashMap.get_in_list_fwd +/- [hashmap_main::hashmap::HashMap::{0}::get_in_list]: forward function -/ +def hashmap.HashMap.get_in_list (T : Type) (key : Usize) (ls : hashmap.List T) : Result T := - hashmap.HashMap.get_in_list_loop_fwd T key ls + hashmap.HashMap.get_in_list_loop T key ls -/- [hashmap_main::hashmap::HashMap::{0}::get] -/ -def hashmap.HashMap.get_fwd +/- [hashmap_main::hashmap::HashMap::{0}::get]: forward function -/ +def hashmap.HashMap.get (T : Type) (self : hashmap.HashMap T) (key : Usize) : Result T := do - let hash ← hashmap.hash_key_fwd key + let hash ← hashmap.hash_key key let i := Vec.len (hashmap.List T) self.hashmap_hash_map_slots let hash_mod ← hash % i let l ← Vec.index (hashmap.List T) self.hashmap_hash_map_slots hash_mod - hashmap.HashMap.get_in_list_fwd T key l + hashmap.HashMap.get_in_list T key l -/- [hashmap_main::hashmap::HashMap::{0}::get_mut_in_list] -/ -divergent def hashmap.HashMap.get_mut_in_list_loop_fwd +/- [hashmap_main::hashmap::HashMap::{0}::get_mut_in_list]: loop 0: forward function -/ +divergent def hashmap.HashMap.get_mut_in_list_loop (T : Type) (ls : hashmap.List T) (key : Usize) : Result T := match ls with | hashmap.List.Cons ckey cvalue tl => if ckey = key then Result.ret cvalue - else hashmap.HashMap.get_mut_in_list_loop_fwd T tl key + else hashmap.HashMap.get_mut_in_list_loop T tl key | hashmap.List.Nil => Result.fail Error.panic -/- [hashmap_main::hashmap::HashMap::{0}::get_mut_in_list] -/ -def hashmap.HashMap.get_mut_in_list_fwd +/- [hashmap_main::hashmap::HashMap::{0}::get_mut_in_list]: forward function -/ +def hashmap.HashMap.get_mut_in_list (T : Type) (ls : hashmap.List T) (key : Usize) : Result T := - hashmap.HashMap.get_mut_in_list_loop_fwd T ls key + hashmap.HashMap.get_mut_in_list_loop T ls key -/- [hashmap_main::hashmap::HashMap::{0}::get_mut_in_list] -/ +/- [hashmap_main::hashmap::HashMap::{0}::get_mut_in_list]: loop 0: backward function 0 -/ divergent def hashmap.HashMap.get_mut_in_list_loop_back (T : Type) (ls : hashmap.List T) (key : Usize) (ret0 : T) : Result (hashmap.List T) @@ -334,31 +341,31 @@ divergent def hashmap.HashMap.get_mut_in_list_loop_back Result.ret (hashmap.List.Cons ckey cvalue tl0) | hashmap.List.Nil => Result.fail Error.panic -/- [hashmap_main::hashmap::HashMap::{0}::get_mut_in_list] -/ +/- [hashmap_main::hashmap::HashMap::{0}::get_mut_in_list]: backward function 0 -/ def hashmap.HashMap.get_mut_in_list_back (T : Type) (ls : hashmap.List T) (key : Usize) (ret0 : T) : Result (hashmap.List T) := hashmap.HashMap.get_mut_in_list_loop_back T ls key ret0 -/- [hashmap_main::hashmap::HashMap::{0}::get_mut] -/ -def hashmap.HashMap.get_mut_fwd +/- [hashmap_main::hashmap::HashMap::{0}::get_mut]: forward function -/ +def hashmap.HashMap.get_mut (T : Type) (self : hashmap.HashMap T) (key : Usize) : Result T := do - let hash ← hashmap.hash_key_fwd key + let hash ← hashmap.hash_key key let i := Vec.len (hashmap.List T) self.hashmap_hash_map_slots let hash_mod ← hash % i let l ← Vec.index_mut (hashmap.List T) self.hashmap_hash_map_slots hash_mod - hashmap.HashMap.get_mut_in_list_fwd T l key + hashmap.HashMap.get_mut_in_list T l key -/- [hashmap_main::hashmap::HashMap::{0}::get_mut] -/ +/- [hashmap_main::hashmap::HashMap::{0}::get_mut]: backward function 0 -/ def hashmap.HashMap.get_mut_back (T : Type) (self : hashmap.HashMap T) (key : Usize) (ret0 : T) : Result (hashmap.HashMap T) := do - let hash ← hashmap.hash_key_fwd key + let hash ← hashmap.hash_key key let i := Vec.len (hashmap.List T) self.hashmap_hash_map_slots let hash_mod ← hash % i let l ← @@ -369,28 +376,28 @@ def hashmap.HashMap.get_mut_back l0 Result.ret { self with hashmap_hash_map_slots := v } -/- [hashmap_main::hashmap::HashMap::{0}::remove_from_list] -/ -divergent def hashmap.HashMap.remove_from_list_loop_fwd +/- [hashmap_main::hashmap::HashMap::{0}::remove_from_list]: loop 0: forward function -/ +divergent def hashmap.HashMap.remove_from_list_loop (T : Type) (key : Usize) (ls : hashmap.List T) : Result (Option T) := match ls with | hashmap.List.Cons ckey t tl => if ckey = key then let mv_ls := - mem.replace_fwd (hashmap.List T) (hashmap.List.Cons ckey t tl) + mem.replace (hashmap.List T) (hashmap.List.Cons ckey t tl) hashmap.List.Nil match mv_ls with | hashmap.List.Cons i cvalue tl0 => Result.ret (Option.some cvalue) | hashmap.List.Nil => Result.fail Error.panic - else hashmap.HashMap.remove_from_list_loop_fwd T key tl + else hashmap.HashMap.remove_from_list_loop T key tl | hashmap.List.Nil => Result.ret Option.none -/- [hashmap_main::hashmap::HashMap::{0}::remove_from_list] -/ -def hashmap.HashMap.remove_from_list_fwd +/- [hashmap_main::hashmap::HashMap::{0}::remove_from_list]: forward function -/ +def hashmap.HashMap.remove_from_list (T : Type) (key : Usize) (ls : hashmap.List T) : Result (Option T) := - hashmap.HashMap.remove_from_list_loop_fwd T key ls + hashmap.HashMap.remove_from_list_loop T key ls -/- [hashmap_main::hashmap::HashMap::{0}::remove_from_list] -/ +/- [hashmap_main::hashmap::HashMap::{0}::remove_from_list]: loop 0: backward function 1 -/ divergent def hashmap.HashMap.remove_from_list_loop_back (T : Type) (key : Usize) (ls : hashmap.List T) : Result (hashmap.List T) := match ls with @@ -398,7 +405,7 @@ divergent def hashmap.HashMap.remove_from_list_loop_back if ckey = key then let mv_ls := - mem.replace_fwd (hashmap.List T) (hashmap.List.Cons ckey t tl) + mem.replace (hashmap.List T) (hashmap.List.Cons ckey t tl) hashmap.List.Nil match mv_ls with | hashmap.List.Cons i cvalue tl0 => Result.ret tl0 @@ -409,21 +416,21 @@ divergent def hashmap.HashMap.remove_from_list_loop_back Result.ret (hashmap.List.Cons ckey t tl0) | hashmap.List.Nil => Result.ret hashmap.List.Nil -/- [hashmap_main::hashmap::HashMap::{0}::remove_from_list] -/ +/- [hashmap_main::hashmap::HashMap::{0}::remove_from_list]: backward function 1 -/ def hashmap.HashMap.remove_from_list_back (T : Type) (key : Usize) (ls : hashmap.List T) : Result (hashmap.List T) := hashmap.HashMap.remove_from_list_loop_back T key ls -/- [hashmap_main::hashmap::HashMap::{0}::remove] -/ -def hashmap.HashMap.remove_fwd +/- [hashmap_main::hashmap::HashMap::{0}::remove]: forward function -/ +def hashmap.HashMap.remove (T : Type) (self : hashmap.HashMap T) (key : Usize) : Result (Option T) := do - let hash ← hashmap.hash_key_fwd key + let hash ← hashmap.hash_key key let i := Vec.len (hashmap.List T) self.hashmap_hash_map_slots let hash_mod ← hash % i let l ← Vec.index_mut (hashmap.List T) self.hashmap_hash_map_slots hash_mod - let x ← hashmap.HashMap.remove_from_list_fwd T key l + let x ← hashmap.HashMap.remove_from_list T key l match x with | Option.none => Result.ret Option.none | Option.some x0 => @@ -432,18 +439,18 @@ def hashmap.HashMap.remove_fwd (Usize.ofInt 1 (by intlit)) Result.ret (Option.some x0) -/- [hashmap_main::hashmap::HashMap::{0}::remove] -/ +/- [hashmap_main::hashmap::HashMap::{0}::remove]: backward function 0 -/ def hashmap.HashMap.remove_back (T : Type) (self : hashmap.HashMap T) (key : Usize) : Result (hashmap.HashMap T) := do - let hash ← hashmap.hash_key_fwd key + let hash ← hashmap.hash_key key let i := Vec.len (hashmap.List T) self.hashmap_hash_map_slots let hash_mod ← hash % i let l ← Vec.index_mut (hashmap.List T) self.hashmap_hash_map_slots hash_mod - let x ← hashmap.HashMap.remove_from_list_fwd T key l + let x ← hashmap.HashMap.remove_from_list T key l match x with | Option.none => do @@ -467,23 +474,23 @@ def hashmap.HashMap.remove_back hashmap_hash_map_num_entries := i0, hashmap_hash_map_slots := v } -/- [hashmap_main::hashmap::test1] -/ -def hashmap.test1_fwd : Result Unit := +/- [hashmap_main::hashmap::test1]: forward function -/ +def hashmap.test1 : Result Unit := do - let hm ← hashmap.HashMap.new_fwd U64 + let hm ← hashmap.HashMap.new U64 let hm0 ← - hashmap.HashMap.insert_fwd_back U64 hm (Usize.ofInt 0 (by intlit)) + hashmap.HashMap.insert U64 hm (Usize.ofInt 0 (by intlit)) (U64.ofInt 42 (by intlit)) let hm1 ← - hashmap.HashMap.insert_fwd_back U64 hm0 (Usize.ofInt 128 (by intlit)) + hashmap.HashMap.insert U64 hm0 (Usize.ofInt 128 (by intlit)) (U64.ofInt 18 (by intlit)) let hm2 ← - hashmap.HashMap.insert_fwd_back U64 hm1 (Usize.ofInt 1024 (by intlit)) + hashmap.HashMap.insert U64 hm1 (Usize.ofInt 1024 (by intlit)) (U64.ofInt 138 (by intlit)) let hm3 ← - hashmap.HashMap.insert_fwd_back U64 hm2 (Usize.ofInt 1056 (by intlit)) + hashmap.HashMap.insert U64 hm2 (Usize.ofInt 1056 (by intlit)) (U64.ofInt 256 (by intlit)) - let i ← hashmap.HashMap.get_fwd U64 hm3 (Usize.ofInt 128 (by intlit)) + let i ← hashmap.HashMap.get U64 hm3 (Usize.ofInt 128 (by intlit)) if not (i = (U64.ofInt 18 (by intlit))) then Result.fail Error.panic else @@ -491,14 +498,13 @@ def hashmap.test1_fwd : Result Unit := let hm4 ← hashmap.HashMap.get_mut_back U64 hm3 (Usize.ofInt 1024 (by intlit)) (U64.ofInt 56 (by intlit)) - let i0 ← - hashmap.HashMap.get_fwd U64 hm4 (Usize.ofInt 1024 (by intlit)) + let i0 ← hashmap.HashMap.get U64 hm4 (Usize.ofInt 1024 (by intlit)) if not (i0 = (U64.ofInt 56 (by intlit))) then Result.fail Error.panic else do let x ← - hashmap.HashMap.remove_fwd U64 hm4 (Usize.ofInt 1024 (by intlit)) + hashmap.HashMap.remove U64 hm4 (Usize.ofInt 1024 (by intlit)) match x with | Option.none => Result.fail Error.panic | Option.some x0 => @@ -510,42 +516,42 @@ def hashmap.test1_fwd : Result Unit := hashmap.HashMap.remove_back U64 hm4 (Usize.ofInt 1024 (by intlit)) let i1 ← - hashmap.HashMap.get_fwd U64 hm5 (Usize.ofInt 0 (by intlit)) + hashmap.HashMap.get U64 hm5 (Usize.ofInt 0 (by intlit)) if not (i1 = (U64.ofInt 42 (by intlit))) then Result.fail Error.panic else do let i2 ← - hashmap.HashMap.get_fwd U64 hm5 + hashmap.HashMap.get U64 hm5 (Usize.ofInt 128 (by intlit)) if not (i2 = (U64.ofInt 18 (by intlit))) then Result.fail Error.panic else do let i3 ← - hashmap.HashMap.get_fwd U64 hm5 + hashmap.HashMap.get U64 hm5 (Usize.ofInt 1056 (by intlit)) if not (i3 = (U64.ofInt 256 (by intlit))) then Result.fail Error.panic else Result.ret () /- Unit test for [hashmap_main::hashmap::test1] -/ -#assert (hashmap.test1_fwd == .ret ()) +#assert (hashmap.test1 == .ret ()) -/- [hashmap_main::insert_on_disk] -/ -def insert_on_disk_fwd +/- [hashmap_main::insert_on_disk]: forward function -/ +def insert_on_disk (key : Usize) (value : U64) (st : State) : Result (State × Unit) := do - let (st0, hm) ← hashmap_utils.deserialize_fwd st - let hm0 ← hashmap.HashMap.insert_fwd_back U64 hm key value - let (st1, _) ← hashmap_utils.serialize_fwd hm0 st0 + let (st0, hm) ← hashmap_utils.deserialize st + let hm0 ← hashmap.HashMap.insert U64 hm key value + let (st1, _) ← hashmap_utils.serialize hm0 st0 Result.ret (st1, ()) -/- [hashmap_main::main] -/ -def main_fwd : Result Unit := +/- [hashmap_main::main]: forward function -/ +def main : Result Unit := Result.ret () /- Unit test for [hashmap_main::main] -/ -#assert (main_fwd == .ret ()) +#assert (main == .ret ()) end hashmap_main diff --git a/tests/lean/HashmapMain/FunsExternal.lean b/tests/lean/HashmapMain/FunsExternal.lean index 3689dd45..b394b32b 100644 --- a/tests/lean/HashmapMain/FunsExternal.lean +++ b/tests/lean/HashmapMain/FunsExternal.lean @@ -7,11 +7,11 @@ open hashmap_main -- TODO: fill those bodies /- [hashmap_main::hashmap_utils::deserialize] -/ -def hashmap_utils.deserialize_fwd +def hashmap_utils.deserialize : State → Result (State × (hashmap.HashMap U64)) := fun _ => .fail .panic /- [hashmap_main::hashmap_utils::serialize] -/ -def hashmap_utils.serialize_fwd +def hashmap_utils.serialize : hashmap.HashMap U64 → State → Result (State × Unit) := fun _ _ => .fail .panic diff --git a/tests/lean/HashmapMain/FunsExternal_Template.lean b/tests/lean/HashmapMain/FunsExternal_Template.lean index 8853b7fc..f537fc8f 100644 --- a/tests/lean/HashmapMain/FunsExternal_Template.lean +++ b/tests/lean/HashmapMain/FunsExternal_Template.lean @@ -6,11 +6,11 @@ import HashmapMain.Types open Primitives open hashmap_main -/- [hashmap_main::hashmap_utils::deserialize] -/ -axiom hashmap_utils.deserialize_fwd +/- [hashmap_main::hashmap_utils::deserialize]: forward function -/ +axiom hashmap_utils.deserialize : State → Result (State × (hashmap.HashMap U64)) -/- [hashmap_main::hashmap_utils::serialize] -/ -axiom hashmap_utils.serialize_fwd +/- [hashmap_main::hashmap_utils::serialize]: forward function -/ +axiom hashmap_utils.serialize : hashmap.HashMap U64 → State → Result (State × Unit) diff --git a/tests/lean/Loops/Funs.lean b/tests/lean/Loops/Funs.lean index 6e6eef3b..8cac7ac0 100644 --- a/tests/lean/Loops/Funs.lean +++ b/tests/lean/Loops/Funs.lean @@ -5,82 +5,83 @@ import Loops.Types open Primitives namespace loops -/- [loops::sum] -/ -divergent def sum_loop_fwd (max : U32) (i : U32) (s : U32) : Result U32 := +/- [loops::sum]: loop 0: forward function -/ +divergent def sum_loop (max : U32) (i : U32) (s : U32) : Result U32 := if i < max then do let s0 ← s + i let i0 ← i + (U32.ofInt 1 (by intlit)) - sum_loop_fwd max i0 s0 + sum_loop max i0 s0 else s * (U32.ofInt 2 (by intlit)) -/- [loops::sum] -/ -def sum_fwd (max : U32) : Result U32 := - sum_loop_fwd max (U32.ofInt 0 (by intlit)) (U32.ofInt 0 (by intlit)) +/- [loops::sum]: forward function -/ +def sum (max : U32) : Result U32 := + sum_loop max (U32.ofInt 0 (by intlit)) (U32.ofInt 0 (by intlit)) -/- [loops::sum_with_mut_borrows] -/ -divergent def sum_with_mut_borrows_loop_fwd +/- [loops::sum_with_mut_borrows]: loop 0: forward function -/ +divergent def sum_with_mut_borrows_loop (max : U32) (mi : U32) (ms : U32) : Result U32 := if mi < max then do let ms0 ← ms + mi let mi0 ← mi + (U32.ofInt 1 (by intlit)) - sum_with_mut_borrows_loop_fwd max mi0 ms0 + sum_with_mut_borrows_loop max mi0 ms0 else ms * (U32.ofInt 2 (by intlit)) -/- [loops::sum_with_mut_borrows] -/ -def sum_with_mut_borrows_fwd (max : U32) : Result U32 := - sum_with_mut_borrows_loop_fwd max (U32.ofInt 0 (by intlit)) +/- [loops::sum_with_mut_borrows]: forward function -/ +def sum_with_mut_borrows (max : U32) : Result U32 := + sum_with_mut_borrows_loop max (U32.ofInt 0 (by intlit)) (U32.ofInt 0 (by intlit)) -/- [loops::sum_with_shared_borrows] -/ -divergent def sum_with_shared_borrows_loop_fwd +/- [loops::sum_with_shared_borrows]: loop 0: forward function -/ +divergent def sum_with_shared_borrows_loop (max : U32) (i : U32) (s : U32) : Result U32 := if i < max then do let i0 ← i + (U32.ofInt 1 (by intlit)) let s0 ← s + i0 - sum_with_shared_borrows_loop_fwd max i0 s0 + sum_with_shared_borrows_loop max i0 s0 else s * (U32.ofInt 2 (by intlit)) -/- [loops::sum_with_shared_borrows] -/ -def sum_with_shared_borrows_fwd (max : U32) : Result U32 := - sum_with_shared_borrows_loop_fwd max (U32.ofInt 0 (by intlit)) +/- [loops::sum_with_shared_borrows]: forward function -/ +def sum_with_shared_borrows (max : U32) : Result U32 := + sum_with_shared_borrows_loop max (U32.ofInt 0 (by intlit)) (U32.ofInt 0 (by intlit)) -/- [loops::clear] -/ -divergent def clear_loop_fwd_back - (v : Vec U32) (i : Usize) : Result (Vec U32) := +/- [loops::clear]: loop 0: merged forward/backward function + (there is a single backward function, and the forward function returns ()) -/ +divergent def clear_loop (v : Vec U32) (i : Usize) : Result (Vec U32) := let i0 := Vec.len U32 v if i < i0 then do let i1 ← i + (Usize.ofInt 1 (by intlit)) let v0 ← Vec.index_mut_back U32 v i (U32.ofInt 0 (by intlit)) - clear_loop_fwd_back v0 i1 + clear_loop v0 i1 else Result.ret v -/- [loops::clear] -/ -def clear_fwd_back (v : Vec U32) : Result (Vec U32) := - clear_loop_fwd_back v (Usize.ofInt 0 (by intlit)) +/- [loops::clear]: merged forward/backward function + (there is a single backward function, and the forward function returns ()) -/ +def clear (v : Vec U32) : Result (Vec U32) := + clear_loop v (Usize.ofInt 0 (by intlit)) -/- [loops::list_mem] -/ -divergent def list_mem_loop_fwd (x : U32) (ls : List U32) : Result Bool := +/- [loops::list_mem]: loop 0: forward function -/ +divergent def list_mem_loop (x : U32) (ls : List U32) : Result Bool := match ls with | List.Cons y tl => if y = x then Result.ret true - else list_mem_loop_fwd x tl + else list_mem_loop x tl | List.Nil => Result.ret false -/- [loops::list_mem] -/ -def list_mem_fwd (x : U32) (ls : List U32) : Result Bool := - list_mem_loop_fwd x ls +/- [loops::list_mem]: forward function -/ +def list_mem (x : U32) (ls : List U32) : Result Bool := + list_mem_loop x ls -/- [loops::list_nth_mut_loop] -/ -divergent def list_nth_mut_loop_loop_fwd +/- [loops::list_nth_mut_loop]: loop 0: forward function -/ +divergent def list_nth_mut_loop_loop (T : Type) (ls : List T) (i : U32) : Result T := match ls with | List.Cons x tl => @@ -89,14 +90,14 @@ divergent def list_nth_mut_loop_loop_fwd else do let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_mut_loop_loop_fwd T tl i0 + list_nth_mut_loop_loop T tl i0 | List.Nil => Result.fail Error.panic -/- [loops::list_nth_mut_loop] -/ -def list_nth_mut_loop_fwd (T : Type) (ls : List T) (i : U32) : Result T := - list_nth_mut_loop_loop_fwd T ls i +/- [loops::list_nth_mut_loop]: forward function -/ +def list_nth_mut_loop (T : Type) (ls : List T) (i : U32) : Result T := + list_nth_mut_loop_loop T ls i -/- [loops::list_nth_mut_loop] -/ +/- [loops::list_nth_mut_loop]: loop 0: backward function 0 -/ divergent def list_nth_mut_loop_loop_back (T : Type) (ls : List T) (i : U32) (ret0 : T) : Result (List T) := match ls with @@ -110,13 +111,13 @@ divergent def list_nth_mut_loop_loop_back Result.ret (List.Cons x tl0) | List.Nil => Result.fail Error.panic -/- [loops::list_nth_mut_loop] -/ +/- [loops::list_nth_mut_loop]: backward function 0 -/ def list_nth_mut_loop_back (T : Type) (ls : List T) (i : U32) (ret0 : T) : Result (List T) := list_nth_mut_loop_loop_back T ls i ret0 -/- [loops::list_nth_shared_loop] -/ -divergent def list_nth_shared_loop_loop_fwd +/- [loops::list_nth_shared_loop]: loop 0: forward function -/ +divergent def list_nth_shared_loop_loop (T : Type) (ls : List T) (i : U32) : Result T := match ls with | List.Cons x tl => @@ -125,30 +126,28 @@ divergent def list_nth_shared_loop_loop_fwd else do let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_shared_loop_loop_fwd T tl i0 + list_nth_shared_loop_loop T tl i0 | List.Nil => Result.fail Error.panic -/- [loops::list_nth_shared_loop] -/ -def list_nth_shared_loop_fwd (T : Type) (ls : List T) (i : U32) : Result T := - list_nth_shared_loop_loop_fwd T ls i +/- [loops::list_nth_shared_loop]: forward function -/ +def list_nth_shared_loop (T : Type) (ls : List T) (i : U32) : Result T := + list_nth_shared_loop_loop T ls i -/- [loops::get_elem_mut] -/ -divergent def get_elem_mut_loop_fwd - (x : Usize) (ls : List Usize) : Result Usize := +/- [loops::get_elem_mut]: loop 0: forward function -/ +divergent def get_elem_mut_loop (x : Usize) (ls : List Usize) : Result Usize := match ls with - | List.Cons y tl => - if y = x - then Result.ret y - else get_elem_mut_loop_fwd x tl + | List.Cons y tl => if y = x + then Result.ret y + else get_elem_mut_loop x tl | List.Nil => Result.fail Error.panic -/- [loops::get_elem_mut] -/ -def get_elem_mut_fwd (slots : Vec (List Usize)) (x : Usize) : Result Usize := +/- [loops::get_elem_mut]: forward function -/ +def get_elem_mut (slots : Vec (List Usize)) (x : Usize) : Result Usize := do let l ← Vec.index_mut (List Usize) slots (Usize.ofInt 0 (by intlit)) - get_elem_mut_loop_fwd x l + get_elem_mut_loop x l -/- [loops::get_elem_mut] -/ +/- [loops::get_elem_mut]: loop 0: backward function 0 -/ divergent def get_elem_mut_loop_back (x : Usize) (ls : List Usize) (ret0 : Usize) : Result (List Usize) := match ls with @@ -161,7 +160,7 @@ divergent def get_elem_mut_loop_back Result.ret (List.Cons y tl0) | List.Nil => Result.fail Error.panic -/- [loops::get_elem_mut] -/ +/- [loops::get_elem_mut]: backward function 0 -/ def get_elem_mut_back (slots : Vec (List Usize)) (x : Usize) (ret0 : Usize) : Result (Vec (List Usize)) @@ -171,37 +170,35 @@ def get_elem_mut_back let l0 ← get_elem_mut_loop_back x l ret0 Vec.index_mut_back (List Usize) slots (Usize.ofInt 0 (by intlit)) l0 -/- [loops::get_elem_shared] -/ -divergent def get_elem_shared_loop_fwd +/- [loops::get_elem_shared]: loop 0: forward function -/ +divergent def get_elem_shared_loop (x : Usize) (ls : List Usize) : Result Usize := match ls with - | List.Cons y tl => - if y = x - then Result.ret y - else get_elem_shared_loop_fwd x tl + | List.Cons y tl => if y = x + then Result.ret y + else get_elem_shared_loop x tl | List.Nil => Result.fail Error.panic -/- [loops::get_elem_shared] -/ -def get_elem_shared_fwd - (slots : Vec (List Usize)) (x : Usize) : Result Usize := +/- [loops::get_elem_shared]: forward function -/ +def get_elem_shared (slots : Vec (List Usize)) (x : Usize) : Result Usize := do let l ← Vec.index (List Usize) slots (Usize.ofInt 0 (by intlit)) - get_elem_shared_loop_fwd x l + get_elem_shared_loop x l -/- [loops::id_mut] -/ -def id_mut_fwd (T : Type) (ls : List T) : Result (List T) := +/- [loops::id_mut]: forward function -/ +def id_mut (T : Type) (ls : List T) : Result (List T) := Result.ret ls -/- [loops::id_mut] -/ +/- [loops::id_mut]: backward function 0 -/ def id_mut_back (T : Type) (ls : List T) (ret0 : List T) : Result (List T) := Result.ret ret0 -/- [loops::id_shared] -/ -def id_shared_fwd (T : Type) (ls : List T) : Result (List T) := +/- [loops::id_shared]: forward function -/ +def id_shared (T : Type) (ls : List T) : Result (List T) := Result.ret ls -/- [loops::list_nth_mut_loop_with_id] -/ -divergent def list_nth_mut_loop_with_id_loop_fwd +/- [loops::list_nth_mut_loop_with_id]: loop 0: forward function -/ +divergent def list_nth_mut_loop_with_id_loop (T : Type) (i : U32) (ls : List T) : Result T := match ls with | List.Cons x tl => @@ -210,17 +207,16 @@ divergent def list_nth_mut_loop_with_id_loop_fwd else do let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_mut_loop_with_id_loop_fwd T i0 tl + list_nth_mut_loop_with_id_loop T i0 tl | List.Nil => Result.fail Error.panic -/- [loops::list_nth_mut_loop_with_id] -/ -def list_nth_mut_loop_with_id_fwd - (T : Type) (ls : List T) (i : U32) : Result T := +/- [loops::list_nth_mut_loop_with_id]: forward function -/ +def list_nth_mut_loop_with_id (T : Type) (ls : List T) (i : U32) : Result T := do - let ls0 ← id_mut_fwd T ls - list_nth_mut_loop_with_id_loop_fwd T i ls0 + let ls0 ← id_mut T ls + list_nth_mut_loop_with_id_loop T i ls0 -/- [loops::list_nth_mut_loop_with_id] -/ +/- [loops::list_nth_mut_loop_with_id]: loop 0: backward function 0 -/ divergent def list_nth_mut_loop_with_id_loop_back (T : Type) (i : U32) (ls : List T) (ret0 : T) : Result (List T) := match ls with @@ -234,16 +230,16 @@ divergent def list_nth_mut_loop_with_id_loop_back Result.ret (List.Cons x tl0) | List.Nil => Result.fail Error.panic -/- [loops::list_nth_mut_loop_with_id] -/ +/- [loops::list_nth_mut_loop_with_id]: backward function 0 -/ def list_nth_mut_loop_with_id_back (T : Type) (ls : List T) (i : U32) (ret0 : T) : Result (List T) := do - let ls0 ← id_mut_fwd T ls + let ls0 ← id_mut T ls let l ← list_nth_mut_loop_with_id_loop_back T i ls0 ret0 id_mut_back T ls l -/- [loops::list_nth_shared_loop_with_id] -/ -divergent def list_nth_shared_loop_with_id_loop_fwd +/- [loops::list_nth_shared_loop_with_id]: loop 0: forward function -/ +divergent def list_nth_shared_loop_with_id_loop (T : Type) (i : U32) (ls : List T) : Result T := match ls with | List.Cons x tl => @@ -252,18 +248,18 @@ divergent def list_nth_shared_loop_with_id_loop_fwd else do let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_shared_loop_with_id_loop_fwd T i0 tl + list_nth_shared_loop_with_id_loop T i0 tl | List.Nil => Result.fail Error.panic -/- [loops::list_nth_shared_loop_with_id] -/ -def list_nth_shared_loop_with_id_fwd +/- [loops::list_nth_shared_loop_with_id]: forward function -/ +def list_nth_shared_loop_with_id (T : Type) (ls : List T) (i : U32) : Result T := do - let ls0 ← id_shared_fwd T ls - list_nth_shared_loop_with_id_loop_fwd T i ls0 + let ls0 ← id_shared T ls + list_nth_shared_loop_with_id_loop T i ls0 -/- [loops::list_nth_mut_loop_pair] -/ -divergent def list_nth_mut_loop_pair_loop_fwd +/- [loops::list_nth_mut_loop_pair]: loop 0: forward function -/ +divergent def list_nth_mut_loop_pair_loop (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) : Result (T × T) := match ls0 with | List.Cons x0 tl0 => @@ -274,16 +270,16 @@ divergent def list_nth_mut_loop_pair_loop_fwd else do let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_mut_loop_pair_loop_fwd T tl0 tl1 i0 + list_nth_mut_loop_pair_loop T tl0 tl1 i0 | List.Nil => Result.fail Error.panic | List.Nil => Result.fail Error.panic -/- [loops::list_nth_mut_loop_pair] -/ -def list_nth_mut_loop_pair_fwd +/- [loops::list_nth_mut_loop_pair]: forward function -/ +def list_nth_mut_loop_pair (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) : Result (T × T) := - list_nth_mut_loop_pair_loop_fwd T ls0 ls1 i + list_nth_mut_loop_pair_loop T ls0 ls1 i -/- [loops::list_nth_mut_loop_pair] -/ +/- [loops::list_nth_mut_loop_pair]: loop 0: backward function 0 -/ divergent def list_nth_mut_loop_pair_loop_back'a (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) (ret0 : T) : Result (List T) @@ -302,14 +298,14 @@ divergent def list_nth_mut_loop_pair_loop_back'a | List.Nil => Result.fail Error.panic | List.Nil => Result.fail Error.panic -/- [loops::list_nth_mut_loop_pair] -/ +/- [loops::list_nth_mut_loop_pair]: backward function 0 -/ def list_nth_mut_loop_pair_back'a (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) (ret0 : T) : Result (List T) := list_nth_mut_loop_pair_loop_back'a T ls0 ls1 i ret0 -/- [loops::list_nth_mut_loop_pair] -/ +/- [loops::list_nth_mut_loop_pair]: loop 0: backward function 1 -/ divergent def list_nth_mut_loop_pair_loop_back'b (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) (ret0 : T) : Result (List T) @@ -328,15 +324,15 @@ divergent def list_nth_mut_loop_pair_loop_back'b | List.Nil => Result.fail Error.panic | List.Nil => Result.fail Error.panic -/- [loops::list_nth_mut_loop_pair] -/ +/- [loops::list_nth_mut_loop_pair]: backward function 1 -/ def list_nth_mut_loop_pair_back'b (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) (ret0 : T) : Result (List T) := list_nth_mut_loop_pair_loop_back'b T ls0 ls1 i ret0 -/- [loops::list_nth_shared_loop_pair] -/ -divergent def list_nth_shared_loop_pair_loop_fwd +/- [loops::list_nth_shared_loop_pair]: loop 0: forward function -/ +divergent def list_nth_shared_loop_pair_loop (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) : Result (T × T) := match ls0 with | List.Cons x0 tl0 => @@ -347,17 +343,17 @@ divergent def list_nth_shared_loop_pair_loop_fwd else do let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_shared_loop_pair_loop_fwd T tl0 tl1 i0 + list_nth_shared_loop_pair_loop T tl0 tl1 i0 | List.Nil => Result.fail Error.panic | List.Nil => Result.fail Error.panic -/- [loops::list_nth_shared_loop_pair] -/ -def list_nth_shared_loop_pair_fwd +/- [loops::list_nth_shared_loop_pair]: forward function -/ +def list_nth_shared_loop_pair (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) : Result (T × T) := - list_nth_shared_loop_pair_loop_fwd T ls0 ls1 i + list_nth_shared_loop_pair_loop T ls0 ls1 i -/- [loops::list_nth_mut_loop_pair_merge] -/ -divergent def list_nth_mut_loop_pair_merge_loop_fwd +/- [loops::list_nth_mut_loop_pair_merge]: loop 0: forward function -/ +divergent def list_nth_mut_loop_pair_merge_loop (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) : Result (T × T) := match ls0 with | List.Cons x0 tl0 => @@ -368,16 +364,16 @@ divergent def list_nth_mut_loop_pair_merge_loop_fwd else do let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_mut_loop_pair_merge_loop_fwd T tl0 tl1 i0 + list_nth_mut_loop_pair_merge_loop T tl0 tl1 i0 | List.Nil => Result.fail Error.panic | List.Nil => Result.fail Error.panic -/- [loops::list_nth_mut_loop_pair_merge] -/ -def list_nth_mut_loop_pair_merge_fwd +/- [loops::list_nth_mut_loop_pair_merge]: forward function -/ +def list_nth_mut_loop_pair_merge (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) : Result (T × T) := - list_nth_mut_loop_pair_merge_loop_fwd T ls0 ls1 i + list_nth_mut_loop_pair_merge_loop T ls0 ls1 i -/- [loops::list_nth_mut_loop_pair_merge] -/ +/- [loops::list_nth_mut_loop_pair_merge]: loop 0: backward function 0 -/ divergent def list_nth_mut_loop_pair_merge_loop_back (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) (ret0 : (T × T)) : Result ((List T) × (List T)) @@ -398,15 +394,15 @@ divergent def list_nth_mut_loop_pair_merge_loop_back | List.Nil => Result.fail Error.panic | List.Nil => Result.fail Error.panic -/- [loops::list_nth_mut_loop_pair_merge] -/ +/- [loops::list_nth_mut_loop_pair_merge]: backward function 0 -/ def list_nth_mut_loop_pair_merge_back (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) (ret0 : (T × T)) : Result ((List T) × (List T)) := list_nth_mut_loop_pair_merge_loop_back T ls0 ls1 i ret0 -/- [loops::list_nth_shared_loop_pair_merge] -/ -divergent def list_nth_shared_loop_pair_merge_loop_fwd +/- [loops::list_nth_shared_loop_pair_merge]: loop 0: forward function -/ +divergent def list_nth_shared_loop_pair_merge_loop (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) : Result (T × T) := match ls0 with | List.Cons x0 tl0 => @@ -417,17 +413,17 @@ divergent def list_nth_shared_loop_pair_merge_loop_fwd else do let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_shared_loop_pair_merge_loop_fwd T tl0 tl1 i0 + list_nth_shared_loop_pair_merge_loop T tl0 tl1 i0 | List.Nil => Result.fail Error.panic | List.Nil => Result.fail Error.panic -/- [loops::list_nth_shared_loop_pair_merge] -/ -def list_nth_shared_loop_pair_merge_fwd +/- [loops::list_nth_shared_loop_pair_merge]: forward function -/ +def list_nth_shared_loop_pair_merge (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) : Result (T × T) := - list_nth_shared_loop_pair_merge_loop_fwd T ls0 ls1 i + list_nth_shared_loop_pair_merge_loop T ls0 ls1 i -/- [loops::list_nth_mut_shared_loop_pair] -/ -divergent def list_nth_mut_shared_loop_pair_loop_fwd +/- [loops::list_nth_mut_shared_loop_pair]: loop 0: forward function -/ +divergent def list_nth_mut_shared_loop_pair_loop (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) : Result (T × T) := match ls0 with | List.Cons x0 tl0 => @@ -438,16 +434,16 @@ divergent def list_nth_mut_shared_loop_pair_loop_fwd else do let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_mut_shared_loop_pair_loop_fwd T tl0 tl1 i0 + list_nth_mut_shared_loop_pair_loop T tl0 tl1 i0 | List.Nil => Result.fail Error.panic | List.Nil => Result.fail Error.panic -/- [loops::list_nth_mut_shared_loop_pair] -/ -def list_nth_mut_shared_loop_pair_fwd +/- [loops::list_nth_mut_shared_loop_pair]: forward function -/ +def list_nth_mut_shared_loop_pair (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) : Result (T × T) := - list_nth_mut_shared_loop_pair_loop_fwd T ls0 ls1 i + list_nth_mut_shared_loop_pair_loop T ls0 ls1 i -/- [loops::list_nth_mut_shared_loop_pair] -/ +/- [loops::list_nth_mut_shared_loop_pair]: loop 0: backward function 0 -/ divergent def list_nth_mut_shared_loop_pair_loop_back (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) (ret0 : T) : Result (List T) @@ -467,15 +463,15 @@ divergent def list_nth_mut_shared_loop_pair_loop_back | List.Nil => Result.fail Error.panic | List.Nil => Result.fail Error.panic -/- [loops::list_nth_mut_shared_loop_pair] -/ +/- [loops::list_nth_mut_shared_loop_pair]: backward function 0 -/ def list_nth_mut_shared_loop_pair_back (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) (ret0 : T) : Result (List T) := list_nth_mut_shared_loop_pair_loop_back T ls0 ls1 i ret0 -/- [loops::list_nth_mut_shared_loop_pair_merge] -/ -divergent def list_nth_mut_shared_loop_pair_merge_loop_fwd +/- [loops::list_nth_mut_shared_loop_pair_merge]: loop 0: forward function -/ +divergent def list_nth_mut_shared_loop_pair_merge_loop (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) : Result (T × T) := match ls0 with | List.Cons x0 tl0 => @@ -486,16 +482,16 @@ divergent def list_nth_mut_shared_loop_pair_merge_loop_fwd else do let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_mut_shared_loop_pair_merge_loop_fwd T tl0 tl1 i0 + list_nth_mut_shared_loop_pair_merge_loop T tl0 tl1 i0 | List.Nil => Result.fail Error.panic | List.Nil => Result.fail Error.panic -/- [loops::list_nth_mut_shared_loop_pair_merge] -/ -def list_nth_mut_shared_loop_pair_merge_fwd +/- [loops::list_nth_mut_shared_loop_pair_merge]: forward function -/ +def list_nth_mut_shared_loop_pair_merge (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) : Result (T × T) := - list_nth_mut_shared_loop_pair_merge_loop_fwd T ls0 ls1 i + list_nth_mut_shared_loop_pair_merge_loop T ls0 ls1 i -/- [loops::list_nth_mut_shared_loop_pair_merge] -/ +/- [loops::list_nth_mut_shared_loop_pair_merge]: loop 0: backward function 0 -/ divergent def list_nth_mut_shared_loop_pair_merge_loop_back (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) (ret0 : T) : Result (List T) @@ -515,15 +511,15 @@ divergent def list_nth_mut_shared_loop_pair_merge_loop_back | List.Nil => Result.fail Error.panic | List.Nil => Result.fail Error.panic -/- [loops::list_nth_mut_shared_loop_pair_merge] -/ +/- [loops::list_nth_mut_shared_loop_pair_merge]: backward function 0 -/ def list_nth_mut_shared_loop_pair_merge_back (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) (ret0 : T) : Result (List T) := list_nth_mut_shared_loop_pair_merge_loop_back T ls0 ls1 i ret0 -/- [loops::list_nth_shared_mut_loop_pair] -/ -divergent def list_nth_shared_mut_loop_pair_loop_fwd +/- [loops::list_nth_shared_mut_loop_pair]: loop 0: forward function -/ +divergent def list_nth_shared_mut_loop_pair_loop (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) : Result (T × T) := match ls0 with | List.Cons x0 tl0 => @@ -534,16 +530,16 @@ divergent def list_nth_shared_mut_loop_pair_loop_fwd else do let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_shared_mut_loop_pair_loop_fwd T tl0 tl1 i0 + list_nth_shared_mut_loop_pair_loop T tl0 tl1 i0 | List.Nil => Result.fail Error.panic | List.Nil => Result.fail Error.panic -/- [loops::list_nth_shared_mut_loop_pair] -/ -def list_nth_shared_mut_loop_pair_fwd +/- [loops::list_nth_shared_mut_loop_pair]: forward function -/ +def list_nth_shared_mut_loop_pair (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) : Result (T × T) := - list_nth_shared_mut_loop_pair_loop_fwd T ls0 ls1 i + list_nth_shared_mut_loop_pair_loop T ls0 ls1 i -/- [loops::list_nth_shared_mut_loop_pair] -/ +/- [loops::list_nth_shared_mut_loop_pair]: loop 0: backward function 1 -/ divergent def list_nth_shared_mut_loop_pair_loop_back (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) (ret0 : T) : Result (List T) @@ -563,15 +559,15 @@ divergent def list_nth_shared_mut_loop_pair_loop_back | List.Nil => Result.fail Error.panic | List.Nil => Result.fail Error.panic -/- [loops::list_nth_shared_mut_loop_pair] -/ +/- [loops::list_nth_shared_mut_loop_pair]: backward function 1 -/ def list_nth_shared_mut_loop_pair_back (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) (ret0 : T) : Result (List T) := list_nth_shared_mut_loop_pair_loop_back T ls0 ls1 i ret0 -/- [loops::list_nth_shared_mut_loop_pair_merge] -/ -divergent def list_nth_shared_mut_loop_pair_merge_loop_fwd +/- [loops::list_nth_shared_mut_loop_pair_merge]: loop 0: forward function -/ +divergent def list_nth_shared_mut_loop_pair_merge_loop (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) : Result (T × T) := match ls0 with | List.Cons x0 tl0 => @@ -582,16 +578,16 @@ divergent def list_nth_shared_mut_loop_pair_merge_loop_fwd else do let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_shared_mut_loop_pair_merge_loop_fwd T tl0 tl1 i0 + list_nth_shared_mut_loop_pair_merge_loop T tl0 tl1 i0 | List.Nil => Result.fail Error.panic | List.Nil => Result.fail Error.panic -/- [loops::list_nth_shared_mut_loop_pair_merge] -/ -def list_nth_shared_mut_loop_pair_merge_fwd +/- [loops::list_nth_shared_mut_loop_pair_merge]: forward function -/ +def list_nth_shared_mut_loop_pair_merge (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) : Result (T × T) := - list_nth_shared_mut_loop_pair_merge_loop_fwd T ls0 ls1 i + list_nth_shared_mut_loop_pair_merge_loop T ls0 ls1 i -/- [loops::list_nth_shared_mut_loop_pair_merge] -/ +/- [loops::list_nth_shared_mut_loop_pair_merge]: loop 0: backward function 0 -/ divergent def list_nth_shared_mut_loop_pair_merge_loop_back (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) (ret0 : T) : Result (List T) @@ -611,7 +607,7 @@ divergent def list_nth_shared_mut_loop_pair_merge_loop_back | List.Nil => Result.fail Error.panic | List.Nil => Result.fail Error.panic -/- [loops::list_nth_shared_mut_loop_pair_merge] -/ +/- [loops::list_nth_shared_mut_loop_pair_merge]: backward function 0 -/ def list_nth_shared_mut_loop_pair_merge_back (T : Type) (ls0 : List T) (ls1 : List T) (i : U32) (ret0 : T) : Result (List T) diff --git a/tests/lean/NoNestedBorrows.lean b/tests/lean/NoNestedBorrows.lean index e4fa7612..dc744a29 100644 --- a/tests/lean/NoNestedBorrows.lean +++ b/tests/lean/NoNestedBorrows.lean @@ -35,64 +35,64 @@ inductive Sum (T1 T2 : Type) := | Left : T1 → Sum T1 T2 | Right : T2 → Sum T1 T2 -/- [no_nested_borrows::neg_test] -/ -def neg_test_fwd (x : I32) : Result I32 := +/- [no_nested_borrows::neg_test]: forward function -/ +def neg_test (x : I32) : Result I32 := - x -/- [no_nested_borrows::add_test] -/ -def add_test_fwd (x : U32) (y : U32) : Result U32 := +/- [no_nested_borrows::add_test]: forward function -/ +def add_test (x : U32) (y : U32) : Result U32 := x + y -/- [no_nested_borrows::subs_test] -/ -def subs_test_fwd (x : U32) (y : U32) : Result U32 := +/- [no_nested_borrows::subs_test]: forward function -/ +def subs_test (x : U32) (y : U32) : Result U32 := x - y -/- [no_nested_borrows::div_test] -/ -def div_test_fwd (x : U32) (y : U32) : Result U32 := +/- [no_nested_borrows::div_test]: forward function -/ +def div_test (x : U32) (y : U32) : Result U32 := x / y -/- [no_nested_borrows::div_test1] -/ -def div_test1_fwd (x : U32) : Result U32 := +/- [no_nested_borrows::div_test1]: forward function -/ +def div_test1 (x : U32) : Result U32 := x / (U32.ofInt 2 (by intlit)) -/- [no_nested_borrows::rem_test] -/ -def rem_test_fwd (x : U32) (y : U32) : Result U32 := +/- [no_nested_borrows::rem_test]: forward function -/ +def rem_test (x : U32) (y : U32) : Result U32 := x % y -/- [no_nested_borrows::cast_test] -/ -def cast_test_fwd (x : U32) : Result I32 := +/- [no_nested_borrows::cast_test]: forward function -/ +def cast_test (x : U32) : Result I32 := Scalar.cast .I32 x -/- [no_nested_borrows::test2] -/ -def test2_fwd : Result Unit := +/- [no_nested_borrows::test2]: forward function -/ +def test2 : Result Unit := do let _ ← (U32.ofInt 23 (by intlit)) + (U32.ofInt 44 (by intlit)) Result.ret () /- Unit test for [no_nested_borrows::test2] -/ -#assert (test2_fwd == .ret ()) +#assert (test2 == .ret ()) -/- [no_nested_borrows::get_max] -/ -def get_max_fwd (x : U32) (y : U32) : Result U32 := +/- [no_nested_borrows::get_max]: forward function -/ +def get_max (x : U32) (y : U32) : Result U32 := if x >= y then Result.ret x else Result.ret y -/- [no_nested_borrows::test3] -/ -def test3_fwd : Result Unit := +/- [no_nested_borrows::test3]: forward function -/ +def test3 : Result Unit := do - let x ← get_max_fwd (U32.ofInt 4 (by intlit)) (U32.ofInt 3 (by intlit)) - let y ← get_max_fwd (U32.ofInt 10 (by intlit)) (U32.ofInt 11 (by intlit)) + let x ← get_max (U32.ofInt 4 (by intlit)) (U32.ofInt 3 (by intlit)) + let y ← get_max (U32.ofInt 10 (by intlit)) (U32.ofInt 11 (by intlit)) let z ← x + y if not (z = (U32.ofInt 15 (by intlit))) then Result.fail Error.panic else Result.ret () /- Unit test for [no_nested_borrows::test3] -/ -#assert (test3_fwd == .ret ()) +#assert (test3 == .ret ()) -/- [no_nested_borrows::test_neg1] -/ -def test_neg1_fwd : Result Unit := +/- [no_nested_borrows::test_neg1]: forward function -/ +def test_neg1 : Result Unit := do let y ← - (I32.ofInt 3 (by intlit)) if not (y = (I32.ofInt (-(3:Int)) (by intlit))) @@ -100,19 +100,19 @@ def test_neg1_fwd : Result Unit := else Result.ret () /- Unit test for [no_nested_borrows::test_neg1] -/ -#assert (test_neg1_fwd == .ret ()) +#assert (test_neg1 == .ret ()) -/- [no_nested_borrows::refs_test1] -/ -def refs_test1_fwd : Result Unit := +/- [no_nested_borrows::refs_test1]: forward function -/ +def refs_test1 : Result Unit := if not ((I32.ofInt 1 (by intlit)) = (I32.ofInt 1 (by intlit))) then Result.fail Error.panic else Result.ret () /- Unit test for [no_nested_borrows::refs_test1] -/ -#assert (refs_test1_fwd == .ret ()) +#assert (refs_test1 == .ret ()) -/- [no_nested_borrows::refs_test2] -/ -def refs_test2_fwd : Result Unit := +/- [no_nested_borrows::refs_test2]: forward function -/ +def refs_test2 : Result Unit := if not ((I32.ofInt 2 (by intlit)) = (I32.ofInt 2 (by intlit))) then Result.fail Error.panic else @@ -127,17 +127,17 @@ def refs_test2_fwd : Result Unit := else Result.ret () /- Unit test for [no_nested_borrows::refs_test2] -/ -#assert (refs_test2_fwd == .ret ()) +#assert (refs_test2 == .ret ()) -/- [no_nested_borrows::test_list1] -/ -def test_list1_fwd : Result Unit := +/- [no_nested_borrows::test_list1]: forward function -/ +def test_list1 : Result Unit := Result.ret () /- Unit test for [no_nested_borrows::test_list1] -/ -#assert (test_list1_fwd == .ret ()) +#assert (test_list1 == .ret ()) -/- [no_nested_borrows::test_box1] -/ -def test_box1_fwd : Result Unit := +/- [no_nested_borrows::test_box1]: forward function -/ +def test_box1 : Result Unit := let b := (I32.ofInt 1 (by intlit)) let x := b if not (x = (I32.ofInt 1 (by intlit))) @@ -145,90 +145,90 @@ def test_box1_fwd : Result Unit := else Result.ret () /- Unit test for [no_nested_borrows::test_box1] -/ -#assert (test_box1_fwd == .ret ()) +#assert (test_box1 == .ret ()) -/- [no_nested_borrows::copy_int] -/ -def copy_int_fwd (x : I32) : Result I32 := +/- [no_nested_borrows::copy_int]: forward function -/ +def copy_int (x : I32) : Result I32 := Result.ret x -/- [no_nested_borrows::test_unreachable] -/ -def test_unreachable_fwd (b : Bool) : Result Unit := +/- [no_nested_borrows::test_unreachable]: forward function -/ +def test_unreachable (b : Bool) : Result Unit := if b then Result.fail Error.panic else Result.ret () -/- [no_nested_borrows::test_panic] -/ -def test_panic_fwd (b : Bool) : Result Unit := +/- [no_nested_borrows::test_panic]: forward function -/ +def test_panic (b : Bool) : Result Unit := if b then Result.fail Error.panic else Result.ret () -/- [no_nested_borrows::test_copy_int] -/ -def test_copy_int_fwd : Result Unit := +/- [no_nested_borrows::test_copy_int]: forward function -/ +def test_copy_int : Result Unit := do - let y ← copy_int_fwd (I32.ofInt 0 (by intlit)) + let y ← copy_int (I32.ofInt 0 (by intlit)) if not ((I32.ofInt 0 (by intlit)) = y) then Result.fail Error.panic else Result.ret () /- Unit test for [no_nested_borrows::test_copy_int] -/ -#assert (test_copy_int_fwd == .ret ()) +#assert (test_copy_int == .ret ()) -/- [no_nested_borrows::is_cons] -/ -def is_cons_fwd (T : Type) (l : List T) : Result Bool := +/- [no_nested_borrows::is_cons]: forward function -/ +def is_cons (T : Type) (l : List T) : Result Bool := match l with | List.Cons t l0 => Result.ret true | List.Nil => Result.ret false -/- [no_nested_borrows::test_is_cons] -/ -def test_is_cons_fwd : Result Unit := +/- [no_nested_borrows::test_is_cons]: forward function -/ +def test_is_cons : Result Unit := do let l := List.Nil - let b ← is_cons_fwd I32 (List.Cons (I32.ofInt 0 (by intlit)) l) + let b ← is_cons I32 (List.Cons (I32.ofInt 0 (by intlit)) l) if not b then Result.fail Error.panic else Result.ret () /- Unit test for [no_nested_borrows::test_is_cons] -/ -#assert (test_is_cons_fwd == .ret ()) +#assert (test_is_cons == .ret ()) -/- [no_nested_borrows::split_list] -/ -def split_list_fwd (T : Type) (l : List T) : Result (T × (List T)) := +/- [no_nested_borrows::split_list]: forward function -/ +def split_list (T : Type) (l : List T) : Result (T × (List T)) := match l with | List.Cons hd tl => Result.ret (hd, tl) | List.Nil => Result.fail Error.panic -/- [no_nested_borrows::test_split_list] -/ -def test_split_list_fwd : Result Unit := +/- [no_nested_borrows::test_split_list]: forward function -/ +def test_split_list : Result Unit := do let l := List.Nil - let p ← split_list_fwd I32 (List.Cons (I32.ofInt 0 (by intlit)) l) + let p ← split_list I32 (List.Cons (I32.ofInt 0 (by intlit)) l) let (hd, _) := p if not (hd = (I32.ofInt 0 (by intlit))) then Result.fail Error.panic else Result.ret () /- Unit test for [no_nested_borrows::test_split_list] -/ -#assert (test_split_list_fwd == .ret ()) +#assert (test_split_list == .ret ()) -/- [no_nested_borrows::choose] -/ -def choose_fwd (T : Type) (b : Bool) (x : T) (y : T) : Result T := +/- [no_nested_borrows::choose]: forward function -/ +def choose (T : Type) (b : Bool) (x : T) (y : T) : Result T := if b then Result.ret x else Result.ret y -/- [no_nested_borrows::choose] -/ +/- [no_nested_borrows::choose]: backward function 0 -/ def choose_back (T : Type) (b : Bool) (x : T) (y : T) (ret0 : T) : Result (T × T) := if b then Result.ret (ret0, y) else Result.ret (x, ret0) -/- [no_nested_borrows::choose_test] -/ -def choose_test_fwd : Result Unit := +/- [no_nested_borrows::choose_test]: forward function -/ +def choose_test : Result Unit := do let z ← - choose_fwd I32 true (I32.ofInt 0 (by intlit)) (I32.ofInt 0 (by intlit)) + choose I32 true (I32.ofInt 0 (by intlit)) (I32.ofInt 0 (by intlit)) let z0 ← z + (I32.ofInt 1 (by intlit)) if not (z0 = (I32.ofInt 1 (by intlit))) then Result.fail Error.panic @@ -245,10 +245,10 @@ def choose_test_fwd : Result Unit := else Result.ret () /- Unit test for [no_nested_borrows::choose_test] -/ -#assert (choose_test_fwd == .ret ()) +#assert (choose_test == .ret ()) -/- [no_nested_borrows::test_char] -/ -def test_char_fwd : Result Char := +/- [no_nested_borrows::test_char]: forward function -/ +def test_char : Result Char := Result.ret 'a' mutual @@ -265,40 +265,38 @@ inductive Tree (T : Type) := end -/- [no_nested_borrows::list_length] -/ -divergent def list_length_fwd (T : Type) (l : List T) : Result U32 := +/- [no_nested_borrows::list_length]: forward function -/ +divergent def list_length (T : Type) (l : List T) : Result U32 := match l with | List.Cons t l1 => do - let i ← list_length_fwd T l1 + let i ← list_length T l1 (U32.ofInt 1 (by intlit)) + i | List.Nil => Result.ret (U32.ofInt 0 (by intlit)) -/- [no_nested_borrows::list_nth_shared] -/ -divergent def list_nth_shared_fwd - (T : Type) (l : List T) (i : U32) : Result T := +/- [no_nested_borrows::list_nth_shared]: forward function -/ +divergent def list_nth_shared (T : Type) (l : List T) (i : U32) : Result T := match l with | List.Cons x tl => if i = (U32.ofInt 0 (by intlit)) then Result.ret x - else - do - let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_shared_fwd T tl i0 + else do + let i0 ← i - (U32.ofInt 1 (by intlit)) + list_nth_shared T tl i0 | List.Nil => Result.fail Error.panic -/- [no_nested_borrows::list_nth_mut] -/ -divergent def list_nth_mut_fwd (T : Type) (l : List T) (i : U32) : Result T := +/- [no_nested_borrows::list_nth_mut]: forward function -/ +divergent def list_nth_mut (T : Type) (l : List T) (i : U32) : Result T := match l with | List.Cons x tl => if i = (U32.ofInt 0 (by intlit)) then Result.ret x else do let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_mut_fwd T tl i0 + list_nth_mut T tl i0 | List.Nil => Result.fail Error.panic -/- [no_nested_borrows::list_nth_mut] -/ +/- [no_nested_borrows::list_nth_mut]: backward function 0 -/ divergent def list_nth_mut_back (T : Type) (l : List T) (i : U32) (ret0 : T) : Result (List T) := match l with @@ -312,46 +310,47 @@ divergent def list_nth_mut_back Result.ret (List.Cons x tl0) | List.Nil => Result.fail Error.panic -/- [no_nested_borrows::list_rev_aux] -/ -divergent def list_rev_aux_fwd +/- [no_nested_borrows::list_rev_aux]: forward function -/ +divergent def list_rev_aux (T : Type) (li : List T) (lo : List T) : Result (List T) := match li with - | List.Cons hd tl => list_rev_aux_fwd T tl (List.Cons hd lo) + | List.Cons hd tl => list_rev_aux T tl (List.Cons hd lo) | List.Nil => Result.ret lo -/- [no_nested_borrows::list_rev] -/ -def list_rev_fwd_back (T : Type) (l : List T) : Result (List T) := - let li := mem.replace_fwd (List T) l List.Nil - list_rev_aux_fwd T li List.Nil +/- [no_nested_borrows::list_rev]: merged forward/backward function + (there is a single backward function, and the forward function returns ()) -/ +def list_rev (T : Type) (l : List T) : Result (List T) := + let li := mem.replace (List T) l List.Nil + list_rev_aux T li List.Nil -/- [no_nested_borrows::test_list_functions] -/ -def test_list_functions_fwd : Result Unit := +/- [no_nested_borrows::test_list_functions]: forward function -/ +def test_list_functions : Result Unit := do let l := List.Nil let l0 := List.Cons (I32.ofInt 2 (by intlit)) l let l1 := List.Cons (I32.ofInt 1 (by intlit)) l0 - let i ← list_length_fwd I32 (List.Cons (I32.ofInt 0 (by intlit)) l1) + let i ← list_length I32 (List.Cons (I32.ofInt 0 (by intlit)) l1) if not (i = (U32.ofInt 3 (by intlit))) then Result.fail Error.panic else do let i0 ← - list_nth_shared_fwd I32 (List.Cons (I32.ofInt 0 (by intlit)) l1) + list_nth_shared I32 (List.Cons (I32.ofInt 0 (by intlit)) l1) (U32.ofInt 0 (by intlit)) if not (i0 = (I32.ofInt 0 (by intlit))) then Result.fail Error.panic else do let i1 ← - list_nth_shared_fwd I32 (List.Cons (I32.ofInt 0 (by intlit)) l1) + list_nth_shared I32 (List.Cons (I32.ofInt 0 (by intlit)) l1) (U32.ofInt 1 (by intlit)) if not (i1 = (I32.ofInt 1 (by intlit))) then Result.fail Error.panic else do let i2 ← - list_nth_shared_fwd I32 (List.Cons (I32.ofInt 0 (by intlit)) - l1) (U32.ofInt 2 (by intlit)) + list_nth_shared I32 (List.Cons (I32.ofInt 0 (by intlit)) l1) + (U32.ofInt 2 (by intlit)) if not (i2 = (I32.ofInt 2 (by intlit))) then Result.fail Error.panic else @@ -360,74 +359,72 @@ def test_list_functions_fwd : Result Unit := list_nth_mut_back I32 (List.Cons (I32.ofInt 0 (by intlit)) l1) (U32.ofInt 1 (by intlit)) (I32.ofInt 3 (by intlit)) - let i3 ← - list_nth_shared_fwd I32 ls (U32.ofInt 0 (by intlit)) + let i3 ← list_nth_shared I32 ls (U32.ofInt 0 (by intlit)) if not (i3 = (I32.ofInt 0 (by intlit))) then Result.fail Error.panic else do let i4 ← - list_nth_shared_fwd I32 ls (U32.ofInt 1 (by intlit)) + list_nth_shared I32 ls (U32.ofInt 1 (by intlit)) if not (i4 = (I32.ofInt 3 (by intlit))) then Result.fail Error.panic else do let i5 ← - list_nth_shared_fwd I32 ls - (U32.ofInt 2 (by intlit)) + list_nth_shared I32 ls (U32.ofInt 2 (by intlit)) if not (i5 = (I32.ofInt 2 (by intlit))) then Result.fail Error.panic else Result.ret () /- Unit test for [no_nested_borrows::test_list_functions] -/ -#assert (test_list_functions_fwd == .ret ()) +#assert (test_list_functions == .ret ()) -/- [no_nested_borrows::id_mut_pair1] -/ -def id_mut_pair1_fwd (T1 T2 : Type) (x : T1) (y : T2) : Result (T1 × T2) := +/- [no_nested_borrows::id_mut_pair1]: forward function -/ +def id_mut_pair1 (T1 T2 : Type) (x : T1) (y : T2) : Result (T1 × T2) := Result.ret (x, y) -/- [no_nested_borrows::id_mut_pair1] -/ +/- [no_nested_borrows::id_mut_pair1]: backward function 0 -/ def id_mut_pair1_back (T1 T2 : Type) (x : T1) (y : T2) (ret0 : (T1 × T2)) : Result (T1 × T2) := let (t, t0) := ret0 Result.ret (t, t0) -/- [no_nested_borrows::id_mut_pair2] -/ -def id_mut_pair2_fwd (T1 T2 : Type) (p : (T1 × T2)) : Result (T1 × T2) := +/- [no_nested_borrows::id_mut_pair2]: forward function -/ +def id_mut_pair2 (T1 T2 : Type) (p : (T1 × T2)) : Result (T1 × T2) := let (t, t0) := p Result.ret (t, t0) -/- [no_nested_borrows::id_mut_pair2] -/ +/- [no_nested_borrows::id_mut_pair2]: backward function 0 -/ def id_mut_pair2_back (T1 T2 : Type) (p : (T1 × T2)) (ret0 : (T1 × T2)) : Result (T1 × T2) := let (t, t0) := ret0 Result.ret (t, t0) -/- [no_nested_borrows::id_mut_pair3] -/ -def id_mut_pair3_fwd (T1 T2 : Type) (x : T1) (y : T2) : Result (T1 × T2) := +/- [no_nested_borrows::id_mut_pair3]: forward function -/ +def id_mut_pair3 (T1 T2 : Type) (x : T1) (y : T2) : Result (T1 × T2) := Result.ret (x, y) -/- [no_nested_borrows::id_mut_pair3] -/ +/- [no_nested_borrows::id_mut_pair3]: backward function 0 -/ def id_mut_pair3_back'a (T1 T2 : Type) (x : T1) (y : T2) (ret0 : T1) : Result T1 := Result.ret ret0 -/- [no_nested_borrows::id_mut_pair3] -/ +/- [no_nested_borrows::id_mut_pair3]: backward function 1 -/ def id_mut_pair3_back'b (T1 T2 : Type) (x : T1) (y : T2) (ret0 : T2) : Result T2 := Result.ret ret0 -/- [no_nested_borrows::id_mut_pair4] -/ -def id_mut_pair4_fwd (T1 T2 : Type) (p : (T1 × T2)) : Result (T1 × T2) := +/- [no_nested_borrows::id_mut_pair4]: forward function -/ +def id_mut_pair4 (T1 T2 : Type) (p : (T1 × T2)) : Result (T1 × T2) := let (t, t0) := p Result.ret (t, t0) -/- [no_nested_borrows::id_mut_pair4] -/ +/- [no_nested_borrows::id_mut_pair4]: backward function 0 -/ def id_mut_pair4_back'a (T1 T2 : Type) (p : (T1 × T2)) (ret0 : T1) : Result T1 := Result.ret ret0 -/- [no_nested_borrows::id_mut_pair4] -/ +/- [no_nested_borrows::id_mut_pair4]: backward function 1 -/ def id_mut_pair4_back'b (T1 T2 : Type) (p : (T1 × T2)) (ret0 : T2) : Result T2 := Result.ret ret0 @@ -436,24 +433,24 @@ def id_mut_pair4_back'b structure StructWithTuple (T1 T2 : Type) where struct_with_tuple_p : (T1 × T2) -/- [no_nested_borrows::new_tuple1] -/ -def new_tuple1_fwd : Result (StructWithTuple U32 U32) := +/- [no_nested_borrows::new_tuple1]: forward function -/ +def new_tuple1 : Result (StructWithTuple U32 U32) := Result.ret { struct_with_tuple_p := ((U32.ofInt 1 (by intlit)), (U32.ofInt 2 (by intlit))) } -/- [no_nested_borrows::new_tuple2] -/ -def new_tuple2_fwd : Result (StructWithTuple I16 I16) := +/- [no_nested_borrows::new_tuple2]: forward function -/ +def new_tuple2 : Result (StructWithTuple I16 I16) := Result.ret { struct_with_tuple_p := ((I16.ofInt 1 (by intlit)), (I16.ofInt 2 (by intlit))) } -/- [no_nested_borrows::new_tuple3] -/ -def new_tuple3_fwd : Result (StructWithTuple U64 I64) := +/- [no_nested_borrows::new_tuple3]: forward function -/ +def new_tuple3 : Result (StructWithTuple U64 I64) := Result.ret { struct_with_tuple_p := @@ -464,8 +461,8 @@ def new_tuple3_fwd : Result (StructWithTuple U64 I64) := structure StructWithPair (T1 T2 : Type) where struct_with_pair_p : Pair T1 T2 -/- [no_nested_borrows::new_pair1] -/ -def new_pair1_fwd : Result (StructWithPair U32 U32) := +/- [no_nested_borrows::new_pair1]: forward function -/ +def new_pair1 : Result (StructWithPair U32 U32) := Result.ret { struct_with_pair_p := @@ -475,68 +472,69 @@ def new_pair1_fwd : Result (StructWithPair U32 U32) := } } -/- [no_nested_borrows::test_constants] -/ -def test_constants_fwd : Result Unit := +/- [no_nested_borrows::test_constants]: forward function -/ +def test_constants : Result Unit := do - let swt ← new_tuple1_fwd + let swt ← new_tuple1 let (i, _) := swt.struct_with_tuple_p if not (i = (U32.ofInt 1 (by intlit))) then Result.fail Error.panic else do - let swt0 ← new_tuple2_fwd + let swt0 ← new_tuple2 let (i0, _) := swt0.struct_with_tuple_p if not (i0 = (I16.ofInt 1 (by intlit))) then Result.fail Error.panic else do - let swt1 ← new_tuple3_fwd + let swt1 ← new_tuple3 let (i1, _) := swt1.struct_with_tuple_p if not (i1 = (U64.ofInt 1 (by intlit))) then Result.fail Error.panic else do - let swp ← new_pair1_fwd + let swp ← new_pair1 if not (swp.struct_with_pair_p.pair_x = (U32.ofInt 1 (by intlit))) then Result.fail Error.panic else Result.ret () /- Unit test for [no_nested_borrows::test_constants] -/ -#assert (test_constants_fwd == .ret ()) +#assert (test_constants == .ret ()) -/- [no_nested_borrows::test_weird_borrows1] -/ -def test_weird_borrows1_fwd : Result Unit := +/- [no_nested_borrows::test_weird_borrows1]: forward function -/ +def test_weird_borrows1 : Result Unit := Result.ret () /- Unit test for [no_nested_borrows::test_weird_borrows1] -/ -#assert (test_weird_borrows1_fwd == .ret ()) +#assert (test_weird_borrows1 == .ret ()) -/- [no_nested_borrows::test_mem_replace] -/ -def test_mem_replace_fwd_back (px : U32) : Result U32 := - let y := mem.replace_fwd U32 px (U32.ofInt 1 (by intlit)) +/- [no_nested_borrows::test_mem_replace]: merged forward/backward function + (there is a single backward function, and the forward function returns ()) -/ +def test_mem_replace (px : U32) : Result U32 := + let y := mem.replace U32 px (U32.ofInt 1 (by intlit)) if not (y = (U32.ofInt 0 (by intlit))) then Result.fail Error.panic else Result.ret (U32.ofInt 2 (by intlit)) -/- [no_nested_borrows::test_shared_borrow_bool1] -/ -def test_shared_borrow_bool1_fwd (b : Bool) : Result U32 := +/- [no_nested_borrows::test_shared_borrow_bool1]: forward function -/ +def test_shared_borrow_bool1 (b : Bool) : Result U32 := if b then Result.ret (U32.ofInt 0 (by intlit)) else Result.ret (U32.ofInt 1 (by intlit)) -/- [no_nested_borrows::test_shared_borrow_bool2] -/ -def test_shared_borrow_bool2_fwd : Result U32 := +/- [no_nested_borrows::test_shared_borrow_bool2]: forward function -/ +def test_shared_borrow_bool2 : Result U32 := Result.ret (U32.ofInt 0 (by intlit)) -/- [no_nested_borrows::test_shared_borrow_enum1] -/ -def test_shared_borrow_enum1_fwd (l : List U32) : Result U32 := +/- [no_nested_borrows::test_shared_borrow_enum1]: forward function -/ +def test_shared_borrow_enum1 (l : List U32) : Result U32 := match l with | List.Cons i l0 => Result.ret (U32.ofInt 1 (by intlit)) | List.Nil => Result.ret (U32.ofInt 0 (by intlit)) -/- [no_nested_borrows::test_shared_borrow_enum2] -/ -def test_shared_borrow_enum2_fwd : Result U32 := +/- [no_nested_borrows::test_shared_borrow_enum2]: forward function -/ +def test_shared_borrow_enum2 : Result U32 := Result.ret (U32.ofInt 0 (by intlit)) end no_nested_borrows diff --git a/tests/lean/Paper.lean b/tests/lean/Paper.lean index 9f63b460..ade65656 100644 --- a/tests/lean/Paper.lean +++ b/tests/lean/Paper.lean @@ -4,39 +4,40 @@ import Base open Primitives namespace paper -/- [paper::ref_incr] -/ -def ref_incr_fwd_back (x : I32) : Result I32 := +/- [paper::ref_incr]: merged forward/backward function + (there is a single backward function, and the forward function returns ()) -/ +def ref_incr (x : I32) : Result I32 := x + (I32.ofInt 1 (by intlit)) -/- [paper::test_incr] -/ -def test_incr_fwd : Result Unit := +/- [paper::test_incr]: forward function -/ +def test_incr : Result Unit := do - let x ← ref_incr_fwd_back (I32.ofInt 0 (by intlit)) + let x ← ref_incr (I32.ofInt 0 (by intlit)) if not (x = (I32.ofInt 1 (by intlit))) then Result.fail Error.panic else Result.ret () /- Unit test for [paper::test_incr] -/ -#assert (test_incr_fwd == .ret ()) +#assert (test_incr == .ret ()) -/- [paper::choose] -/ -def choose_fwd (T : Type) (b : Bool) (x : T) (y : T) : Result T := +/- [paper::choose]: forward function -/ +def choose (T : Type) (b : Bool) (x : T) (y : T) : Result T := if b then Result.ret x else Result.ret y -/- [paper::choose] -/ +/- [paper::choose]: backward function 0 -/ def choose_back (T : Type) (b : Bool) (x : T) (y : T) (ret0 : T) : Result (T × T) := if b then Result.ret (ret0, y) else Result.ret (x, ret0) -/- [paper::test_choose] -/ -def test_choose_fwd : Result Unit := +/- [paper::test_choose]: forward function -/ +def test_choose : Result Unit := do let z ← - choose_fwd I32 true (I32.ofInt 0 (by intlit)) (I32.ofInt 0 (by intlit)) + choose I32 true (I32.ofInt 0 (by intlit)) (I32.ofInt 0 (by intlit)) let z0 ← z + (I32.ofInt 1 (by intlit)) if not (z0 = (I32.ofInt 1 (by intlit))) then Result.fail Error.panic @@ -53,25 +54,25 @@ def test_choose_fwd : Result Unit := else Result.ret () /- Unit test for [paper::test_choose] -/ -#assert (test_choose_fwd == .ret ()) +#assert (test_choose == .ret ()) /- [paper::List] -/ inductive List (T : Type) := | Cons : T → List T → List T | Nil : List T -/- [paper::list_nth_mut] -/ -divergent def list_nth_mut_fwd (T : Type) (l : List T) (i : U32) : Result T := +/- [paper::list_nth_mut]: forward function -/ +divergent def list_nth_mut (T : Type) (l : List T) (i : U32) : Result T := match l with | List.Cons x tl => if i = (U32.ofInt 0 (by intlit)) then Result.ret x else do let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_mut_fwd T tl i0 + list_nth_mut T tl i0 | List.Nil => Result.fail Error.panic -/- [paper::list_nth_mut] -/ +/- [paper::list_nth_mut]: backward function 0 -/ divergent def list_nth_mut_back (T : Type) (l : List T) (i : U32) (ret0 : T) : Result (List T) := match l with @@ -85,40 +86,40 @@ divergent def list_nth_mut_back Result.ret (List.Cons x tl0) | List.Nil => Result.fail Error.panic -/- [paper::sum] -/ -divergent def sum_fwd (l : List I32) : Result I32 := +/- [paper::sum]: forward function -/ +divergent def sum (l : List I32) : Result I32 := match l with | List.Cons x tl => do - let i ← sum_fwd tl + let i ← sum tl x + i | List.Nil => Result.ret (I32.ofInt 0 (by intlit)) -/- [paper::test_nth] -/ -def test_nth_fwd : Result Unit := +/- [paper::test_nth]: forward function -/ +def test_nth : Result Unit := do let l := List.Nil let l0 := List.Cons (I32.ofInt 3 (by intlit)) l let l1 := List.Cons (I32.ofInt 2 (by intlit)) l0 let x ← - list_nth_mut_fwd I32 (List.Cons (I32.ofInt 1 (by intlit)) l1) + list_nth_mut I32 (List.Cons (I32.ofInt 1 (by intlit)) l1) (U32.ofInt 2 (by intlit)) let x0 ← x + (I32.ofInt 1 (by intlit)) let l2 ← list_nth_mut_back I32 (List.Cons (I32.ofInt 1 (by intlit)) l1) (U32.ofInt 2 (by intlit)) x0 - let i ← sum_fwd l2 + let i ← sum l2 if not (i = (I32.ofInt 7 (by intlit))) then Result.fail Error.panic else Result.ret () /- Unit test for [paper::test_nth] -/ -#assert (test_nth_fwd == .ret ()) +#assert (test_nth == .ret ()) -/- [paper::call_choose] -/ -def call_choose_fwd (p : (U32 × U32)) : Result U32 := +/- [paper::call_choose]: forward function -/ +def call_choose (p : (U32 × U32)) : Result U32 := do let (px, py) := p - let pz ← choose_fwd U32 true px py + let pz ← choose U32 true px py let pz0 ← pz + (U32.ofInt 1 (by intlit)) let (px0, _) ← choose_back U32 true px py pz0 Result.ret px0 diff --git a/tests/lean/PoloniusList.lean b/tests/lean/PoloniusList.lean index 1d7ec99b..1453c275 100644 --- a/tests/lean/PoloniusList.lean +++ b/tests/lean/PoloniusList.lean @@ -9,17 +9,16 @@ inductive List (T : Type) := | Cons : T → List T → List T | Nil : List T -/- [polonius_list::get_list_at_x] -/ -divergent def get_list_at_x_fwd - (ls : List U32) (x : U32) : Result (List U32) := +/- [polonius_list::get_list_at_x]: forward function -/ +divergent def get_list_at_x (ls : List U32) (x : U32) : Result (List U32) := match ls with | List.Cons hd tl => if hd = x then Result.ret (List.Cons hd tl) - else get_list_at_x_fwd tl x + else get_list_at_x tl x | List.Nil => Result.ret List.Nil -/- [polonius_list::get_list_at_x] -/ +/- [polonius_list::get_list_at_x]: backward function 0 -/ divergent def get_list_at_x_back (ls : List U32) (x : U32) (ret0 : List U32) : Result (List U32) := match ls with -- cgit v1.2.3 From 36c3348bacf7127d3736f9aac16a430a30424020 Mon Sep 17 00:00:00 2001 From: Son Ho Date: Thu, 6 Jul 2023 13:46:26 +0200 Subject: Use short names for the structure fields in Lean --- tests/lean/BetreeMain/Funs.lean | 106 +++++++++++++--------------------- tests/lean/BetreeMain/Types.lean | 16 +++--- tests/lean/Constants.lean | 15 +++-- tests/lean/Hashmap/Funs.lean | 90 +++++++++++++---------------- tests/lean/Hashmap/Types.lean | 8 +-- tests/lean/HashmapMain/Funs.lean | 116 +++++++++++++------------------------- tests/lean/HashmapMain/Types.lean | 8 +-- tests/lean/NoNestedBorrows.lean | 43 ++++---------- 8 files changed, 154 insertions(+), 248 deletions(-) (limited to 'tests/lean') diff --git a/tests/lean/BetreeMain/Funs.lean b/tests/lean/BetreeMain/Funs.lean index 41e4349e..b0939155 100644 --- a/tests/lean/BetreeMain/Funs.lean +++ b/tests/lean/BetreeMain/Funs.lean @@ -48,23 +48,20 @@ def betree.fresh_node_id_back (counter : U64) : Result U64 := /- [betree_main::betree::NodeIdCounter::{0}::new]: forward function -/ def betree.NodeIdCounter.new : Result betree.NodeIdCounter := - Result.ret - { betree_node_id_counter_next_node_id := (U64.ofInt 0 (by intlit)) } + Result.ret { next_node_id := (U64.ofInt 0 (by intlit)) } /- [betree_main::betree::NodeIdCounter::{0}::fresh_id]: forward function -/ def betree.NodeIdCounter.fresh_id (self : betree.NodeIdCounter) : Result U64 := do - let _ ← self.betree_node_id_counter_next_node_id + - (U64.ofInt 1 (by intlit)) - Result.ret self.betree_node_id_counter_next_node_id + let _ ← self.next_node_id + (U64.ofInt 1 (by intlit)) + Result.ret self.next_node_id /- [betree_main::betree::NodeIdCounter::{0}::fresh_id]: backward function 0 -/ def betree.NodeIdCounter.fresh_id_back (self : betree.NodeIdCounter) : Result betree.NodeIdCounter := do - let i ← self.betree_node_id_counter_next_node_id + - (U64.ofInt 1 (by intlit)) - Result.ret { betree_node_id_counter_next_node_id := i } + let i ← self.next_node_id + (U64.ofInt 1 (by intlit)) + Result.ret { next_node_id := i } /- [core::num::u64::{10}::MAX] -/ def core_num_u64_max_body : Result U64 := @@ -181,8 +178,7 @@ def betree.Leaf.split Result (State × betree.Internal) := do - let p ← - betree.List.split_at (U64 × U64) content params.betree_params_split_size + let p ← betree.List.split_at (U64 × U64) content params.split_size let (content0, content1) := p let p0 ← betree.List.hd (U64 × U64) content1 let (pivot, _) := p0 @@ -191,17 +187,9 @@ def betree.Leaf.split let id1 ← betree.NodeIdCounter.fresh_id node_id_cnt0 let (st0, _) ← betree.store_leaf_node id0 content0 st let (st1, _) ← betree.store_leaf_node id1 content1 st0 - let n := betree.Node.Leaf - { - betree_leaf_id := id0, - betree_leaf_size := params.betree_params_split_size - } - let n0 := betree.Node.Leaf - { - betree_leaf_id := id1, - betree_leaf_size := params.betree_params_split_size - } - Result.ret (st1, betree.Internal.mk self.betree_leaf_id pivot n n0) + let n := betree.Node.Leaf { id := id0, size := params.split_size } + let n0 := betree.Node.Leaf { id := id1, size := params.split_size } + Result.ret (st1, betree.Internal.mk self.id pivot n n0) /- [betree_main::betree::Leaf::{3}::split]: backward function 2 -/ def betree.Leaf.split_back @@ -210,8 +198,7 @@ def betree.Leaf.split_back Result betree.NodeIdCounter := do - let p ← - betree.List.split_at (U64 × U64) content params.betree_params_split_size + let p ← betree.List.split_at (U64 × U64) content params.split_size let (content0, content1) := p let _ ← betree.List.hd (U64 × U64) content1 let id0 ← betree.NodeIdCounter.fresh_id node_id_cnt @@ -392,7 +379,7 @@ mutual divergent def betree.Node.lookup Result.ret (st1, opt) | betree.Node.Leaf node => do - let (st0, bindings) ← betree.load_leaf_node node.betree_leaf_id st + let (st0, bindings) ← betree.load_leaf_node node.id st let opt ← betree.Node.lookup_in_bindings key bindings Result.ret (st0, opt) @@ -463,7 +450,7 @@ divergent def betree.Node.lookup_back Result.ret (betree.Node.Internal node0) | betree.Node.Leaf node => do - let (_, bindings) ← betree.load_leaf_node node.betree_leaf_id st + let (_, bindings) ← betree.load_leaf_node node.id st let _ ← betree.Node.lookup_in_bindings key bindings Result.ret (betree.Node.Leaf node) @@ -732,7 +719,7 @@ mutual divergent def betree.Node.apply_messages let (st0, content) ← betree.load_internal_node i st let content0 ← betree.Node.apply_messages_to_internal content msgs let num_msgs ← betree.List.len (U64 × betree.Message) content0 - if num_msgs >= params.betree_params_min_flush_size + if num_msgs >= params.min_flush_size then do let (st1, content1) ← @@ -750,22 +737,20 @@ mutual divergent def betree.Node.apply_messages Result.ret (st1, ()) | betree.Node.Leaf node => do - let (st0, content) ← betree.load_leaf_node node.betree_leaf_id st + let (st0, content) ← betree.load_leaf_node node.id st let content0 ← betree.Node.apply_messages_to_leaf content msgs let len ← betree.List.len (U64 × U64) content0 - let i ← (U64.ofInt 2 (by intlit)) * params.betree_params_split_size + let i ← (U64.ofInt 2 (by intlit)) * params.split_size if len >= i then do let (st1, _) ← betree.Leaf.split node content0 params node_id_cnt st0 - let (st2, _) ← - betree.store_leaf_node node.betree_leaf_id betree.List.Nil st1 + let (st2, _) ← betree.store_leaf_node node.id betree.List.Nil st1 Result.ret (st2, ()) else do - let (st1, _) ← - betree.store_leaf_node node.betree_leaf_id content0 st0 + let (st1, _) ← betree.store_leaf_node node.id content0 st0 Result.ret (st1, ()) /- [betree_main::betree::Node::{5}::apply_messages]: backward function 0 -/ @@ -782,7 +767,7 @@ divergent def betree.Node.apply_messages_back let (st0, content) ← betree.load_internal_node i st let content0 ← betree.Node.apply_messages_to_internal content msgs let num_msgs ← betree.List.len (U64 × betree.Message) content0 - if num_msgs >= params.betree_params_min_flush_size + if num_msgs >= params.min_flush_size then do let (st1, content1) ← @@ -802,25 +787,23 @@ divergent def betree.Node.apply_messages_back node_id_cnt) | betree.Node.Leaf node => do - let (st0, content) ← betree.load_leaf_node node.betree_leaf_id st + let (st0, content) ← betree.load_leaf_node node.id st let content0 ← betree.Node.apply_messages_to_leaf content msgs let len ← betree.List.len (U64 × U64) content0 - let i ← (U64.ofInt 2 (by intlit)) * params.betree_params_split_size + let i ← (U64.ofInt 2 (by intlit)) * params.split_size if len >= i then do let (st1, new_node) ← betree.Leaf.split node content0 params node_id_cnt st0 - let _ ← - betree.store_leaf_node node.betree_leaf_id betree.List.Nil st1 + let _ ← betree.store_leaf_node node.id betree.List.Nil st1 let node_id_cnt0 ← betree.Leaf.split_back node content0 params node_id_cnt st0 Result.ret (betree.Node.Internal new_node, node_id_cnt0) else do - let _ ← betree.store_leaf_node node.betree_leaf_id content0 st0 - Result.ret (betree.Node.Leaf { node with betree_leaf_size := len }, - node_id_cnt) + let _ ← betree.store_leaf_node node.id content0 st0 + Result.ret (betree.Node.Leaf { node with size := len }, node_id_cnt) /- [betree_main::betree::Internal::{4}::flush]: forward function -/ divergent def betree.Internal.flush @@ -834,7 +817,7 @@ divergent def betree.Internal.flush let p ← betree.List.partition_at_pivot betree.Message content i let (msgs_left, msgs_right) := p let len_left ← betree.List.len (U64 × betree.Message) msgs_left - if len_left >= params.betree_params_min_flush_size + if len_left >= params.min_flush_size then do let (st0, _) ← @@ -842,7 +825,7 @@ divergent def betree.Internal.flush let (_, node_id_cnt0) ← betree.Node.apply_messages_back n params node_id_cnt msgs_left st let len_right ← betree.List.len (U64 × betree.Message) msgs_right - if len_right >= params.betree_params_min_flush_size + if len_right >= params.min_flush_size then do let (st1, _) ← @@ -872,7 +855,7 @@ divergent def betree.Internal.flush_back let p ← betree.List.partition_at_pivot betree.Message content i0 let (msgs_left, msgs_right) := p let len_left ← betree.List.len (U64 × betree.Message) msgs_left - if len_left >= params.betree_params_min_flush_size + if len_left >= params.min_flush_size then do let (st0, _) ← @@ -880,7 +863,7 @@ divergent def betree.Internal.flush_back let (n1, node_id_cnt0) ← betree.Node.apply_messages_back n params node_id_cnt msgs_left st let len_right ← betree.List.len (U64 × betree.Message) msgs_right - if len_right >= params.betree_params_min_flush_size + if len_right >= params.min_flush_size then do let (n2, node_id_cnt1) ← @@ -936,18 +919,11 @@ def betree.BeTree.new let node_id_cnt0 ← betree.NodeIdCounter.fresh_id_back node_id_cnt Result.ret (st0, { - betree_be_tree_params := - { - betree_params_min_flush_size := min_flush_size, - betree_params_split_size := split_size - }, - betree_be_tree_node_id_cnt := node_id_cnt0, - betree_be_tree_root := - (betree.Node.Leaf - { - betree_leaf_id := id, - betree_leaf_size := (U64.ofInt 0 (by intlit)) - }) + params := + { min_flush_size := min_flush_size, split_size := split_size }, + node_id_cnt := node_id_cnt0, + root := + (betree.Node.Leaf { id := id, size := (U64.ofInt 0 (by intlit)) }) }) /- [betree_main::betree::BeTree::{6}::apply]: forward function -/ @@ -957,11 +933,9 @@ def betree.BeTree.apply := do let (st0, _) ← - betree.Node.apply self.betree_be_tree_root self.betree_be_tree_params - self.betree_be_tree_node_id_cnt key msg st + betree.Node.apply self.root self.params self.node_id_cnt key msg st let _ ← - betree.Node.apply_back self.betree_be_tree_root - self.betree_be_tree_params self.betree_be_tree_node_id_cnt key msg st + betree.Node.apply_back self.root self.params self.node_id_cnt key msg st Result.ret (st0, ()) /- [betree_main::betree::BeTree::{6}::apply]: backward function 0 -/ @@ -971,10 +945,8 @@ def betree.BeTree.apply_back := do let (n, nic) ← - betree.Node.apply_back self.betree_be_tree_root - self.betree_be_tree_params self.betree_be_tree_node_id_cnt key msg st - Result.ret - { self with betree_be_tree_node_id_cnt := nic, betree_be_tree_root := n } + betree.Node.apply_back self.root self.params self.node_id_cnt key msg st + Result.ret { self with node_id_cnt := nic, root := n } /- [betree_main::betree::BeTree::{6}::insert]: forward function -/ def betree.BeTree.insert @@ -1033,14 +1005,14 @@ def betree.BeTree.lookup (self : betree.BeTree) (key : U64) (st : State) : Result (State × (Option U64)) := - betree.Node.lookup self.betree_be_tree_root key st + betree.Node.lookup self.root key st /- [betree_main::betree::BeTree::{6}::lookup]: backward function 0 -/ def betree.BeTree.lookup_back (self : betree.BeTree) (key : U64) (st : State) : Result betree.BeTree := do - let n ← betree.Node.lookup_back self.betree_be_tree_root key st - Result.ret { self with betree_be_tree_root := n } + let n ← betree.Node.lookup_back self.root key st + Result.ret { self with root := n } /- [betree_main::main]: forward function -/ def main : Result Unit := diff --git a/tests/lean/BetreeMain/Types.lean b/tests/lean/BetreeMain/Types.lean index afbdac9a..783ade64 100644 --- a/tests/lean/BetreeMain/Types.lean +++ b/tests/lean/BetreeMain/Types.lean @@ -22,8 +22,8 @@ inductive betree.Message := /- [betree_main::betree::Leaf] -/ structure betree.Leaf where - betree_leaf_id : U64 - betree_leaf_size : U64 + id : U64 + size : U64 mutual @@ -40,18 +40,18 @@ end /- [betree_main::betree::Params] -/ structure betree.Params where - betree_params_min_flush_size : U64 - betree_params_split_size : U64 + min_flush_size : U64 + split_size : U64 /- [betree_main::betree::NodeIdCounter] -/ structure betree.NodeIdCounter where - betree_node_id_counter_next_node_id : U64 + next_node_id : U64 /- [betree_main::betree::BeTree] -/ structure betree.BeTree where - betree_be_tree_params : betree.Params - betree_be_tree_node_id_cnt : betree.NodeIdCounter - betree_be_tree_root : betree.Node + params : betree.Params + node_id_cnt : betree.NodeIdCounter + root : betree.Node /- The state type used in the state-error monad -/ axiom State : Type diff --git a/tests/lean/Constants.lean b/tests/lean/Constants.lean index f37c9204..8f22bfba 100644 --- a/tests/lean/Constants.lean +++ b/tests/lean/Constants.lean @@ -35,12 +35,12 @@ def mk_pair0 (x : U32) (y : U32) : Result (U32 × U32) := /- [constants::Pair] -/ structure Pair (T1 T2 : Type) where - pair_x : T1 - pair_y : T2 + x : T1 + y : T2 /- [constants::mk_pair1]: forward function -/ def mk_pair1 (x : U32) (y : U32) : Result (Pair U32 U32) := - Result.ret { pair_x := x, pair_y := y } + Result.ret { x := x, y := y } /- [constants::P0] -/ def p0_body : Result (U32 × U32) := @@ -59,17 +59,16 @@ def p2_c : (U32 × U32) := eval_global p2_body (by simp) /- [constants::P3] -/ def p3_body : Result (Pair U32 U32) := - Result.ret - { pair_x := (U32.ofInt 0 (by intlit)), pair_y := (U32.ofInt 1 (by intlit)) } + Result.ret { x := (U32.ofInt 0 (by intlit)), y := (U32.ofInt 1 (by intlit)) } def p3_c : Pair U32 U32 := eval_global p3_body (by simp) /- [constants::Wrap] -/ structure Wrap (T : Type) where - wrap_val : T + val : T /- [constants::Wrap::{0}::new]: forward function -/ def Wrap.new (T : Type) (val : T) : Result (Wrap T) := - Result.ret { wrap_val := val } + Result.ret { val := val } /- [constants::Y] -/ def y_body : Result (Wrap I32) := Wrap.new I32 (I32.ofInt 2 (by intlit)) @@ -77,7 +76,7 @@ def y_c : Wrap I32 := eval_global y_body (by simp) /- [constants::unwrap_y]: forward function -/ def unwrap_y : Result I32 := - Result.ret y_c.wrap_val + Result.ret y_c.val /- [constants::YVAL] -/ def yval_body : Result I32 := unwrap_y diff --git a/tests/lean/Hashmap/Funs.lean b/tests/lean/Hashmap/Funs.lean index 34ff1379..5e11ffdd 100644 --- a/tests/lean/Hashmap/Funs.lean +++ b/tests/lean/Hashmap/Funs.lean @@ -38,10 +38,10 @@ def HashMap.new_with_capacity let i0 ← i / max_load_divisor Result.ret { - hash_map_num_entries := (Usize.ofInt 0 (by intlit)), - hash_map_max_load_factor := (max_load_dividend, max_load_divisor), - hash_map_max_load := i0, - hash_map_slots := slots + num_entries := (Usize.ofInt 0 (by intlit)), + max_load_factor := (max_load_dividend, max_load_divisor), + max_load := i0, + slots := slots } /- [hashmap::HashMap::{0}::new]: forward function -/ @@ -66,19 +66,13 @@ divergent def HashMap.clear_loop (there is a single backward function, and the forward function returns ()) -/ def HashMap.clear (T : Type) (self : HashMap T) : Result (HashMap T) := do - let v ← - HashMap.clear_loop T self.hash_map_slots (Usize.ofInt 0 (by intlit)) + let v ← HashMap.clear_loop T self.slots (Usize.ofInt 0 (by intlit)) Result.ret - { - self - with - hash_map_num_entries := (Usize.ofInt 0 (by intlit)), - hash_map_slots := v - } + { self with num_entries := (Usize.ofInt 0 (by intlit)), slots := v } /- [hashmap::HashMap::{0}::len]: forward function -/ def HashMap.len (T : Type) (self : HashMap T) : Result Usize := - Result.ret self.hash_map_num_entries + Result.ret self.num_entries /- [hashmap::HashMap::{0}::insert_in_list]: loop 0: forward function -/ divergent def HashMap.insert_in_list_loop @@ -122,23 +116,22 @@ def HashMap.insert_no_resize := do let hash ← hash_key key - let i := Vec.len (List T) self.hash_map_slots + let i := Vec.len (List T) self.slots let hash_mod ← hash % i - let l ← Vec.index_mut (List T) self.hash_map_slots hash_mod + let l ← Vec.index_mut (List T) self.slots hash_mod let inserted ← HashMap.insert_in_list T key value l if inserted then do - let i0 ← self.hash_map_num_entries + (Usize.ofInt 1 (by intlit)) + let i0 ← self.num_entries + (Usize.ofInt 1 (by intlit)) let l0 ← HashMap.insert_in_list_back T key value l - let v ← Vec.index_mut_back (List T) self.hash_map_slots hash_mod l0 - Result.ret - { self with hash_map_num_entries := i0, hash_map_slots := v } + let v ← Vec.index_mut_back (List T) self.slots hash_mod l0 + Result.ret { self with num_entries := i0, slots := v } else do let l0 ← HashMap.insert_in_list_back T key value l - let v ← Vec.index_mut_back (List T) self.hash_map_slots hash_mod l0 - Result.ret { self with hash_map_slots := v } + let v ← Vec.index_mut_back (List T) self.slots hash_mod l0 + Result.ret { self with slots := v } /- [core::num::u32::{9}::MAX] -/ def core_num_u32_max_body : Result U32 := @@ -194,9 +187,9 @@ def HashMap.move_elements def HashMap.try_resize (T : Type) (self : HashMap T) : Result (HashMap T) := do let max_usize ← Scalar.cast .Usize core_num_u32_max_c - let capacity := Vec.len (List T) self.hash_map_slots + let capacity := Vec.len (List T) self.slots let n1 ← max_usize / (Usize.ofInt 2 (by intlit)) - let (i, i0) := self.hash_map_max_load_factor + let (i, i0) := self.max_load_factor let i1 ← n1 / i if capacity <= i1 then @@ -204,16 +197,14 @@ def HashMap.try_resize (T : Type) (self : HashMap T) : Result (HashMap T) := let i2 ← capacity * (Usize.ofInt 2 (by intlit)) let ntable ← HashMap.new_with_capacity T i2 i i0 let (ntable0, _) ← - HashMap.move_elements T ntable self.hash_map_slots - (Usize.ofInt 0 (by intlit)) + HashMap.move_elements T ntable self.slots (Usize.ofInt 0 (by intlit)) Result.ret { ntable0 with - hash_map_num_entries := self.hash_map_num_entries, - hash_map_max_load_factor := (i, i0) + num_entries := self.num_entries, max_load_factor := (i, i0) } - else Result.ret { self with hash_map_max_load_factor := (i, i0) } + else Result.ret { self with max_load_factor := (i, i0) } /- [hashmap::HashMap::{0}::insert]: merged forward/backward function (there is a single backward function, and the forward function returns ()) -/ @@ -224,7 +215,7 @@ def HashMap.insert do let self0 ← HashMap.insert_no_resize T self key value let i ← HashMap.len T self0 - if i > self0.hash_map_max_load + if i > self0.max_load then HashMap.try_resize T self0 else Result.ret self0 @@ -248,9 +239,9 @@ def HashMap.contains_key (T : Type) (self : HashMap T) (key : Usize) : Result Bool := do let hash ← hash_key key - let i := Vec.len (List T) self.hash_map_slots + let i := Vec.len (List T) self.slots let hash_mod ← hash % i - let l ← Vec.index (List T) self.hash_map_slots hash_mod + let l ← Vec.index (List T) self.slots hash_mod HashMap.contains_key_in_list T key l /- [hashmap::HashMap::{0}::get_in_list]: loop 0: forward function -/ @@ -271,9 +262,9 @@ def HashMap.get_in_list (T : Type) (key : Usize) (ls : List T) : Result T := def HashMap.get (T : Type) (self : HashMap T) (key : Usize) : Result T := do let hash ← hash_key key - let i := Vec.len (List T) self.hash_map_slots + let i := Vec.len (List T) self.slots let hash_mod ← hash % i - let l ← Vec.index (List T) self.hash_map_slots hash_mod + let l ← Vec.index (List T) self.slots hash_mod HashMap.get_in_list T key l /- [hashmap::HashMap::{0}::get_mut_in_list]: loop 0: forward function -/ @@ -313,9 +304,9 @@ def HashMap.get_mut_in_list_back def HashMap.get_mut (T : Type) (self : HashMap T) (key : Usize) : Result T := do let hash ← hash_key key - let i := Vec.len (List T) self.hash_map_slots + let i := Vec.len (List T) self.slots let hash_mod ← hash % i - let l ← Vec.index_mut (List T) self.hash_map_slots hash_mod + let l ← Vec.index_mut (List T) self.slots hash_mod HashMap.get_mut_in_list T l key /- [hashmap::HashMap::{0}::get_mut]: backward function 0 -/ @@ -325,12 +316,12 @@ def HashMap.get_mut_back := do let hash ← hash_key key - let i := Vec.len (List T) self.hash_map_slots + let i := Vec.len (List T) self.slots let hash_mod ← hash % i - let l ← Vec.index_mut (List T) self.hash_map_slots hash_mod + let l ← Vec.index_mut (List T) self.slots hash_mod let l0 ← HashMap.get_mut_in_list_back T l key ret0 - let v ← Vec.index_mut_back (List T) self.hash_map_slots hash_mod l0 - Result.ret { self with hash_map_slots := v } + let v ← Vec.index_mut_back (List T) self.slots hash_mod l0 + Result.ret { self with slots := v } /- [hashmap::HashMap::{0}::remove_from_list]: loop 0: forward function -/ divergent def HashMap.remove_from_list_loop @@ -378,15 +369,15 @@ def HashMap.remove (T : Type) (self : HashMap T) (key : Usize) : Result (Option T) := do let hash ← hash_key key - let i := Vec.len (List T) self.hash_map_slots + let i := Vec.len (List T) self.slots let hash_mod ← hash % i - let l ← Vec.index_mut (List T) self.hash_map_slots hash_mod + let l ← Vec.index_mut (List T) self.slots hash_mod let x ← HashMap.remove_from_list T key l match x with | Option.none => Result.ret Option.none | Option.some x0 => do - let _ ← self.hash_map_num_entries - (Usize.ofInt 1 (by intlit)) + let _ ← self.num_entries - (Usize.ofInt 1 (by intlit)) Result.ret (Option.some x0) /- [hashmap::HashMap::{0}::remove]: backward function 0 -/ @@ -394,23 +385,22 @@ def HashMap.remove_back (T : Type) (self : HashMap T) (key : Usize) : Result (HashMap T) := do let hash ← hash_key key - let i := Vec.len (List T) self.hash_map_slots + let i := Vec.len (List T) self.slots let hash_mod ← hash % i - let l ← Vec.index_mut (List T) self.hash_map_slots hash_mod + let l ← Vec.index_mut (List T) self.slots hash_mod let x ← HashMap.remove_from_list T key l match x with | Option.none => do let l0 ← HashMap.remove_from_list_back T key l - let v ← Vec.index_mut_back (List T) self.hash_map_slots hash_mod l0 - Result.ret { self with hash_map_slots := v } + let v ← Vec.index_mut_back (List T) self.slots hash_mod l0 + Result.ret { self with slots := v } | Option.some x0 => do - let i0 ← self.hash_map_num_entries - (Usize.ofInt 1 (by intlit)) + let i0 ← self.num_entries - (Usize.ofInt 1 (by intlit)) let l0 ← HashMap.remove_from_list_back T key l - let v ← Vec.index_mut_back (List T) self.hash_map_slots hash_mod l0 - Result.ret - { self with hash_map_num_entries := i0, hash_map_slots := v } + let v ← Vec.index_mut_back (List T) self.slots hash_mod l0 + Result.ret { self with num_entries := i0, slots := v } /- [hashmap::test1]: forward function -/ def test1 : Result Unit := diff --git a/tests/lean/Hashmap/Types.lean b/tests/lean/Hashmap/Types.lean index 7530f3b9..6606cf9e 100644 --- a/tests/lean/Hashmap/Types.lean +++ b/tests/lean/Hashmap/Types.lean @@ -11,9 +11,9 @@ inductive List (T : Type) := /- [hashmap::HashMap] -/ structure HashMap (T : Type) where - hash_map_num_entries : Usize - hash_map_max_load_factor : (Usize × Usize) - hash_map_max_load : Usize - hash_map_slots : Vec (List T) + num_entries : Usize + max_load_factor : (Usize × Usize) + max_load : Usize + slots : Vec (List T) end hashmap diff --git a/tests/lean/HashmapMain/Funs.lean b/tests/lean/HashmapMain/Funs.lean index b1afcd44..e82cc9b2 100644 --- a/tests/lean/HashmapMain/Funs.lean +++ b/tests/lean/HashmapMain/Funs.lean @@ -43,11 +43,10 @@ def hashmap.HashMap.new_with_capacity let i0 ← i / max_load_divisor Result.ret { - hashmap_hash_map_num_entries := (Usize.ofInt 0 (by intlit)), - hashmap_hash_map_max_load_factor := - (max_load_dividend, max_load_divisor), - hashmap_hash_map_max_load := i0, - hashmap_hash_map_slots := slots + num_entries := (Usize.ofInt 0 (by intlit)), + max_load_factor := (max_load_dividend, max_load_divisor), + max_load := i0, + slots := slots } /- [hashmap_main::hashmap::HashMap::{0}::new]: forward function -/ @@ -77,19 +76,13 @@ def hashmap.HashMap.clear (T : Type) (self : hashmap.HashMap T) : Result (hashmap.HashMap T) := do let v ← - hashmap.HashMap.clear_loop T self.hashmap_hash_map_slots - (Usize.ofInt 0 (by intlit)) + hashmap.HashMap.clear_loop T self.slots (Usize.ofInt 0 (by intlit)) Result.ret - { - self - with - hashmap_hash_map_num_entries := (Usize.ofInt 0 (by intlit)), - hashmap_hash_map_slots := v - } + { self with num_entries := (Usize.ofInt 0 (by intlit)), slots := v } /- [hashmap_main::hashmap::HashMap::{0}::len]: forward function -/ def hashmap.HashMap.len (T : Type) (self : hashmap.HashMap T) : Result Usize := - Result.ret self.hashmap_hash_map_num_entries + Result.ret self.num_entries /- [hashmap_main::hashmap::HashMap::{0}::insert_in_list]: loop 0: forward function -/ divergent def hashmap.HashMap.insert_in_list_loop @@ -138,33 +131,22 @@ def hashmap.HashMap.insert_no_resize := do let hash ← hashmap.hash_key key - let i := Vec.len (hashmap.List T) self.hashmap_hash_map_slots + let i := Vec.len (hashmap.List T) self.slots let hash_mod ← hash % i - let l ← - Vec.index_mut (hashmap.List T) self.hashmap_hash_map_slots hash_mod + let l ← Vec.index_mut (hashmap.List T) self.slots hash_mod let inserted ← hashmap.HashMap.insert_in_list T key value l if inserted then do - let i0 ← self.hashmap_hash_map_num_entries + - (Usize.ofInt 1 (by intlit)) + let i0 ← self.num_entries + (Usize.ofInt 1 (by intlit)) let l0 ← hashmap.HashMap.insert_in_list_back T key value l - let v ← - Vec.index_mut_back (hashmap.List T) self.hashmap_hash_map_slots - hash_mod l0 - Result.ret - { - self - with - hashmap_hash_map_num_entries := i0, hashmap_hash_map_slots := v - } + let v ← Vec.index_mut_back (hashmap.List T) self.slots hash_mod l0 + Result.ret { self with num_entries := i0, slots := v } else do let l0 ← hashmap.HashMap.insert_in_list_back T key value l - let v ← - Vec.index_mut_back (hashmap.List T) self.hashmap_hash_map_slots - hash_mod l0 - Result.ret { self with hashmap_hash_map_slots := v } + let v ← Vec.index_mut_back (hashmap.List T) self.slots hash_mod l0 + Result.ret { self with slots := v } /- [core::num::u32::{9}::MAX] -/ def core_num_u32_max_body : Result U32 := @@ -227,9 +209,9 @@ def hashmap.HashMap.try_resize (T : Type) (self : hashmap.HashMap T) : Result (hashmap.HashMap T) := do let max_usize ← Scalar.cast .Usize core_num_u32_max_c - let capacity := Vec.len (hashmap.List T) self.hashmap_hash_map_slots + let capacity := Vec.len (hashmap.List T) self.slots let n1 ← max_usize / (Usize.ofInt 2 (by intlit)) - let (i, i0) := self.hashmap_hash_map_max_load_factor + let (i, i0) := self.max_load_factor let i1 ← n1 / i if capacity <= i1 then @@ -237,16 +219,15 @@ def hashmap.HashMap.try_resize let i2 ← capacity * (Usize.ofInt 2 (by intlit)) let ntable ← hashmap.HashMap.new_with_capacity T i2 i i0 let (ntable0, _) ← - hashmap.HashMap.move_elements T ntable self.hashmap_hash_map_slots + hashmap.HashMap.move_elements T ntable self.slots (Usize.ofInt 0 (by intlit)) Result.ret { ntable0 with - hashmap_hash_map_num_entries := self.hashmap_hash_map_num_entries, - hashmap_hash_map_max_load_factor := (i, i0) + num_entries := self.num_entries, max_load_factor := (i, i0) } - else Result.ret { self with hashmap_hash_map_max_load_factor := (i, i0) } + else Result.ret { self with max_load_factor := (i, i0) } /- [hashmap_main::hashmap::HashMap::{0}::insert]: merged forward/backward function (there is a single backward function, and the forward function returns ()) -/ @@ -257,7 +238,7 @@ def hashmap.HashMap.insert do let self0 ← hashmap.HashMap.insert_no_resize T self key value let i ← hashmap.HashMap.len T self0 - if i > self0.hashmap_hash_map_max_load + if i > self0.max_load then hashmap.HashMap.try_resize T self0 else Result.ret self0 @@ -281,9 +262,9 @@ def hashmap.HashMap.contains_key (T : Type) (self : hashmap.HashMap T) (key : Usize) : Result Bool := do let hash ← hashmap.hash_key key - let i := Vec.len (hashmap.List T) self.hashmap_hash_map_slots + let i := Vec.len (hashmap.List T) self.slots let hash_mod ← hash % i - let l ← Vec.index (hashmap.List T) self.hashmap_hash_map_slots hash_mod + let l ← Vec.index (hashmap.List T) self.slots hash_mod hashmap.HashMap.contains_key_in_list T key l /- [hashmap_main::hashmap::HashMap::{0}::get_in_list]: loop 0: forward function -/ @@ -306,9 +287,9 @@ def hashmap.HashMap.get (T : Type) (self : hashmap.HashMap T) (key : Usize) : Result T := do let hash ← hashmap.hash_key key - let i := Vec.len (hashmap.List T) self.hashmap_hash_map_slots + let i := Vec.len (hashmap.List T) self.slots let hash_mod ← hash % i - let l ← Vec.index (hashmap.List T) self.hashmap_hash_map_slots hash_mod + let l ← Vec.index (hashmap.List T) self.slots hash_mod hashmap.HashMap.get_in_list T key l /- [hashmap_main::hashmap::HashMap::{0}::get_mut_in_list]: loop 0: forward function -/ @@ -353,10 +334,9 @@ def hashmap.HashMap.get_mut (T : Type) (self : hashmap.HashMap T) (key : Usize) : Result T := do let hash ← hashmap.hash_key key - let i := Vec.len (hashmap.List T) self.hashmap_hash_map_slots + let i := Vec.len (hashmap.List T) self.slots let hash_mod ← hash % i - let l ← - Vec.index_mut (hashmap.List T) self.hashmap_hash_map_slots hash_mod + let l ← Vec.index_mut (hashmap.List T) self.slots hash_mod hashmap.HashMap.get_mut_in_list T l key /- [hashmap_main::hashmap::HashMap::{0}::get_mut]: backward function 0 -/ @@ -366,15 +346,12 @@ def hashmap.HashMap.get_mut_back := do let hash ← hashmap.hash_key key - let i := Vec.len (hashmap.List T) self.hashmap_hash_map_slots + let i := Vec.len (hashmap.List T) self.slots let hash_mod ← hash % i - let l ← - Vec.index_mut (hashmap.List T) self.hashmap_hash_map_slots hash_mod + let l ← Vec.index_mut (hashmap.List T) self.slots hash_mod let l0 ← hashmap.HashMap.get_mut_in_list_back T l key ret0 - let v ← - Vec.index_mut_back (hashmap.List T) self.hashmap_hash_map_slots hash_mod - l0 - Result.ret { self with hashmap_hash_map_slots := v } + let v ← Vec.index_mut_back (hashmap.List T) self.slots hash_mod l0 + Result.ret { self with slots := v } /- [hashmap_main::hashmap::HashMap::{0}::remove_from_list]: loop 0: forward function -/ divergent def hashmap.HashMap.remove_from_list_loop @@ -426,17 +403,15 @@ def hashmap.HashMap.remove (T : Type) (self : hashmap.HashMap T) (key : Usize) : Result (Option T) := do let hash ← hashmap.hash_key key - let i := Vec.len (hashmap.List T) self.hashmap_hash_map_slots + let i := Vec.len (hashmap.List T) self.slots let hash_mod ← hash % i - let l ← - Vec.index_mut (hashmap.List T) self.hashmap_hash_map_slots hash_mod + let l ← Vec.index_mut (hashmap.List T) self.slots hash_mod let x ← hashmap.HashMap.remove_from_list T key l match x with | Option.none => Result.ret Option.none | Option.some x0 => do - let _ ← self.hashmap_hash_map_num_entries - - (Usize.ofInt 1 (by intlit)) + let _ ← self.num_entries - (Usize.ofInt 1 (by intlit)) Result.ret (Option.some x0) /- [hashmap_main::hashmap::HashMap::{0}::remove]: backward function 0 -/ @@ -446,33 +421,22 @@ def hashmap.HashMap.remove_back := do let hash ← hashmap.hash_key key - let i := Vec.len (hashmap.List T) self.hashmap_hash_map_slots + let i := Vec.len (hashmap.List T) self.slots let hash_mod ← hash % i - let l ← - Vec.index_mut (hashmap.List T) self.hashmap_hash_map_slots hash_mod + let l ← Vec.index_mut (hashmap.List T) self.slots hash_mod let x ← hashmap.HashMap.remove_from_list T key l match x with | Option.none => do let l0 ← hashmap.HashMap.remove_from_list_back T key l - let v ← - Vec.index_mut_back (hashmap.List T) self.hashmap_hash_map_slots - hash_mod l0 - Result.ret { self with hashmap_hash_map_slots := v } + let v ← Vec.index_mut_back (hashmap.List T) self.slots hash_mod l0 + Result.ret { self with slots := v } | Option.some x0 => do - let i0 ← self.hashmap_hash_map_num_entries - - (Usize.ofInt 1 (by intlit)) + let i0 ← self.num_entries - (Usize.ofInt 1 (by intlit)) let l0 ← hashmap.HashMap.remove_from_list_back T key l - let v ← - Vec.index_mut_back (hashmap.List T) self.hashmap_hash_map_slots - hash_mod l0 - Result.ret - { - self - with - hashmap_hash_map_num_entries := i0, hashmap_hash_map_slots := v - } + let v ← Vec.index_mut_back (hashmap.List T) self.slots hash_mod l0 + Result.ret { self with num_entries := i0, slots := v } /- [hashmap_main::hashmap::test1]: forward function -/ def hashmap.test1 : Result Unit := diff --git a/tests/lean/HashmapMain/Types.lean b/tests/lean/HashmapMain/Types.lean index ac195e3d..3b3d0d7c 100644 --- a/tests/lean/HashmapMain/Types.lean +++ b/tests/lean/HashmapMain/Types.lean @@ -11,10 +11,10 @@ inductive hashmap.List (T : Type) := /- [hashmap_main::hashmap::HashMap] -/ structure hashmap.HashMap (T : Type) where - hashmap_hash_map_num_entries : Usize - hashmap_hash_map_max_load_factor : (Usize × Usize) - hashmap_hash_map_max_load : Usize - hashmap_hash_map_slots : Vec (hashmap.List T) + num_entries : Usize + max_load_factor : (Usize × Usize) + max_load : Usize + slots : Vec (hashmap.List T) /- The state type used in the state-error monad -/ axiom State : Type diff --git a/tests/lean/NoNestedBorrows.lean b/tests/lean/NoNestedBorrows.lean index dc744a29..1c6421bb 100644 --- a/tests/lean/NoNestedBorrows.lean +++ b/tests/lean/NoNestedBorrows.lean @@ -6,8 +6,8 @@ namespace no_nested_borrows /- [no_nested_borrows::Pair] -/ structure Pair (T1 T2 : Type) where - pair_x : T1 - pair_y : T2 + x : T1 + y : T2 /- [no_nested_borrows::List] -/ inductive List (T : Type) := @@ -431,71 +431,52 @@ def id_mut_pair4_back'b /- [no_nested_borrows::StructWithTuple] -/ structure StructWithTuple (T1 T2 : Type) where - struct_with_tuple_p : (T1 × T2) + p : (T1 × T2) /- [no_nested_borrows::new_tuple1]: forward function -/ def new_tuple1 : Result (StructWithTuple U32 U32) := - Result.ret - { - struct_with_tuple_p := - ((U32.ofInt 1 (by intlit)), (U32.ofInt 2 (by intlit))) - } + Result.ret { p := ((U32.ofInt 1 (by intlit)), (U32.ofInt 2 (by intlit))) } /- [no_nested_borrows::new_tuple2]: forward function -/ def new_tuple2 : Result (StructWithTuple I16 I16) := - Result.ret - { - struct_with_tuple_p := - ((I16.ofInt 1 (by intlit)), (I16.ofInt 2 (by intlit))) - } + Result.ret { p := ((I16.ofInt 1 (by intlit)), (I16.ofInt 2 (by intlit))) } /- [no_nested_borrows::new_tuple3]: forward function -/ def new_tuple3 : Result (StructWithTuple U64 I64) := - Result.ret - { - struct_with_tuple_p := - ((U64.ofInt 1 (by intlit)), (I64.ofInt 2 (by intlit))) - } + Result.ret { p := ((U64.ofInt 1 (by intlit)), (I64.ofInt 2 (by intlit))) } /- [no_nested_borrows::StructWithPair] -/ structure StructWithPair (T1 T2 : Type) where - struct_with_pair_p : Pair T1 T2 + p : Pair T1 T2 /- [no_nested_borrows::new_pair1]: forward function -/ def new_pair1 : Result (StructWithPair U32 U32) := Result.ret - { - struct_with_pair_p := - { - pair_x := (U32.ofInt 1 (by intlit)), - pair_y := (U32.ofInt 2 (by intlit)) - } - } + { p := { x := (U32.ofInt 1 (by intlit)), y := (U32.ofInt 2 (by intlit)) } } /- [no_nested_borrows::test_constants]: forward function -/ def test_constants : Result Unit := do let swt ← new_tuple1 - let (i, _) := swt.struct_with_tuple_p + let (i, _) := swt.p if not (i = (U32.ofInt 1 (by intlit))) then Result.fail Error.panic else do let swt0 ← new_tuple2 - let (i0, _) := swt0.struct_with_tuple_p + let (i0, _) := swt0.p if not (i0 = (I16.ofInt 1 (by intlit))) then Result.fail Error.panic else do let swt1 ← new_tuple3 - let (i1, _) := swt1.struct_with_tuple_p + let (i1, _) := swt1.p if not (i1 = (U64.ofInt 1 (by intlit))) then Result.fail Error.panic else do let swp ← new_pair1 - if not (swp.struct_with_pair_p.pair_x = - (U32.ofInt 1 (by intlit))) + if not (swp.p.x = (U32.ofInt 1 (by intlit))) then Result.fail Error.panic else Result.ret () -- cgit v1.2.3 From e010c10fb9a1e2d88b52a4f6b4a0865448276013 Mon Sep 17 00:00:00 2001 From: Son Ho Date: Wed, 12 Jul 2023 15:58:38 +0200 Subject: Make the `by inlit` implicit --- tests/lean/BetreeMain/Funs.lean | 31 ++++---- tests/lean/Constants.lean | 35 ++++----- tests/lean/External/Funs.lean | 10 +-- tests/lean/Hashmap/Funs.lean | 81 +++++++++------------ tests/lean/HashmapMain/Funs.lean | 86 +++++++++------------- tests/lean/Loops/Funs.lean | 140 +++++++++++++++++------------------- tests/lean/NoNestedBorrows.lean | 152 ++++++++++++++++++--------------------- tests/lean/Paper.lean | 48 ++++++------- tests/lean/lake-manifest.json | 8 ++- 9 files changed, 269 insertions(+), 322 deletions(-) (limited to 'tests/lean') diff --git a/tests/lean/BetreeMain/Funs.lean b/tests/lean/BetreeMain/Funs.lean index b0939155..142adf08 100644 --- a/tests/lean/BetreeMain/Funs.lean +++ b/tests/lean/BetreeMain/Funs.lean @@ -39,33 +39,33 @@ def betree.store_leaf_node /- [betree_main::betree::fresh_node_id]: forward function -/ def betree.fresh_node_id (counter : U64) : Result U64 := do - let _ ← counter + (U64.ofInt 1 (by intlit)) + let _ ← counter + (U64.ofInt 1) Result.ret counter /- [betree_main::betree::fresh_node_id]: backward function 0 -/ def betree.fresh_node_id_back (counter : U64) : Result U64 := - counter + (U64.ofInt 1 (by intlit)) + counter + (U64.ofInt 1) /- [betree_main::betree::NodeIdCounter::{0}::new]: forward function -/ def betree.NodeIdCounter.new : Result betree.NodeIdCounter := - Result.ret { next_node_id := (U64.ofInt 0 (by intlit)) } + Result.ret { next_node_id := (U64.ofInt 0) } /- [betree_main::betree::NodeIdCounter::{0}::fresh_id]: forward function -/ def betree.NodeIdCounter.fresh_id (self : betree.NodeIdCounter) : Result U64 := do - let _ ← self.next_node_id + (U64.ofInt 1 (by intlit)) + let _ ← self.next_node_id + (U64.ofInt 1) Result.ret self.next_node_id /- [betree_main::betree::NodeIdCounter::{0}::fresh_id]: backward function 0 -/ def betree.NodeIdCounter.fresh_id_back (self : betree.NodeIdCounter) : Result betree.NodeIdCounter := do - let i ← self.next_node_id + (U64.ofInt 1 (by intlit)) + let i ← self.next_node_id + (U64.ofInt 1) Result.ret { next_node_id := i } /- [core::num::u64::{10}::MAX] -/ def core_num_u64_max_body : Result U64 := - Result.ret (U64.ofInt 18446744073709551615 (by intlit)) + Result.ret (U64.ofInt 18446744073709551615) def core_num_u64_max_c : U64 := eval_global core_num_u64_max_body (by simp) /- [betree_main::betree::upsert_update]: forward function -/ @@ -75,7 +75,7 @@ def betree.upsert_update | Option.none => match st with | betree.UpsertFunState.Add v => Result.ret v - | betree.UpsertFunState.Sub i => Result.ret (U64.ofInt 0 (by intlit)) + | betree.UpsertFunState.Sub i => Result.ret (U64.ofInt 0) | Option.some prev0 => match st with | betree.UpsertFunState.Add v => @@ -87,7 +87,7 @@ def betree.upsert_update | betree.UpsertFunState.Sub v => if prev0 >= v then prev0 - v - else Result.ret (U64.ofInt 0 (by intlit)) + else Result.ret (U64.ofInt 0) /- [betree_main::betree::List::{1}::len]: forward function -/ divergent def betree.List.len (T : Type) (self : betree.List T) : Result U64 := @@ -95,21 +95,21 @@ divergent def betree.List.len (T : Type) (self : betree.List T) : Result U64 := | betree.List.Cons t tl => do let i ← betree.List.len T tl - (U64.ofInt 1 (by intlit)) + i - | betree.List.Nil => Result.ret (U64.ofInt 0 (by intlit)) + (U64.ofInt 1) + i + | betree.List.Nil => Result.ret (U64.ofInt 0) /- [betree_main::betree::List::{1}::split_at]: forward function -/ divergent def betree.List.split_at (T : Type) (self : betree.List T) (n : U64) : Result ((betree.List T) × (betree.List T)) := - if n = (U64.ofInt 0 (by intlit)) + if n = (U64.ofInt 0) then Result.ret (betree.List.Nil, self) else match self with | betree.List.Cons hd tl => do - let i ← n - (U64.ofInt 1 (by intlit)) + let i ← n - (U64.ofInt 1) let p ← betree.List.split_at T tl i let (ls0, ls1) := p let l := ls0 @@ -740,7 +740,7 @@ mutual divergent def betree.Node.apply_messages let (st0, content) ← betree.load_leaf_node node.id st let content0 ← betree.Node.apply_messages_to_leaf content msgs let len ← betree.List.len (U64 × U64) content0 - let i ← (U64.ofInt 2 (by intlit)) * params.split_size + let i ← (U64.ofInt 2) * params.split_size if len >= i then do @@ -790,7 +790,7 @@ divergent def betree.Node.apply_messages_back let (st0, content) ← betree.load_leaf_node node.id st let content0 ← betree.Node.apply_messages_to_leaf content msgs let len ← betree.List.len (U64 × U64) content0 - let i ← (U64.ofInt 2 (by intlit)) * params.split_size + let i ← (U64.ofInt 2) * params.split_size if len >= i then do @@ -922,8 +922,7 @@ def betree.BeTree.new params := { min_flush_size := min_flush_size, split_size := split_size }, node_id_cnt := node_id_cnt0, - root := - (betree.Node.Leaf { id := id, size := (U64.ofInt 0 (by intlit)) }) + root := (betree.Node.Leaf { id := id, size := (U64.ofInt 0) }) }) /- [betree_main::betree::BeTree::{6}::apply]: forward function -/ diff --git a/tests/lean/Constants.lean b/tests/lean/Constants.lean index 8f22bfba..4a5a7b8f 100644 --- a/tests/lean/Constants.lean +++ b/tests/lean/Constants.lean @@ -5,12 +5,11 @@ open Primitives namespace constants /- [constants::X0] -/ -def x0_body : Result U32 := Result.ret (U32.ofInt 0 (by intlit)) +def x0_body : Result U32 := Result.ret (U32.ofInt 0) def x0_c : U32 := eval_global x0_body (by simp) /- [core::num::u32::{9}::MAX] -/ -def core_num_u32_max_body : Result U32 := - Result.ret (U32.ofInt 4294967295 (by intlit)) +def core_num_u32_max_body : Result U32 := Result.ret (U32.ofInt 4294967295) def core_num_u32_max_c : U32 := eval_global core_num_u32_max_body (by simp) /- [constants::X1] -/ @@ -18,15 +17,15 @@ def x1_body : Result U32 := Result.ret core_num_u32_max_c def x1_c : U32 := eval_global x1_body (by simp) /- [constants::X2] -/ -def x2_body : Result U32 := Result.ret (U32.ofInt 3 (by intlit)) +def x2_body : Result U32 := Result.ret (U32.ofInt 3) def x2_c : U32 := eval_global x2_body (by simp) /- [constants::incr]: forward function -/ def incr (n : U32) : Result U32 := - n + (U32.ofInt 1 (by intlit)) + n + (U32.ofInt 1) /- [constants::X3] -/ -def x3_body : Result U32 := incr (U32.ofInt 32 (by intlit)) +def x3_body : Result U32 := incr (U32.ofInt 32) def x3_c : U32 := eval_global x3_body (by simp) /- [constants::mk_pair0]: forward function -/ @@ -43,23 +42,20 @@ def mk_pair1 (x : U32) (y : U32) : Result (Pair U32 U32) := Result.ret { x := x, y := y } /- [constants::P0] -/ -def p0_body : Result (U32 × U32) := - mk_pair0 (U32.ofInt 0 (by intlit)) (U32.ofInt 1 (by intlit)) +def p0_body : Result (U32 × U32) := mk_pair0 (U32.ofInt 0) (U32.ofInt 1) def p0_c : (U32 × U32) := eval_global p0_body (by simp) /- [constants::P1] -/ -def p1_body : Result (Pair U32 U32) := - mk_pair1 (U32.ofInt 0 (by intlit)) (U32.ofInt 1 (by intlit)) +def p1_body : Result (Pair U32 U32) := mk_pair1 (U32.ofInt 0) (U32.ofInt 1) def p1_c : Pair U32 U32 := eval_global p1_body (by simp) /- [constants::P2] -/ -def p2_body : Result (U32 × U32) := - Result.ret ((U32.ofInt 0 (by intlit)), (U32.ofInt 1 (by intlit))) +def p2_body : Result (U32 × U32) := Result.ret ((U32.ofInt 0), (U32.ofInt 1)) def p2_c : (U32 × U32) := eval_global p2_body (by simp) /- [constants::P3] -/ def p3_body : Result (Pair U32 U32) := - Result.ret { x := (U32.ofInt 0 (by intlit)), y := (U32.ofInt 1 (by intlit)) } + Result.ret { x := (U32.ofInt 0), y := (U32.ofInt 1) } def p3_c : Pair U32 U32 := eval_global p3_body (by simp) /- [constants::Wrap] -/ @@ -71,7 +67,7 @@ def Wrap.new (T : Type) (val : T) : Result (Wrap T) := Result.ret { val := val } /- [constants::Y] -/ -def y_body : Result (Wrap I32) := Wrap.new I32 (I32.ofInt 2 (by intlit)) +def y_body : Result (Wrap I32) := Wrap.new I32 (I32.ofInt 2) def y_c : Wrap I32 := eval_global y_body (by simp) /- [constants::unwrap_y]: forward function -/ @@ -83,7 +79,7 @@ def yval_body : Result I32 := unwrap_y def yval_c : I32 := eval_global yval_body (by simp) /- [constants::get_z1::Z1] -/ -def get_z1_z1_body : Result I32 := Result.ret (I32.ofInt 3 (by intlit)) +def get_z1_z1_body : Result I32 := Result.ret (I32.ofInt 3) def get_z1_z1_c : I32 := eval_global get_z1_z1_body (by simp) /- [constants::get_z1]: forward function -/ @@ -95,7 +91,7 @@ def add (a : I32) (b : I32) : Result I32 := a + b /- [constants::Q1] -/ -def q1_body : Result I32 := Result.ret (I32.ofInt 5 (by intlit)) +def q1_body : Result I32 := Result.ret (I32.ofInt 5) def q1_c : I32 := eval_global q1_body (by simp) /- [constants::Q2] -/ @@ -103,7 +99,7 @@ def q2_body : Result I32 := Result.ret q1_c def q2_c : I32 := eval_global q2_body (by simp) /- [constants::Q3] -/ -def q3_body : Result I32 := add q2_c (I32.ofInt 3 (by intlit)) +def q3_body : Result I32 := add q2_c (I32.ofInt 3) def q3_c : I32 := eval_global q3_body (by simp) /- [constants::get_z2]: forward function -/ @@ -114,7 +110,7 @@ def get_z2 : Result I32 := add q1_c i0 /- [constants::S1] -/ -def s1_body : Result U32 := Result.ret (U32.ofInt 6 (by intlit)) +def s1_body : Result U32 := Result.ret (U32.ofInt 6) def s1_c : U32 := eval_global s1_body (by simp) /- [constants::S2] -/ @@ -126,8 +122,7 @@ def s3_body : Result (Pair U32 U32) := Result.ret p3_c def s3_c : Pair U32 U32 := eval_global s3_body (by simp) /- [constants::S4] -/ -def s4_body : Result (Pair U32 U32) := - mk_pair1 (U32.ofInt 7 (by intlit)) (U32.ofInt 8 (by intlit)) +def s4_body : Result (Pair U32 U32) := mk_pair1 (U32.ofInt 7) (U32.ofInt 8) def s4_c : Pair U32 U32 := eval_global s4_body (by simp) end constants diff --git a/tests/lean/External/Funs.lean b/tests/lean/External/Funs.lean index 122f94da..674aaebd 100644 --- a/tests/lean/External/Funs.lean +++ b/tests/lean/External/Funs.lean @@ -36,7 +36,7 @@ def test_new_non_zero_u32 def test_vec : Result Unit := do let v := Vec.new U32 - let _ ← Vec.push U32 v (U32.ofInt 0 (by intlit)) + let _ ← Vec.push U32 v (U32.ofInt 0) Result.ret () /- Unit test for [external::test_vec] -/ @@ -74,14 +74,14 @@ def test_custom_swap_back (x : U32) (y : U32) (st : State) (st0 : State) : Result (State × (U32 × U32)) := - custom_swap_back U32 x y st (U32.ofInt 1 (by intlit)) st0 + custom_swap_back U32 x y st (U32.ofInt 1) st0 /- [external::test_swap_non_zero]: forward function -/ def test_swap_non_zero (x : U32) (st : State) : Result (State × U32) := do - let (st0, _) ← swap U32 x (U32.ofInt 0 (by intlit)) st - let (st1, (x0, _)) ← swap_back U32 x (U32.ofInt 0 (by intlit)) st st0 - if x0 = (U32.ofInt 0 (by intlit)) + let (st0, _) ← swap U32 x (U32.ofInt 0) st + let (st1, (x0, _)) ← swap_back U32 x (U32.ofInt 0) st st0 + if x0 = (U32.ofInt 0) then Result.fail Error.panic else Result.ret (st1, x0) diff --git a/tests/lean/Hashmap/Funs.lean b/tests/lean/Hashmap/Funs.lean index 5e11ffdd..870693b5 100644 --- a/tests/lean/Hashmap/Funs.lean +++ b/tests/lean/Hashmap/Funs.lean @@ -12,11 +12,11 @@ def hash_key (k : Usize) : Result Usize := /- [hashmap::HashMap::{0}::allocate_slots]: loop 0: forward function -/ divergent def HashMap.allocate_slots_loop (T : Type) (slots : Vec (List T)) (n : Usize) : Result (Vec (List T)) := - if n > (Usize.ofInt 0 (by intlit)) + if n > (Usize.ofInt 0) then do let slots0 ← Vec.push (List T) slots List.Nil - let n0 ← n - (Usize.ofInt 1 (by intlit)) + let n0 ← n - (Usize.ofInt 1) HashMap.allocate_slots_loop T slots0 n0 else Result.ret slots @@ -38,7 +38,7 @@ def HashMap.new_with_capacity let i0 ← i / max_load_divisor Result.ret { - num_entries := (Usize.ofInt 0 (by intlit)), + num_entries := (Usize.ofInt 0), max_load_factor := (max_load_dividend, max_load_divisor), max_load := i0, slots := slots @@ -46,8 +46,7 @@ def HashMap.new_with_capacity /- [hashmap::HashMap::{0}::new]: forward function -/ def HashMap.new (T : Type) : Result (HashMap T) := - HashMap.new_with_capacity T (Usize.ofInt 32 (by intlit)) - (Usize.ofInt 4 (by intlit)) (Usize.ofInt 5 (by intlit)) + HashMap.new_with_capacity T (Usize.ofInt 32) (Usize.ofInt 4) (Usize.ofInt 5) /- [hashmap::HashMap::{0}::clear]: loop 0: merged forward/backward function (there is a single backward function, and the forward function returns ()) -/ @@ -57,7 +56,7 @@ divergent def HashMap.clear_loop if i < i0 then do - let i1 ← i + (Usize.ofInt 1 (by intlit)) + let i1 ← i + (Usize.ofInt 1) let slots0 ← Vec.index_mut_back (List T) slots i List.Nil HashMap.clear_loop T slots0 i1 else Result.ret slots @@ -66,9 +65,8 @@ divergent def HashMap.clear_loop (there is a single backward function, and the forward function returns ()) -/ def HashMap.clear (T : Type) (self : HashMap T) : Result (HashMap T) := do - let v ← HashMap.clear_loop T self.slots (Usize.ofInt 0 (by intlit)) - Result.ret - { self with num_entries := (Usize.ofInt 0 (by intlit)), slots := v } + let v ← HashMap.clear_loop T self.slots (Usize.ofInt 0) + Result.ret { self with num_entries := (Usize.ofInt 0), slots := v } /- [hashmap::HashMap::{0}::len]: forward function -/ def HashMap.len (T : Type) (self : HashMap T) : Result Usize := @@ -123,7 +121,7 @@ def HashMap.insert_no_resize if inserted then do - let i0 ← self.num_entries + (Usize.ofInt 1 (by intlit)) + let i0 ← self.num_entries + (Usize.ofInt 1) let l0 ← HashMap.insert_in_list_back T key value l let v ← Vec.index_mut_back (List T) self.slots hash_mod l0 Result.ret { self with num_entries := i0, slots := v } @@ -134,8 +132,7 @@ def HashMap.insert_no_resize Result.ret { self with slots := v } /- [core::num::u32::{9}::MAX] -/ -def core_num_u32_max_body : Result U32 := - Result.ret (U32.ofInt 4294967295 (by intlit)) +def core_num_u32_max_body : Result U32 := Result.ret (U32.ofInt 4294967295) def core_num_u32_max_c : U32 := eval_global core_num_u32_max_body (by simp) /- [hashmap::HashMap::{0}::move_elements_from_list]: loop 0: merged forward/backward function @@ -168,7 +165,7 @@ divergent def HashMap.move_elements_loop let l ← Vec.index_mut (List T) slots i let ls := mem.replace (List T) l List.Nil let ntable0 ← HashMap.move_elements_from_list T ntable ls - let i1 ← i + (Usize.ofInt 1 (by intlit)) + let i1 ← i + (Usize.ofInt 1) let l0 := mem.replace_back (List T) l List.Nil let slots0 ← Vec.index_mut_back (List T) slots i l0 HashMap.move_elements_loop T ntable0 slots0 i1 @@ -188,16 +185,16 @@ def HashMap.try_resize (T : Type) (self : HashMap T) : Result (HashMap T) := do let max_usize ← Scalar.cast .Usize core_num_u32_max_c let capacity := Vec.len (List T) self.slots - let n1 ← max_usize / (Usize.ofInt 2 (by intlit)) + let n1 ← max_usize / (Usize.ofInt 2) let (i, i0) := self.max_load_factor let i1 ← n1 / i if capacity <= i1 then do - let i2 ← capacity * (Usize.ofInt 2 (by intlit)) + let i2 ← capacity * (Usize.ofInt 2) let ntable ← HashMap.new_with_capacity T i2 i i0 let (ntable0, _) ← - HashMap.move_elements T ntable self.slots (Usize.ofInt 0 (by intlit)) + HashMap.move_elements T ntable self.slots (Usize.ofInt 0) Result.ret { ntable0 @@ -377,7 +374,7 @@ def HashMap.remove | Option.none => Result.ret Option.none | Option.some x0 => do - let _ ← self.num_entries - (Usize.ofInt 1 (by intlit)) + let _ ← self.num_entries - (Usize.ofInt 1) Result.ret (Option.some x0) /- [hashmap::HashMap::{0}::remove]: backward function 0 -/ @@ -397,7 +394,7 @@ def HashMap.remove_back Result.ret { self with slots := v } | Option.some x0 => do - let i0 ← self.num_entries - (Usize.ofInt 1 (by intlit)) + let i0 ← self.num_entries - (Usize.ofInt 1) let l0 ← HashMap.remove_from_list_back T key l let v ← Vec.index_mut_back (List T) self.slots hash_mod l0 Result.ret { self with num_entries := i0, slots := v } @@ -406,55 +403,43 @@ def HashMap.remove_back def test1 : Result Unit := do let hm ← HashMap.new U64 - let hm0 ← - HashMap.insert U64 hm (Usize.ofInt 0 (by intlit)) - (U64.ofInt 42 (by intlit)) - let hm1 ← - HashMap.insert U64 hm0 (Usize.ofInt 128 (by intlit)) - (U64.ofInt 18 (by intlit)) - let hm2 ← - HashMap.insert U64 hm1 (Usize.ofInt 1024 (by intlit)) - (U64.ofInt 138 (by intlit)) - let hm3 ← - HashMap.insert U64 hm2 (Usize.ofInt 1056 (by intlit)) - (U64.ofInt 256 (by intlit)) - let i ← HashMap.get U64 hm3 (Usize.ofInt 128 (by intlit)) - if not (i = (U64.ofInt 18 (by intlit))) + let hm0 ← HashMap.insert U64 hm (Usize.ofInt 0) (U64.ofInt 42) + let hm1 ← HashMap.insert U64 hm0 (Usize.ofInt 128) (U64.ofInt 18) + let hm2 ← HashMap.insert U64 hm1 (Usize.ofInt 1024) (U64.ofInt 138) + let hm3 ← HashMap.insert U64 hm2 (Usize.ofInt 1056) (U64.ofInt 256) + let i ← HashMap.get U64 hm3 (Usize.ofInt 128) + if not (i = (U64.ofInt 18)) then Result.fail Error.panic else do let hm4 ← - HashMap.get_mut_back U64 hm3 (Usize.ofInt 1024 (by intlit)) - (U64.ofInt 56 (by intlit)) - let i0 ← HashMap.get U64 hm4 (Usize.ofInt 1024 (by intlit)) - if not (i0 = (U64.ofInt 56 (by intlit))) + HashMap.get_mut_back U64 hm3 (Usize.ofInt 1024) (U64.ofInt 56) + let i0 ← HashMap.get U64 hm4 (Usize.ofInt 1024) + if not (i0 = (U64.ofInt 56)) then Result.fail Error.panic else do - let x ← HashMap.remove U64 hm4 (Usize.ofInt 1024 (by intlit)) + let x ← HashMap.remove U64 hm4 (Usize.ofInt 1024) match x with | Option.none => Result.fail Error.panic | Option.some x0 => - if not (x0 = (U64.ofInt 56 (by intlit))) + if not (x0 = (U64.ofInt 56)) then Result.fail Error.panic else do - let hm5 ← - HashMap.remove_back U64 hm4 (Usize.ofInt 1024 (by intlit)) - let i1 ← HashMap.get U64 hm5 (Usize.ofInt 0 (by intlit)) - if not (i1 = (U64.ofInt 42 (by intlit))) + let hm5 ← HashMap.remove_back U64 hm4 (Usize.ofInt 1024) + let i1 ← HashMap.get U64 hm5 (Usize.ofInt 0) + if not (i1 = (U64.ofInt 42)) then Result.fail Error.panic else do - let i2 ← - HashMap.get U64 hm5 (Usize.ofInt 128 (by intlit)) - if not (i2 = (U64.ofInt 18 (by intlit))) + let i2 ← HashMap.get U64 hm5 (Usize.ofInt 128) + if not (i2 = (U64.ofInt 18)) then Result.fail Error.panic else do - let i3 ← - HashMap.get U64 hm5 (Usize.ofInt 1056 (by intlit)) - if not (i3 = (U64.ofInt 256 (by intlit))) + let i3 ← HashMap.get U64 hm5 (Usize.ofInt 1056) + if not (i3 = (U64.ofInt 256)) then Result.fail Error.panic else Result.ret () diff --git a/tests/lean/HashmapMain/Funs.lean b/tests/lean/HashmapMain/Funs.lean index e82cc9b2..610bae46 100644 --- a/tests/lean/HashmapMain/Funs.lean +++ b/tests/lean/HashmapMain/Funs.lean @@ -15,11 +15,11 @@ divergent def hashmap.HashMap.allocate_slots_loop (T : Type) (slots : Vec (hashmap.List T)) (n : Usize) : Result (Vec (hashmap.List T)) := - if n > (Usize.ofInt 0 (by intlit)) + if n > (Usize.ofInt 0) then do let slots0 ← Vec.push (hashmap.List T) slots hashmap.List.Nil - let n0 ← n - (Usize.ofInt 1 (by intlit)) + let n0 ← n - (Usize.ofInt 1) hashmap.HashMap.allocate_slots_loop T slots0 n0 else Result.ret slots @@ -43,7 +43,7 @@ def hashmap.HashMap.new_with_capacity let i0 ← i / max_load_divisor Result.ret { - num_entries := (Usize.ofInt 0 (by intlit)), + num_entries := (Usize.ofInt 0), max_load_factor := (max_load_dividend, max_load_divisor), max_load := i0, slots := slots @@ -51,8 +51,8 @@ def hashmap.HashMap.new_with_capacity /- [hashmap_main::hashmap::HashMap::{0}::new]: forward function -/ def hashmap.HashMap.new (T : Type) : Result (hashmap.HashMap T) := - hashmap.HashMap.new_with_capacity T (Usize.ofInt 32 (by intlit)) - (Usize.ofInt 4 (by intlit)) (Usize.ofInt 5 (by intlit)) + hashmap.HashMap.new_with_capacity T (Usize.ofInt 32) (Usize.ofInt 4) + (Usize.ofInt 5) /- [hashmap_main::hashmap::HashMap::{0}::clear]: loop 0: merged forward/backward function (there is a single backward function, and the forward function returns ()) -/ @@ -64,7 +64,7 @@ divergent def hashmap.HashMap.clear_loop if i < i0 then do - let i1 ← i + (Usize.ofInt 1 (by intlit)) + let i1 ← i + (Usize.ofInt 1) let slots0 ← Vec.index_mut_back (hashmap.List T) slots i hashmap.List.Nil hashmap.HashMap.clear_loop T slots0 i1 @@ -75,10 +75,8 @@ divergent def hashmap.HashMap.clear_loop def hashmap.HashMap.clear (T : Type) (self : hashmap.HashMap T) : Result (hashmap.HashMap T) := do - let v ← - hashmap.HashMap.clear_loop T self.slots (Usize.ofInt 0 (by intlit)) - Result.ret - { self with num_entries := (Usize.ofInt 0 (by intlit)), slots := v } + let v ← hashmap.HashMap.clear_loop T self.slots (Usize.ofInt 0) + Result.ret { self with num_entries := (Usize.ofInt 0), slots := v } /- [hashmap_main::hashmap::HashMap::{0}::len]: forward function -/ def hashmap.HashMap.len (T : Type) (self : hashmap.HashMap T) : Result Usize := @@ -138,7 +136,7 @@ def hashmap.HashMap.insert_no_resize if inserted then do - let i0 ← self.num_entries + (Usize.ofInt 1 (by intlit)) + let i0 ← self.num_entries + (Usize.ofInt 1) let l0 ← hashmap.HashMap.insert_in_list_back T key value l let v ← Vec.index_mut_back (hashmap.List T) self.slots hash_mod l0 Result.ret { self with num_entries := i0, slots := v } @@ -149,8 +147,7 @@ def hashmap.HashMap.insert_no_resize Result.ret { self with slots := v } /- [core::num::u32::{9}::MAX] -/ -def core_num_u32_max_body : Result U32 := - Result.ret (U32.ofInt 4294967295 (by intlit)) +def core_num_u32_max_body : Result U32 := Result.ret (U32.ofInt 4294967295) def core_num_u32_max_c : U32 := eval_global core_num_u32_max_body (by simp) /- [hashmap_main::hashmap::HashMap::{0}::move_elements_from_list]: loop 0: merged forward/backward function @@ -188,7 +185,7 @@ divergent def hashmap.HashMap.move_elements_loop let l ← Vec.index_mut (hashmap.List T) slots i let ls := mem.replace (hashmap.List T) l hashmap.List.Nil let ntable0 ← hashmap.HashMap.move_elements_from_list T ntable ls - let i1 ← i + (Usize.ofInt 1 (by intlit)) + let i1 ← i + (Usize.ofInt 1) let l0 := mem.replace_back (hashmap.List T) l hashmap.List.Nil let slots0 ← Vec.index_mut_back (hashmap.List T) slots i l0 hashmap.HashMap.move_elements_loop T ntable0 slots0 i1 @@ -210,17 +207,16 @@ def hashmap.HashMap.try_resize do let max_usize ← Scalar.cast .Usize core_num_u32_max_c let capacity := Vec.len (hashmap.List T) self.slots - let n1 ← max_usize / (Usize.ofInt 2 (by intlit)) + let n1 ← max_usize / (Usize.ofInt 2) let (i, i0) := self.max_load_factor let i1 ← n1 / i if capacity <= i1 then do - let i2 ← capacity * (Usize.ofInt 2 (by intlit)) + let i2 ← capacity * (Usize.ofInt 2) let ntable ← hashmap.HashMap.new_with_capacity T i2 i i0 let (ntable0, _) ← - hashmap.HashMap.move_elements T ntable self.slots - (Usize.ofInt 0 (by intlit)) + hashmap.HashMap.move_elements T ntable self.slots (Usize.ofInt 0) Result.ret { ntable0 @@ -411,7 +407,7 @@ def hashmap.HashMap.remove | Option.none => Result.ret Option.none | Option.some x0 => do - let _ ← self.num_entries - (Usize.ofInt 1 (by intlit)) + let _ ← self.num_entries - (Usize.ofInt 1) Result.ret (Option.some x0) /- [hashmap_main::hashmap::HashMap::{0}::remove]: backward function 0 -/ @@ -433,7 +429,7 @@ def hashmap.HashMap.remove_back Result.ret { self with slots := v } | Option.some x0 => do - let i0 ← self.num_entries - (Usize.ofInt 1 (by intlit)) + let i0 ← self.num_entries - (Usize.ofInt 1) let l0 ← hashmap.HashMap.remove_from_list_back T key l let v ← Vec.index_mut_back (hashmap.List T) self.slots hash_mod l0 Result.ret { self with num_entries := i0, slots := v } @@ -442,60 +438,48 @@ def hashmap.HashMap.remove_back def hashmap.test1 : Result Unit := do let hm ← hashmap.HashMap.new U64 - let hm0 ← - hashmap.HashMap.insert U64 hm (Usize.ofInt 0 (by intlit)) - (U64.ofInt 42 (by intlit)) - let hm1 ← - hashmap.HashMap.insert U64 hm0 (Usize.ofInt 128 (by intlit)) - (U64.ofInt 18 (by intlit)) + let hm0 ← hashmap.HashMap.insert U64 hm (Usize.ofInt 0) (U64.ofInt 42) + let hm1 ← hashmap.HashMap.insert U64 hm0 (Usize.ofInt 128) (U64.ofInt 18) let hm2 ← - hashmap.HashMap.insert U64 hm1 (Usize.ofInt 1024 (by intlit)) - (U64.ofInt 138 (by intlit)) + hashmap.HashMap.insert U64 hm1 (Usize.ofInt 1024) (U64.ofInt 138) let hm3 ← - hashmap.HashMap.insert U64 hm2 (Usize.ofInt 1056 (by intlit)) - (U64.ofInt 256 (by intlit)) - let i ← hashmap.HashMap.get U64 hm3 (Usize.ofInt 128 (by intlit)) - if not (i = (U64.ofInt 18 (by intlit))) + hashmap.HashMap.insert U64 hm2 (Usize.ofInt 1056) (U64.ofInt 256) + let i ← hashmap.HashMap.get U64 hm3 (Usize.ofInt 128) + if not (i = (U64.ofInt 18)) then Result.fail Error.panic else do let hm4 ← - hashmap.HashMap.get_mut_back U64 hm3 (Usize.ofInt 1024 (by intlit)) - (U64.ofInt 56 (by intlit)) - let i0 ← hashmap.HashMap.get U64 hm4 (Usize.ofInt 1024 (by intlit)) - if not (i0 = (U64.ofInt 56 (by intlit))) + hashmap.HashMap.get_mut_back U64 hm3 (Usize.ofInt 1024) + (U64.ofInt 56) + let i0 ← hashmap.HashMap.get U64 hm4 (Usize.ofInt 1024) + if not (i0 = (U64.ofInt 56)) then Result.fail Error.panic else do - let x ← - hashmap.HashMap.remove U64 hm4 (Usize.ofInt 1024 (by intlit)) + let x ← hashmap.HashMap.remove U64 hm4 (Usize.ofInt 1024) match x with | Option.none => Result.fail Error.panic | Option.some x0 => - if not (x0 = (U64.ofInt 56 (by intlit))) + if not (x0 = (U64.ofInt 56)) then Result.fail Error.panic else do let hm5 ← - hashmap.HashMap.remove_back U64 hm4 - (Usize.ofInt 1024 (by intlit)) - let i1 ← - hashmap.HashMap.get U64 hm5 (Usize.ofInt 0 (by intlit)) - if not (i1 = (U64.ofInt 42 (by intlit))) + hashmap.HashMap.remove_back U64 hm4 (Usize.ofInt 1024) + let i1 ← hashmap.HashMap.get U64 hm5 (Usize.ofInt 0) + if not (i1 = (U64.ofInt 42)) then Result.fail Error.panic else do - let i2 ← - hashmap.HashMap.get U64 hm5 - (Usize.ofInt 128 (by intlit)) - if not (i2 = (U64.ofInt 18 (by intlit))) + let i2 ← hashmap.HashMap.get U64 hm5 (Usize.ofInt 128) + if not (i2 = (U64.ofInt 18)) then Result.fail Error.panic else do let i3 ← - hashmap.HashMap.get U64 hm5 - (Usize.ofInt 1056 (by intlit)) - if not (i3 = (U64.ofInt 256 (by intlit))) + hashmap.HashMap.get U64 hm5 (Usize.ofInt 1056) + if not (i3 = (U64.ofInt 256)) then Result.fail Error.panic else Result.ret () diff --git a/tests/lean/Loops/Funs.lean b/tests/lean/Loops/Funs.lean index 8cac7ac0..f7e6603d 100644 --- a/tests/lean/Loops/Funs.lean +++ b/tests/lean/Loops/Funs.lean @@ -8,16 +8,15 @@ namespace loops /- [loops::sum]: loop 0: forward function -/ divergent def sum_loop (max : U32) (i : U32) (s : U32) : Result U32 := if i < max - then - do - let s0 ← s + i - let i0 ← i + (U32.ofInt 1 (by intlit)) - sum_loop max i0 s0 - else s * (U32.ofInt 2 (by intlit)) + then do + let s0 ← s + i + let i0 ← i + (U32.ofInt 1) + sum_loop max i0 s0 + else s * (U32.ofInt 2) /- [loops::sum]: forward function -/ def sum (max : U32) : Result U32 := - sum_loop max (U32.ofInt 0 (by intlit)) (U32.ofInt 0 (by intlit)) + sum_loop max (U32.ofInt 0) (U32.ofInt 0) /- [loops::sum_with_mut_borrows]: loop 0: forward function -/ divergent def sum_with_mut_borrows_loop @@ -26,14 +25,13 @@ divergent def sum_with_mut_borrows_loop then do let ms0 ← ms + mi - let mi0 ← mi + (U32.ofInt 1 (by intlit)) + let mi0 ← mi + (U32.ofInt 1) sum_with_mut_borrows_loop max mi0 ms0 - else ms * (U32.ofInt 2 (by intlit)) + else ms * (U32.ofInt 2) /- [loops::sum_with_mut_borrows]: forward function -/ def sum_with_mut_borrows (max : U32) : Result U32 := - sum_with_mut_borrows_loop max (U32.ofInt 0 (by intlit)) - (U32.ofInt 0 (by intlit)) + sum_with_mut_borrows_loop max (U32.ofInt 0) (U32.ofInt 0) /- [loops::sum_with_shared_borrows]: loop 0: forward function -/ divergent def sum_with_shared_borrows_loop @@ -41,15 +39,14 @@ divergent def sum_with_shared_borrows_loop if i < max then do - let i0 ← i + (U32.ofInt 1 (by intlit)) + let i0 ← i + (U32.ofInt 1) let s0 ← s + i0 sum_with_shared_borrows_loop max i0 s0 - else s * (U32.ofInt 2 (by intlit)) + else s * (U32.ofInt 2) /- [loops::sum_with_shared_borrows]: forward function -/ def sum_with_shared_borrows (max : U32) : Result U32 := - sum_with_shared_borrows_loop max (U32.ofInt 0 (by intlit)) - (U32.ofInt 0 (by intlit)) + sum_with_shared_borrows_loop max (U32.ofInt 0) (U32.ofInt 0) /- [loops::clear]: loop 0: merged forward/backward function (there is a single backward function, and the forward function returns ()) -/ @@ -58,15 +55,15 @@ divergent def clear_loop (v : Vec U32) (i : Usize) : Result (Vec U32) := if i < i0 then do - let i1 ← i + (Usize.ofInt 1 (by intlit)) - let v0 ← Vec.index_mut_back U32 v i (U32.ofInt 0 (by intlit)) + let i1 ← i + (Usize.ofInt 1) + let v0 ← Vec.index_mut_back U32 v i (U32.ofInt 0) clear_loop v0 i1 else Result.ret v /- [loops::clear]: merged forward/backward function (there is a single backward function, and the forward function returns ()) -/ def clear (v : Vec U32) : Result (Vec U32) := - clear_loop v (Usize.ofInt 0 (by intlit)) + clear_loop v (Usize.ofInt 0) /- [loops::list_mem]: loop 0: forward function -/ divergent def list_mem_loop (x : U32) (ls : List U32) : Result Bool := @@ -85,12 +82,11 @@ divergent def list_nth_mut_loop_loop (T : Type) (ls : List T) (i : U32) : Result T := match ls with | List.Cons x tl => - if i = (U32.ofInt 0 (by intlit)) + if i = (U32.ofInt 0) then Result.ret x - else - do - let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_mut_loop_loop T tl i0 + else do + let i0 ← i - (U32.ofInt 1) + list_nth_mut_loop_loop T tl i0 | List.Nil => Result.fail Error.panic /- [loops::list_nth_mut_loop]: forward function -/ @@ -102,11 +98,11 @@ divergent def list_nth_mut_loop_loop_back (T : Type) (ls : List T) (i : U32) (ret0 : T) : Result (List T) := match ls with | List.Cons x tl => - if i = (U32.ofInt 0 (by intlit)) + if i = (U32.ofInt 0) then Result.ret (List.Cons ret0 tl) else do - let i0 ← i - (U32.ofInt 1 (by intlit)) + let i0 ← i - (U32.ofInt 1) let tl0 ← list_nth_mut_loop_loop_back T tl i0 ret0 Result.ret (List.Cons x tl0) | List.Nil => Result.fail Error.panic @@ -121,12 +117,11 @@ divergent def list_nth_shared_loop_loop (T : Type) (ls : List T) (i : U32) : Result T := match ls with | List.Cons x tl => - if i = (U32.ofInt 0 (by intlit)) + if i = (U32.ofInt 0) then Result.ret x - else - do - let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_shared_loop_loop T tl i0 + else do + let i0 ← i - (U32.ofInt 1) + list_nth_shared_loop_loop T tl i0 | List.Nil => Result.fail Error.panic /- [loops::list_nth_shared_loop]: forward function -/ @@ -144,7 +139,7 @@ divergent def get_elem_mut_loop (x : Usize) (ls : List Usize) : Result Usize := /- [loops::get_elem_mut]: forward function -/ def get_elem_mut (slots : Vec (List Usize)) (x : Usize) : Result Usize := do - let l ← Vec.index_mut (List Usize) slots (Usize.ofInt 0 (by intlit)) + let l ← Vec.index_mut (List Usize) slots (Usize.ofInt 0) get_elem_mut_loop x l /- [loops::get_elem_mut]: loop 0: backward function 0 -/ @@ -166,9 +161,9 @@ def get_elem_mut_back Result (Vec (List Usize)) := do - let l ← Vec.index_mut (List Usize) slots (Usize.ofInt 0 (by intlit)) + let l ← Vec.index_mut (List Usize) slots (Usize.ofInt 0) let l0 ← get_elem_mut_loop_back x l ret0 - Vec.index_mut_back (List Usize) slots (Usize.ofInt 0 (by intlit)) l0 + Vec.index_mut_back (List Usize) slots (Usize.ofInt 0) l0 /- [loops::get_elem_shared]: loop 0: forward function -/ divergent def get_elem_shared_loop @@ -182,7 +177,7 @@ divergent def get_elem_shared_loop /- [loops::get_elem_shared]: forward function -/ def get_elem_shared (slots : Vec (List Usize)) (x : Usize) : Result Usize := do - let l ← Vec.index (List Usize) slots (Usize.ofInt 0 (by intlit)) + let l ← Vec.index (List Usize) slots (Usize.ofInt 0) get_elem_shared_loop x l /- [loops::id_mut]: forward function -/ @@ -202,12 +197,11 @@ divergent def list_nth_mut_loop_with_id_loop (T : Type) (i : U32) (ls : List T) : Result T := match ls with | List.Cons x tl => - if i = (U32.ofInt 0 (by intlit)) + if i = (U32.ofInt 0) then Result.ret x - else - do - let i0 ← i - (U32.ofInt 1 (by intlit)) - list_nth_mut_loop_with_id_loop T i0 tl + else do + let i0 ← i - (U32.ofInt 1) + list_nth_mut_loop_with_id_loop T i0 tl | List.Nil => Result.fail Error.panic /- [loops::list_nth_mut_loop_with_id]: forward function -/ @@ -221,11 +215,11 @@ divergent def list_nth_mut_loop_with_id_loop_back (T : Type) (i : U32) (ls : List T) (ret0 : T) : Result (List T) := match ls with | List.Cons x tl => - if i = (U32.ofInt 0 (by intlit)) + if i = (U32.ofInt 0) then Result.ret (List.Cons ret0 tl) else do - let i0 ← i - (U32.ofInt 1 (by intlit)) + let i0 ← i - (U32.ofInt 1) let tl0 ← list_nth_mut_loop_with_id_loop_back T i0 tl ret0 Result.ret (List.Cons x tl0) | List.Nil => Result.fail Error.panic @@ -243,11 +237,11 @@ divergent def list_nth_shared_loop_with_id_loop (T : Type) (i : U32) (ls : List T) : Result T := match ls with | List.Cons x tl => - if i = (U32.ofInt 0 (by intlit)) + if i = (U32.ofInt 0) then Result.ret x else do - let i0 ← i - (U32.ofInt 1 (by intlit)) + let i0 ← i - (U32.ofInt 1) list_nth_shared_loop_with_id_loop T i0 tl | List.Nil => Result.fail Error.panic @@ -265,11 +259,11 @@ divergent def list_nth_mut_loop_pair_loop | List.Cons x0 tl0 => match ls1 with | List.Cons x1 tl1 => - if i = (U32.ofInt 0 (by intlit)) + if i = (U32.ofInt 0) then Result.ret (x0, x1) else do - let i0 ← i - (U32.ofInt 1 (by intlit)) + let i0 ← i - (U32.ofInt 1) list_nth_mut_loop_pair_loop T tl0 tl1 i0 | List.Nil => Result.fail Error.panic | List.Nil => Result.fail Error.panic @@ -288,11 +282,11 @@ divergent def list_nth_mut_loop_pair_loop_back'a | List.Cons x0 tl0 => match ls1 with | List.Cons x1 tl1 => - if i = (U32.ofInt 0 (by intlit)) + if i = (U32.ofInt 0) then Result.ret (List.Cons ret0 tl0) else do - let i0 ← i - (U32.ofInt 1 (by intlit)) + let i0 ← i - (U32.ofInt 1) let tl00 ← list_nth_mut_loop_pair_loop_back'a T tl0 tl1 i0 ret0 Result.ret (List.Cons x0 tl00) | List.Nil => Result.fail Error.panic @@ -314,11 +308,11 @@ divergent def list_nth_mut_loop_pair_loop_back'b | List.Cons x0 tl0 => match ls1 with | List.Cons x1 tl1 => - if i = (U32.ofInt 0 (by intlit)) + if i = (U32.ofInt 0) then Result.ret (List.Cons ret0 tl1) else do - let i0 ← i - (U32.ofInt 1 (by intlit)) + let i0 ← i - (U32.ofInt 1) let tl10 ← list_nth_mut_loop_pair_loop_back'b T tl0 tl1 i0 ret0 Result.ret (List.Cons x1 tl10) | List.Nil => Result.fail Error.panic @@ -338,11 +332,11 @@ divergent def list_nth_shared_loop_pair_loop | List.Cons x0 tl0 => match ls1 with | List.Cons x1 tl1 => - if i = (U32.ofInt 0 (by intlit)) + if i = (U32.ofInt 0) then Result.ret (x0, x1) else do - let i0 ← i - (U32.ofInt 1 (by intlit)) + let i0 ← i - (U32.ofInt 1) list_nth_shared_loop_pair_loop T tl0 tl1 i0 | List.Nil => Result.fail Error.panic | List.Nil => Result.fail Error.panic @@ -359,11 +353,11 @@ divergent def list_nth_mut_loop_pair_merge_loop | List.Cons x0 tl0 => match ls1 with | List.Cons x1 tl1 => - if i = (U32.ofInt 0 (by intlit)) + if i = (U32.ofInt 0) then Result.ret (x0, x1) else do - let i0 ← i - (U32.ofInt 1 (by intlit)) + let i0 ← i - (U32.ofInt 1) list_nth_mut_loop_pair_merge_loop T tl0 tl1 i0 | List.Nil => Result.fail Error.panic | List.Nil => Result.fail Error.panic @@ -382,12 +376,12 @@ divergent def list_nth_mut_loop_pair_merge_loop_back | List.Cons x0 tl0 => match ls1 with | List.Cons x1 tl1 => - if i = (U32.ofInt 0 (by intlit)) + if i = (U32.ofInt 0) then let (t, t0) := ret0 Result.ret (List.Cons t tl0, List.Cons t0 tl1) else do - let i0 ← i - (U32.ofInt 1 (by intlit)) + let i0 ← i - (U32.ofInt 1) let (tl00, tl10) ← list_nth_mut_loop_pair_merge_loop_back T tl0 tl1 i0 ret0 Result.ret (List.Cons x0 tl00, List.Cons x1 tl10) @@ -408,11 +402,11 @@ divergent def list_nth_shared_loop_pair_merge_loop | List.Cons x0 tl0 => match ls1 with | List.Cons x1 tl1 => - if i = (U32.ofInt 0 (by intlit)) + if i = (U32.ofInt 0) then Result.ret (x0, x1) else do - let i0 ← i - (U32.ofInt 1 (by intlit)) + let i0 ← i - (U32.ofInt 1) list_nth_shared_loop_pair_merge_loop T tl0 tl1 i0 | List.Nil => Result.fail Error.panic | List.Nil => Result.fail Error.panic @@ -429,11 +423,11 @@ divergent def list_nth_mut_shared_loop_pair_loop | List.Cons x0 tl0 => match ls1 with | List.Cons x1 tl1 => - if i = (U32.ofInt 0 (by intlit)) + if i = (U32.ofInt 0) then Result.ret (x0, x1) else do - let i0 ← i - (U32.ofInt 1 (by intlit)) + let i0 ← i - (U32.ofInt 1) list_nth_mut_shared_loop_pair_loop T tl0 tl1 i0 | List.Nil => Result.fail Error.panic | List.Nil => Result.fail Error.panic @@ -452,11 +446,11 @@ divergent def list_nth_mut_shared_loop_pair_loop_back | List.Cons x0 tl0 => match ls1 with | List.Cons x1 tl1 => - if i = (U32.ofInt 0 (by intlit)) + if i = (U32.ofInt 0) then Result.ret (List.Cons ret0 tl0) else do - let i0 ← i - (U32.ofInt 1 (by intlit)) + let i0 ← i - (U32.ofInt 1) let tl00 ← list_nth_mut_shared_loop_pair_loop_back T tl0 tl1 i0 ret0 Result.ret (List.Cons x0 tl00) @@ -477,11 +471,11 @@ divergent def list_nth_mut_shared_loop_pair_merge_loop | List.Cons x0 tl0 => match ls1 with | List.Cons x1 tl1 => - if i = (U32.ofInt 0 (by intlit)) + if i = (U32.ofInt 0) then Result.ret (x0, x1) else do - let i0 ← i - (U32.ofInt 1 (by intlit)) + let i0 ← i - (U32.ofInt 1) list_nth_mut_shared_loop_pair_merge_loop T tl0 tl1 i0 | List.Nil => Result.fail Error.panic | List.Nil => Result.fail Error.panic @@ -500,11 +494,11 @@ divergent def list_nth_mut_shared_loop_pair_merge_loop_back | List.Cons x0 tl0 => match ls1 with | List.Cons x1 tl1 => - if i = (U32.ofInt 0 (by intlit)) + if i = (U32.ofInt 0) then Result.ret (List.Cons ret0 tl0) else do - let i0 ← i - (U32.ofInt 1 (by intlit)) + let i0 ← i - (U32.ofInt 1) let tl00 ← list_nth_mut_shared_loop_pair_merge_loop_back T tl0 tl1 i0 ret0 Result.ret (List.Cons x0 tl00) @@ -525,11 +519,11 @@ divergent def list_nth_shared_mut_loop_pair_loop | List.Cons x0 tl0 => match ls1 with | List.Cons x1 tl1 => - if i = (U32.ofInt 0 (by intlit)) + if i = (U32.ofInt 0) then Result.ret (x0, x1) else do - let i0 ← i - (U32.ofInt 1 (by intlit)) + let i0 ← i - (U32.ofInt 1) list_nth_shared_mut_loop_pair_loop T tl0 tl1 i0 | List.Nil => Result.fail Error.panic | List.Nil => Result.fail Error.panic @@ -548,11 +542,11 @@ divergent def list_nth_shared_mut_loop_pair_loop_back | List.Cons x0 tl0 => match ls1 with | List.Cons x1 tl1 => - if i = (U32.ofInt 0 (by intlit)) + if i = (U32.ofInt 0) then Result.ret (List.Cons ret0 tl1) else do - let i0 ← i - (U32.ofInt 1 (by intlit)) + let i0 ← i - (U32.ofInt 1) let tl10 ← list_nth_shared_mut_loop_pair_loop_back T tl0 tl1 i0 ret0 Result.ret (List.Cons x1 tl10) @@ -573,11 +567,11 @@ divergent def list_nth_shared_mut_loop_pair_merge_loop | List.Cons x0 tl0 => match ls1 with | List.Cons x1 tl1 => - if i = (U32.ofInt 0 (by intlit)) + if i = (U32.ofInt 0) then Result.ret (x0, x1) else do - let i0 ← i - (U32.ofInt 1 (by intlit)) + let i0 ← i - (U32.ofInt 1) list_nth_shared_mut_loop_pair_merge_loop T tl0 tl1 i0 | List.Nil => Result.fail Error.panic | List.Nil => Result.fail Error.panic @@ -596,11 +590,11 @@ divergent def list_nth_shared_mut_loop_pair_merge_loop_back | List.Cons x0 tl0 => match ls1 with | List.Cons x1 tl1 => - if i = (U32.ofInt 0 (by intlit)) + if i = (U32.ofInt 0) then Result.ret (List.Cons ret0 tl1) else do - let i0 ← i - (U32.ofInt 1 (by intlit)) + let i0 ← i - (U32.ofInt 1) let tl10 ← list_nth_shared_mut_loop_pair_merge_loop_back T tl0 tl1 i0 ret0 Result.ret (List.Cons x1 tl10) diff --git a/tests/lean/NoNestedBorrows.lean b/tests/lean/NoNestedBorrows.lean index 1c6421bb..bc707fd9 100644 --- a/tests/lean/NoNestedBorrows.lean +++ b/tests/lean/NoNestedBorrows.lean @@ -53,7 +53,7 @@ def div_test (x : U32) (y : U32) : Result U32 := /- [no_nested_borrows::div_test1]: forward function -/ def div_test1 (x : U32) : Result U32 := - x / (U32.ofInt 2 (by intlit)) + x / (U32.ofInt 2) /- [no_nested_borrows::rem_test]: forward function -/ def rem_test (x : U32) (y : U32) : Result U32 := @@ -66,7 +66,7 @@ def cast_test (x : U32) : Result I32 := /- [no_nested_borrows::test2]: forward function -/ def test2 : Result Unit := do - let _ ← (U32.ofInt 23 (by intlit)) + (U32.ofInt 44 (by intlit)) + let _ ← (U32.ofInt 23) + (U32.ofInt 44) Result.ret () /- Unit test for [no_nested_borrows::test2] -/ @@ -81,10 +81,10 @@ def get_max (x : U32) (y : U32) : Result U32 := /- [no_nested_borrows::test3]: forward function -/ def test3 : Result Unit := do - let x ← get_max (U32.ofInt 4 (by intlit)) (U32.ofInt 3 (by intlit)) - let y ← get_max (U32.ofInt 10 (by intlit)) (U32.ofInt 11 (by intlit)) + let x ← get_max (U32.ofInt 4) (U32.ofInt 3) + let y ← get_max (U32.ofInt 10) (U32.ofInt 11) let z ← x + y - if not (z = (U32.ofInt 15 (by intlit))) + if not (z = (U32.ofInt 15)) then Result.fail Error.panic else Result.ret () @@ -94,8 +94,8 @@ def test3 : Result Unit := /- [no_nested_borrows::test_neg1]: forward function -/ def test_neg1 : Result Unit := do - let y ← - (I32.ofInt 3 (by intlit)) - if not (y = (I32.ofInt (-(3:Int)) (by intlit))) + let y ← - (I32.ofInt 3) + if not (y = (I32.ofInt (-(3:Int)))) then Result.fail Error.panic else Result.ret () @@ -104,7 +104,7 @@ def test_neg1 : Result Unit := /- [no_nested_borrows::refs_test1]: forward function -/ def refs_test1 : Result Unit := - if not ((I32.ofInt 1 (by intlit)) = (I32.ofInt 1 (by intlit))) + if not ((I32.ofInt 1) = (I32.ofInt 1)) then Result.fail Error.panic else Result.ret () @@ -113,16 +113,16 @@ def refs_test1 : Result Unit := /- [no_nested_borrows::refs_test2]: forward function -/ def refs_test2 : Result Unit := - if not ((I32.ofInt 2 (by intlit)) = (I32.ofInt 2 (by intlit))) + if not ((I32.ofInt 2) = (I32.ofInt 2)) then Result.fail Error.panic else - if not ((I32.ofInt 0 (by intlit)) = (I32.ofInt 0 (by intlit))) + if not ((I32.ofInt 0) = (I32.ofInt 0)) then Result.fail Error.panic else - if not ((I32.ofInt 2 (by intlit)) = (I32.ofInt 2 (by intlit))) + if not ((I32.ofInt 2) = (I32.ofInt 2)) then Result.fail Error.panic else - if not ((I32.ofInt 2 (by intlit)) = (I32.ofInt 2 (by intlit))) + if not ((I32.ofInt 2) = (I32.ofInt 2)) then Result.fail Error.panic else Result.ret () @@ -138,9 +138,9 @@ def test_list1 : Result Unit := /- [no_nested_borrows::test_box1]: forward function -/ def test_box1 : Result Unit := - let b := (I32.ofInt 1 (by intlit)) + let b := (I32.ofInt 1) let x := b - if not (x = (I32.ofInt 1 (by intlit))) + if not (x = (I32.ofInt 1)) then Result.fail Error.panic else Result.ret () @@ -166,8 +166,8 @@ def test_panic (b : Bool) : Result Unit := /- [no_nested_borrows::test_copy_int]: forward function -/ def test_copy_int : Result Unit := do - let y ← copy_int (I32.ofInt 0 (by intlit)) - if not ((I32.ofInt 0 (by intlit)) = y) + let y ← copy_int (I32.ofInt 0) + if not ((I32.ofInt 0) = y) then Result.fail Error.panic else Result.ret () @@ -184,7 +184,7 @@ def is_cons (T : Type) (l : List T) : Result Bool := def test_is_cons : Result Unit := do let l := List.Nil - let b ← is_cons I32 (List.Cons (I32.ofInt 0 (by intlit)) l) + let b ← is_cons I32 (List.Cons (I32.ofInt 0) l) if not b then Result.fail Error.panic else Result.ret () @@ -202,9 +202,9 @@ def split_list (T : Type) (l : List T) : Result (T × (List T)) := def test_split_list : Result Unit := do let l := List.Nil - let p ← split_list I32 (List.Cons (I32.ofInt 0 (by intlit)) l) + let p ← split_list I32 (List.Cons (I32.ofInt 0) l) let (hd, _) := p - if not (hd = (I32.ofInt 0 (by intlit))) + if not (hd = (I32.ofInt 0)) then Result.fail Error.panic else Result.ret () @@ -227,20 +227,17 @@ def choose_back /- [no_nested_borrows::choose_test]: forward function -/ def choose_test : Result Unit := do - let z ← - choose I32 true (I32.ofInt 0 (by intlit)) (I32.ofInt 0 (by intlit)) - let z0 ← z + (I32.ofInt 1 (by intlit)) - if not (z0 = (I32.ofInt 1 (by intlit))) + let z ← choose I32 true (I32.ofInt 0) (I32.ofInt 0) + let z0 ← z + (I32.ofInt 1) + if not (z0 = (I32.ofInt 1)) then Result.fail Error.panic else do - let (x, y) ← - choose_back I32 true (I32.ofInt 0 (by intlit)) - (I32.ofInt 0 (by intlit)) z0 - if not (x = (I32.ofInt 1 (by intlit))) + let (x, y) ← choose_back I32 true (I32.ofInt 0) (I32.ofInt 0) z0 + if not (x = (I32.ofInt 1)) then Result.fail Error.panic else - if not (y = (I32.ofInt 0 (by intlit))) + if not (y = (I32.ofInt 0)) then Result.fail Error.panic else Result.ret () @@ -268,20 +265,19 @@ end /- [no_nested_borrows::list_length]: forward function -/ divergent def list_length (T : Type) (l : List T) : Result U32 := match l with - | List.Cons t l1 => - do - let i ← list_length T l1 - (U32.ofInt 1 (by intlit)) + i - | List.Nil => Result.ret (U32.ofInt 0 (by intlit)) + | List.Cons t l1 => do + let i ← list_length T l1 + (U32.ofInt 1) + i + | List.Nil => Result.ret (U32.ofInt 0) /- [no_nested_borrows::list_nth_shared]: forward function -/ divergent def list_nth_shared (T : Type) (l : List T) (i : U32) : Result T := match l with | List.Cons x tl => - if i = (U32.ofInt 0 (by intlit)) + if i = (U32.ofInt 0) then Result.ret x else do - let i0 ← i - (U32.ofInt 1 (by intlit)) + let i0 ← i - (U32.ofInt 1) list_nth_shared T tl i0 | List.Nil => Result.fail Error.panic @@ -289,10 +285,10 @@ divergent def list_nth_shared (T : Type) (l : List T) (i : U32) : Result T := divergent def list_nth_mut (T : Type) (l : List T) (i : U32) : Result T := match l with | List.Cons x tl => - if i = (U32.ofInt 0 (by intlit)) + if i = (U32.ofInt 0) then Result.ret x else do - let i0 ← i - (U32.ofInt 1 (by intlit)) + let i0 ← i - (U32.ofInt 1) list_nth_mut T tl i0 | List.Nil => Result.fail Error.panic @@ -301,11 +297,11 @@ divergent def list_nth_mut_back (T : Type) (l : List T) (i : U32) (ret0 : T) : Result (List T) := match l with | List.Cons x tl => - if i = (U32.ofInt 0 (by intlit)) + if i = (U32.ofInt 0) then Result.ret (List.Cons ret0 tl) else do - let i0 ← i - (U32.ofInt 1 (by intlit)) + let i0 ← i - (U32.ofInt 1) let tl0 ← list_nth_mut_back T tl i0 ret0 Result.ret (List.Cons x tl0) | List.Nil => Result.fail Error.panic @@ -327,52 +323,47 @@ def list_rev (T : Type) (l : List T) : Result (List T) := def test_list_functions : Result Unit := do let l := List.Nil - let l0 := List.Cons (I32.ofInt 2 (by intlit)) l - let l1 := List.Cons (I32.ofInt 1 (by intlit)) l0 - let i ← list_length I32 (List.Cons (I32.ofInt 0 (by intlit)) l1) - if not (i = (U32.ofInt 3 (by intlit))) + let l0 := List.Cons (I32.ofInt 2) l + let l1 := List.Cons (I32.ofInt 1) l0 + let i ← list_length I32 (List.Cons (I32.ofInt 0) l1) + if not (i = (U32.ofInt 3)) then Result.fail Error.panic else do let i0 ← - list_nth_shared I32 (List.Cons (I32.ofInt 0 (by intlit)) l1) - (U32.ofInt 0 (by intlit)) - if not (i0 = (I32.ofInt 0 (by intlit))) + list_nth_shared I32 (List.Cons (I32.ofInt 0) l1) (U32.ofInt 0) + if not (i0 = (I32.ofInt 0)) then Result.fail Error.panic else do let i1 ← - list_nth_shared I32 (List.Cons (I32.ofInt 0 (by intlit)) l1) - (U32.ofInt 1 (by intlit)) - if not (i1 = (I32.ofInt 1 (by intlit))) + list_nth_shared I32 (List.Cons (I32.ofInt 0) l1) (U32.ofInt 1) + if not (i1 = (I32.ofInt 1)) then Result.fail Error.panic else do let i2 ← - list_nth_shared I32 (List.Cons (I32.ofInt 0 (by intlit)) l1) - (U32.ofInt 2 (by intlit)) - if not (i2 = (I32.ofInt 2 (by intlit))) + list_nth_shared I32 (List.Cons (I32.ofInt 0) l1) + (U32.ofInt 2) + if not (i2 = (I32.ofInt 2)) then Result.fail Error.panic else do let ls ← - list_nth_mut_back I32 (List.Cons - (I32.ofInt 0 (by intlit)) l1) (U32.ofInt 1 (by intlit)) - (I32.ofInt 3 (by intlit)) - let i3 ← list_nth_shared I32 ls (U32.ofInt 0 (by intlit)) - if not (i3 = (I32.ofInt 0 (by intlit))) + list_nth_mut_back I32 (List.Cons (I32.ofInt 0) l1) + (U32.ofInt 1) (I32.ofInt 3) + let i3 ← list_nth_shared I32 ls (U32.ofInt 0) + if not (i3 = (I32.ofInt 0)) then Result.fail Error.panic else do - let i4 ← - list_nth_shared I32 ls (U32.ofInt 1 (by intlit)) - if not (i4 = (I32.ofInt 3 (by intlit))) + let i4 ← list_nth_shared I32 ls (U32.ofInt 1) + if not (i4 = (I32.ofInt 3)) then Result.fail Error.panic else do - let i5 ← - list_nth_shared I32 ls (U32.ofInt 2 (by intlit)) - if not (i5 = (I32.ofInt 2 (by intlit))) + let i5 ← list_nth_shared I32 ls (U32.ofInt 2) + if not (i5 = (I32.ofInt 2)) then Result.fail Error.panic else Result.ret () @@ -435,15 +426,15 @@ structure StructWithTuple (T1 T2 : Type) where /- [no_nested_borrows::new_tuple1]: forward function -/ def new_tuple1 : Result (StructWithTuple U32 U32) := - Result.ret { p := ((U32.ofInt 1 (by intlit)), (U32.ofInt 2 (by intlit))) } + Result.ret { p := ((U32.ofInt 1), (U32.ofInt 2)) } /- [no_nested_borrows::new_tuple2]: forward function -/ def new_tuple2 : Result (StructWithTuple I16 I16) := - Result.ret { p := ((I16.ofInt 1 (by intlit)), (I16.ofInt 2 (by intlit))) } + Result.ret { p := ((I16.ofInt 1), (I16.ofInt 2)) } /- [no_nested_borrows::new_tuple3]: forward function -/ def new_tuple3 : Result (StructWithTuple U64 I64) := - Result.ret { p := ((U64.ofInt 1 (by intlit)), (I64.ofInt 2 (by intlit))) } + Result.ret { p := ((U64.ofInt 1), (I64.ofInt 2)) } /- [no_nested_borrows::StructWithPair] -/ structure StructWithPair (T1 T2 : Type) where @@ -451,32 +442,31 @@ structure StructWithPair (T1 T2 : Type) where /- [no_nested_borrows::new_pair1]: forward function -/ def new_pair1 : Result (StructWithPair U32 U32) := - Result.ret - { p := { x := (U32.ofInt 1 (by intlit)), y := (U32.ofInt 2 (by intlit)) } } + Result.ret { p := { x := (U32.ofInt 1), y := (U32.ofInt 2) } } /- [no_nested_borrows::test_constants]: forward function -/ def test_constants : Result Unit := do let swt ← new_tuple1 let (i, _) := swt.p - if not (i = (U32.ofInt 1 (by intlit))) + if not (i = (U32.ofInt 1)) then Result.fail Error.panic else do let swt0 ← new_tuple2 let (i0, _) := swt0.p - if not (i0 = (I16.ofInt 1 (by intlit))) + if not (i0 = (I16.ofInt 1)) then Result.fail Error.panic else do let swt1 ← new_tuple3 let (i1, _) := swt1.p - if not (i1 = (U64.ofInt 1 (by intlit))) + if not (i1 = (U64.ofInt 1)) then Result.fail Error.panic else do let swp ← new_pair1 - if not (swp.p.x = (U32.ofInt 1 (by intlit))) + if not (swp.p.x = (U32.ofInt 1)) then Result.fail Error.panic else Result.ret () @@ -493,29 +483,29 @@ def test_weird_borrows1 : Result Unit := /- [no_nested_borrows::test_mem_replace]: merged forward/backward function (there is a single backward function, and the forward function returns ()) -/ def test_mem_replace (px : U32) : Result U32 := - let y := mem.replace U32 px (U32.ofInt 1 (by intlit)) - if not (y = (U32.ofInt 0 (by intlit))) + let y := mem.replace U32 px (U32.ofInt 1) + if not (y = (U32.ofInt 0)) then Result.fail Error.panic - else Result.ret (U32.ofInt 2 (by intlit)) + else Result.ret (U32.ofInt 2) /- [no_nested_borrows::test_shared_borrow_bool1]: forward function -/ def test_shared_borrow_bool1 (b : Bool) : Result U32 := if b - then Result.ret (U32.ofInt 0 (by intlit)) - else Result.ret (U32.ofInt 1 (by intlit)) + then Result.ret (U32.ofInt 0) + else Result.ret (U32.ofInt 1) /- [no_nested_borrows::test_shared_borrow_bool2]: forward function -/ def test_shared_borrow_bool2 : Result U32 := - Result.ret (U32.ofInt 0 (by intlit)) + Result.ret (U32.ofInt 0) /- [no_nested_borrows::test_shared_borrow_enum1]: forward function -/ def test_shared_borrow_enum1 (l : List U32) : Result U32 := match l with - | List.Cons i l0 => Result.ret (U32.ofInt 1 (by intlit)) - | List.Nil => Result.ret (U32.ofInt 0 (by intlit)) + | List.Cons i l0 => Result.ret (U32.ofInt 1) + | List.Nil => Result.ret (U32.ofInt 0) /- [no_nested_borrows::test_shared_borrow_enum2]: forward function -/ def test_shared_borrow_enum2 : Result U32 := - Result.ret (U32.ofInt 0 (by intlit)) + Result.ret (U32.ofInt 0) end no_nested_borrows diff --git a/tests/lean/Paper.lean b/tests/lean/Paper.lean index ade65656..cee7128a 100644 --- a/tests/lean/Paper.lean +++ b/tests/lean/Paper.lean @@ -7,13 +7,13 @@ namespace paper /- [paper::ref_incr]: merged forward/backward function (there is a single backward function, and the forward function returns ()) -/ def ref_incr (x : I32) : Result I32 := - x + (I32.ofInt 1 (by intlit)) + x + (I32.ofInt 1) /- [paper::test_incr]: forward function -/ def test_incr : Result Unit := do - let x ← ref_incr (I32.ofInt 0 (by intlit)) - if not (x = (I32.ofInt 1 (by intlit))) + let x ← ref_incr (I32.ofInt 0) + if not (x = (I32.ofInt 1)) then Result.fail Error.panic else Result.ret () @@ -36,20 +36,17 @@ def choose_back /- [paper::test_choose]: forward function -/ def test_choose : Result Unit := do - let z ← - choose I32 true (I32.ofInt 0 (by intlit)) (I32.ofInt 0 (by intlit)) - let z0 ← z + (I32.ofInt 1 (by intlit)) - if not (z0 = (I32.ofInt 1 (by intlit))) + let z ← choose I32 true (I32.ofInt 0) (I32.ofInt 0) + let z0 ← z + (I32.ofInt 1) + if not (z0 = (I32.ofInt 1)) then Result.fail Error.panic else do - let (x, y) ← - choose_back I32 true (I32.ofInt 0 (by intlit)) - (I32.ofInt 0 (by intlit)) z0 - if not (x = (I32.ofInt 1 (by intlit))) + let (x, y) ← choose_back I32 true (I32.ofInt 0) (I32.ofInt 0) z0 + if not (x = (I32.ofInt 1)) then Result.fail Error.panic else - if not (y = (I32.ofInt 0 (by intlit))) + if not (y = (I32.ofInt 0)) then Result.fail Error.panic else Result.ret () @@ -65,10 +62,10 @@ inductive List (T : Type) := divergent def list_nth_mut (T : Type) (l : List T) (i : U32) : Result T := match l with | List.Cons x tl => - if i = (U32.ofInt 0 (by intlit)) + if i = (U32.ofInt 0) then Result.ret x else do - let i0 ← i - (U32.ofInt 1 (by intlit)) + let i0 ← i - (U32.ofInt 1) list_nth_mut T tl i0 | List.Nil => Result.fail Error.panic @@ -77,11 +74,11 @@ divergent def list_nth_mut_back (T : Type) (l : List T) (i : U32) (ret0 : T) : Result (List T) := match l with | List.Cons x tl => - if i = (U32.ofInt 0 (by intlit)) + if i = (U32.ofInt 0) then Result.ret (List.Cons ret0 tl) else do - let i0 ← i - (U32.ofInt 1 (by intlit)) + let i0 ← i - (U32.ofInt 1) let tl0 ← list_nth_mut_back T tl i0 ret0 Result.ret (List.Cons x tl0) | List.Nil => Result.fail Error.panic @@ -92,23 +89,20 @@ divergent def sum (l : List I32) : Result I32 := | List.Cons x tl => do let i ← sum tl x + i - | List.Nil => Result.ret (I32.ofInt 0 (by intlit)) + | List.Nil => Result.ret (I32.ofInt 0) /- [paper::test_nth]: forward function -/ def test_nth : Result Unit := do let l := List.Nil - let l0 := List.Cons (I32.ofInt 3 (by intlit)) l - let l1 := List.Cons (I32.ofInt 2 (by intlit)) l0 - let x ← - list_nth_mut I32 (List.Cons (I32.ofInt 1 (by intlit)) l1) - (U32.ofInt 2 (by intlit)) - let x0 ← x + (I32.ofInt 1 (by intlit)) + let l0 := List.Cons (I32.ofInt 3) l + let l1 := List.Cons (I32.ofInt 2) l0 + let x ← list_nth_mut I32 (List.Cons (I32.ofInt 1) l1) (U32.ofInt 2) + let x0 ← x + (I32.ofInt 1) let l2 ← - list_nth_mut_back I32 (List.Cons (I32.ofInt 1 (by intlit)) l1) - (U32.ofInt 2 (by intlit)) x0 + list_nth_mut_back I32 (List.Cons (I32.ofInt 1) l1) (U32.ofInt 2) x0 let i ← sum l2 - if not (i = (I32.ofInt 7 (by intlit))) + if not (i = (I32.ofInt 7)) then Result.fail Error.panic else Result.ret () @@ -120,7 +114,7 @@ def call_choose (p : (U32 × U32)) : Result U32 := do let (px, py) := p let pz ← choose U32 true px py - let pz0 ← pz + (U32.ofInt 1 (by intlit)) + let pz0 ← pz + (U32.ofInt 1) let (px0, _) ← choose_back U32 true px py pz0 Result.ret px0 diff --git a/tests/lean/lake-manifest.json b/tests/lean/lake-manifest.json index 75f7010a..8bbfb7cd 100644 --- a/tests/lean/lake-manifest.json +++ b/tests/lean/lake-manifest.json @@ -8,10 +8,16 @@ "name": "proofwidgets", "inputRev?": "v0.0.11"}}, {"path": {"name": "Base", "dir": "./../../backends/lean"}}, + {"git": + {"url": "https://github.com/mhuisi/lean4-cli.git", + "subDir?": null, + "rev": "5a858c32963b6b19be0d477a30a1f4b6c120be7e", + "name": "Cli", + "inputRev?": "nightly"}}, {"git": {"url": "https://github.com/leanprover-community/mathlib4.git", "subDir?": null, - "rev": "bb4fb766e41dd3a64197263ec132c7f9c4b50065", + "rev": "82fe7e902b01fe686b91c19550f1e228ad2dec1c", "name": "mathlib", "inputRev?": null}}, {"git": -- cgit v1.2.3 From 4f7ebc2358d78d31d63a609a32e5a732b82d468e Mon Sep 17 00:00:00 2001 From: Son Ho Date: Mon, 17 Jul 2023 12:12:34 +0200 Subject: Update the lean dependencies and update IList --- tests/lean/lake-manifest.json | 8 ++++---- tests/lean/lean-toolchain | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'tests/lean') diff --git a/tests/lean/lake-manifest.json b/tests/lean/lake-manifest.json index 8bbfb7cd..94030cb6 100644 --- a/tests/lean/lake-manifest.json +++ b/tests/lean/lake-manifest.json @@ -17,24 +17,24 @@ {"git": {"url": "https://github.com/leanprover-community/mathlib4.git", "subDir?": null, - "rev": "82fe7e902b01fe686b91c19550f1e228ad2dec1c", + "rev": "fa05951a270fef2873666c46f138e90338cd48d6", "name": "mathlib", "inputRev?": null}}, {"git": {"url": "https://github.com/gebner/quote4", "subDir?": null, - "rev": "c71f94e34c1cda52eef5c93dc9da409ab2727420", + "rev": "c0d9516f44d07feee01c1103c8f2f7c24a822b55", "name": "Qq", "inputRev?": "master"}}, {"git": {"url": "https://github.com/JLimperg/aesop", "subDir?": null, - "rev": "ca73109cc40837bc61df8024c9016da4b4f99d4c", + "rev": "f04538ab6ad07642368cf11d2702acc0a9b4bcee", "name": "aesop", "inputRev?": "master"}}, {"git": {"url": "https://github.com/leanprover/std4", "subDir?": null, - "rev": "e68aa8f5fe47aad78987df45f99094afbcb5e936", + "rev": "dff883c55395438ae2a5c65ad5ddba084b600feb", "name": "std", "inputRev?": "main"}}]} diff --git a/tests/lean/lean-toolchain b/tests/lean/lean-toolchain index 42e7d786..334c5053 100644 --- a/tests/lean/lean-toolchain +++ b/tests/lean/lean-toolchain @@ -1 +1 @@ -leanprover/lean4:nightly-2023-06-20 \ No newline at end of file +leanprover/lean4:nightly-2023-07-12 \ No newline at end of file -- cgit v1.2.3 From 510e409b551f876a28f93f869e108f3f9e761212 Mon Sep 17 00:00:00 2001 From: Aymeric Fromherz Date: Mon, 17 Jul 2023 16:09:33 +0200 Subject: With @sonmarcho, some Lean proofs for hashmaps + comments about possible improvements --- tests/lean/Hashmap.lean | 1 + tests/lean/Hashmap/Properties.lean | 195 +++++++++++++++++++++++++++++++++++++ 2 files changed, 196 insertions(+) create mode 100644 tests/lean/Hashmap/Properties.lean (limited to 'tests/lean') diff --git a/tests/lean/Hashmap.lean b/tests/lean/Hashmap.lean index 41630205..35034754 100644 --- a/tests/lean/Hashmap.lean +++ b/tests/lean/Hashmap.lean @@ -1 +1,2 @@ import Hashmap.Funs +import Hashmap.Properties diff --git a/tests/lean/Hashmap/Properties.lean b/tests/lean/Hashmap/Properties.lean new file mode 100644 index 00000000..ca1f29c4 --- /dev/null +++ b/tests/lean/Hashmap/Properties.lean @@ -0,0 +1,195 @@ +import Hashmap.Funs + +open Primitives +open Result + + +namespace List + +theorem index_ne + {α : Type u} [Inhabited α] (l: List α) (i: ℤ) (j: ℤ) (x: α) : + 0 ≤ i → i < l.len → 0 ≤ j → j < l.len → j ≠ i → + (l.update i x).index j = l.index j + := + λ _ _ _ _ _ => match l with + | [] => by simp at * + | hd :: tl => + if h: i = 0 then + have : j ≠ 0 := by scalar_tac + by simp [*] + else if h : j = 0 then + have : i ≠ 0 := by scalar_tac + by simp [*] + else + by + simp [*] + simp at * + apply index_ne <;> scalar_tac + +theorem index_eq + {α : Type u} [Inhabited α] (l: List α) (i: ℤ) (x: α) : + 0 ≤ i → i < l.len → + (l.update i x).index i = x + := + fun _ _ => match l with + | [] => by simp at *; exfalso; scalar_tac -- TODO: exfalso needed. Son FIXME + | hd :: tl => + if h: i = 0 then + by + simp [*] + else + by + simp [*] + simp at * + apply index_eq <;> scalar_tac + +end List + + +namespace Primitives + +@[pspec] +theorem Vec.index_mut_spec + {α : Type u} [Inhabited α] (v: Vec α) (i: Usize) (h: i.val < v.val.len) : + ∃ x, + v.index_mut α i = ret x ∧ x = v.val.index i.val + := by sorry + +@[pspec] +theorem Vec.index_mut_back_spec + {α : Type u} (v: Vec α) (i: Usize) (x:α) : + i.val < v.val.len → ∃ nv, + v.index_mut_back α i x = ret nv ∧ nv.val = v.val.update i.val x + := by sorry + +end Primitives + +namespace List + +def allP {α : Type u} (l : List α) (p: α → Prop) : Prop := + foldr (fun a r => p a ∧ r) True l + +@[simp] +theorem allP_nil {α : Type u} (p: α → Prop) : allP [] p := + by simp [allP, foldr] + +@[simp] +theorem allP_cons {α : Type u} (hd: α) (tl : List α) (p: α → Prop) : + allP (hd :: tl) p ↔ p hd ∧ allP tl p + := by simp [allP, foldr] + + +def pairwise_rel + {α : Type u} (rel : α → α → Prop) (l: List α) : Prop + := match l with + | [] => True + | hd :: tl => allP tl (rel hd) ∧ pairwise_rel rel tl + +@[simp] +theorem pairwise_rel_nil {α : Type u} (rel : α → α → Prop) : + pairwise_rel rel [] + := by simp [pairwise_rel] + +@[simp] +theorem pairwise_rel_cons {α : Type u} (rel : α → α → Prop) (hd: α) (tl: List α) : + pairwise_rel rel (hd :: tl) ↔ allP tl (rel hd) ∧ pairwise_rel rel tl + := by simp [pairwise_rel] + +end List + +namespace hashmap + +namespace List + +def v {α : Type} (ls: List α) : _root_.List (Usize × α) := + match ls with + | Nil => [] + | Cons k x tl => (k, x) :: v tl + +def lookup {α : Type} (ls: _root_.List (Usize × α)) (key: Usize) : Option α := + match ls with + | [] => none + | (k, x) :: tl => if k = key then some x else lookup tl key + +end List + +namespace HashMap + +@[pspec] +theorem insert_in_list_spec {α : Type} (key: Usize) (value: α) (ls: List α) : + ∃ b, + insert_in_list α key value ls = ret b ∧ + (b ↔ List.lookup ls.v key = none) + := match ls with + | .Nil => by simp [insert_in_list, insert_in_list_loop, List.lookup] + | .Cons k v tl => + if h: k = key then -- TODO: The order of k/key matters + by + simp [insert_in_list, List.lookup] + rw [insert_in_list_loop] + simp [h] + else + by + have ⟨ b, hi ⟩ := insert_in_list_spec key value tl + exists b + simp [insert_in_list, List.lookup] + rw [insert_in_list_loop] -- TODO: Using it in simp leads to infinite recursion + simp [h] + simp [insert_in_list] at hi + exact hi + +/-- +@[pspec] +theorem insert_in_list_spec2 {α : Type} (key: Usize) (value: α) (ls: List α) : + ∃ b, + insert_in_list α key value ls = ret b ∧ + (b = (List.lookup ls.v key = none)) + := by + induction ls + case Nil => simp [insert_in_list, insert_in_list_loop, List.lookup] + case Cons k v tl ih => + simp only [insert_in_list, List.lookup] + rw [insert_in_list_loop] + simp only + if h: k = key then + simp [h] + else + conv => + rhs; ext; arg 1; simp [h] -- TODO: Simplify + simp [insert_in_list] at ih; + progress -- TODO: Does not work +--/ + +@[pspec] +theorem insert_in_list_back_spec {α : Type} (key: Usize) (value: α) (l0: List α) : + ∃ l1, + insert_in_list_back α key value l0 = ret l1 ∧ + List.lookup l1.v key = value ∧ + (∀ k, k ≠ key → List.lookup l1.v k = List.lookup l0.v k) + := match l0 with + | .Nil => by simp [insert_in_list_back, insert_in_list_loop_back, List.lookup]; tauto + | .Cons k v tl => + if h: k = key then + by + simp [insert_in_list_back, List.lookup] + rw [insert_in_list_loop_back] + simp [h, List.lookup] + intro k1 h1 + have h2 : ¬(key = k1) := by tauto -- TODO: Why is the order of args in eq swapped + simp [h2] + else + by + simp [insert_in_list_back, List.lookup] + rw [insert_in_list_loop_back] + simp [h, List.lookup] + have ⟨tl0 , _, _ ⟩ := insert_in_list_back_spec key value tl -- TODO: Use progress + simp [insert_in_list_back] at * + simp [*] + have : ¬ (key = k) := by tauto + simp [List.lookup, *] + simp (config := {contextual := true}) [*] + +end HashMap +-- def distinct_keys {α : Type u} + +end hashmap -- cgit v1.2.3 From aaa2fdfd104f7010ebaf2977a22280716ac15d13 Mon Sep 17 00:00:00 2001 From: Son Ho Date: Mon, 17 Jul 2023 23:42:25 +0200 Subject: Make minor modifications --- tests/lean/Hashmap/Properties.lean | 25 ------------------------- 1 file changed, 25 deletions(-) (limited to 'tests/lean') diff --git a/tests/lean/Hashmap/Properties.lean b/tests/lean/Hashmap/Properties.lean index ca1f29c4..1db39422 100644 --- a/tests/lean/Hashmap/Properties.lean +++ b/tests/lean/Hashmap/Properties.lean @@ -3,7 +3,6 @@ import Hashmap.Funs open Primitives open Result - namespace List theorem index_ne @@ -43,29 +42,6 @@ theorem index_eq simp at * apply index_eq <;> scalar_tac -end List - - -namespace Primitives - -@[pspec] -theorem Vec.index_mut_spec - {α : Type u} [Inhabited α] (v: Vec α) (i: Usize) (h: i.val < v.val.len) : - ∃ x, - v.index_mut α i = ret x ∧ x = v.val.index i.val - := by sorry - -@[pspec] -theorem Vec.index_mut_back_spec - {α : Type u} (v: Vec α) (i: Usize) (x:α) : - i.val < v.val.len → ∃ nv, - v.index_mut_back α i x = ret nv ∧ nv.val = v.val.update i.val x - := by sorry - -end Primitives - -namespace List - def allP {α : Type u} (l : List α) (p: α → Prop) : Prop := foldr (fun a r => p a ∧ r) True l @@ -78,7 +54,6 @@ theorem allP_cons {α : Type u} (hd: α) (tl : List α) (p: α → Prop) : allP (hd :: tl) p ↔ p hd ∧ allP tl p := by simp [allP, foldr] - def pairwise_rel {α : Type u} (rel : α → α → Prop) (l: List α) : Prop := match l with -- cgit v1.2.3 From e07177ee2de3fd1346ab6b1fc09aefbcb0e24459 Mon Sep 17 00:00:00 2001 From: Son Ho Date: Tue, 18 Jul 2023 12:22:59 +0200 Subject: Improve progress --- tests/lean/Hashmap/Properties.lean | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) (limited to 'tests/lean') diff --git a/tests/lean/Hashmap/Properties.lean b/tests/lean/Hashmap/Properties.lean index 1db39422..ee22bebd 100644 --- a/tests/lean/Hashmap/Properties.lean +++ b/tests/lean/Hashmap/Properties.lean @@ -105,6 +105,8 @@ theorem insert_in_list_spec {α : Type} (key: Usize) (value: α) (ls: List α) : simp [h] else by + -- TODO: use progress: detect that this is a recursive call, or give + -- the possibility of specifying an identifier have ⟨ b, hi ⟩ := insert_in_list_spec key value tl exists b simp [insert_in_list, List.lookup] @@ -112,13 +114,12 @@ theorem insert_in_list_spec {α : Type} (key: Usize) (value: α) (ls: List α) : simp [h] simp [insert_in_list] at hi exact hi - -/-- + @[pspec] theorem insert_in_list_spec2 {α : Type} (key: Usize) (value: α) (ls: List α) : ∃ b, insert_in_list α key value ls = ret b ∧ - (b = (List.lookup ls.v key = none)) + (b ↔ (List.lookup ls.v key = none)) := by induction ls case Nil => simp [insert_in_list, insert_in_list_loop, List.lookup] @@ -130,11 +131,12 @@ theorem insert_in_list_spec2 {α : Type} (key: Usize) (value: α) (ls: List α) simp [h] else conv => - rhs; ext; arg 1; simp [h] -- TODO: Simplify - simp [insert_in_list] at ih; - progress -- TODO: Does not work ---/ - + rhs; ext; left; simp [h] -- TODO: Simplify + simp only [insert_in_list] at ih; + -- TODO: give the possibility of using underscores + progress as ⟨ b h ⟩ + simp [*] + @[pspec] theorem insert_in_list_back_spec {α : Type} (key: Usize) (value: α) (l0: List α) : ∃ l1, @@ -150,8 +152,7 @@ theorem insert_in_list_back_spec {α : Type} (key: Usize) (value: α) (l0: List rw [insert_in_list_loop_back] simp [h, List.lookup] intro k1 h1 - have h2 : ¬(key = k1) := by tauto -- TODO: Why is the order of args in eq swapped - simp [h2] + simp [*] else by simp [insert_in_list_back, List.lookup] @@ -159,12 +160,9 @@ theorem insert_in_list_back_spec {α : Type} (key: Usize) (value: α) (l0: List simp [h, List.lookup] have ⟨tl0 , _, _ ⟩ := insert_in_list_back_spec key value tl -- TODO: Use progress simp [insert_in_list_back] at * - simp [*] - have : ¬ (key = k) := by tauto simp [List.lookup, *] simp (config := {contextual := true}) [*] end HashMap --- def distinct_keys {α : Type u} end hashmap -- cgit v1.2.3 From 204742bf2449c88abaea8ebd284c55d98b43488a Mon Sep 17 00:00:00 2001 From: Son Ho Date: Wed, 19 Jul 2023 14:48:08 +0200 Subject: Improve progress --- tests/lean/Hashmap/Properties.lean | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) (limited to 'tests/lean') diff --git a/tests/lean/Hashmap/Properties.lean b/tests/lean/Hashmap/Properties.lean index ee22bebd..3b9b960f 100644 --- a/tests/lean/Hashmap/Properties.lean +++ b/tests/lean/Hashmap/Properties.lean @@ -115,6 +115,37 @@ theorem insert_in_list_spec {α : Type} (key: Usize) (value: α) (ls: List α) : simp [insert_in_list] at hi exact hi +set_option trace.Progress true +@[pspec] +theorem insert_in_list_spec1 {α : Type} (key: Usize) (value: α) (ls: List α) : + ∃ b, + insert_in_list α key value ls = ret b ∧ + (b ↔ List.lookup ls.v key = none) + := match ls with + | .Nil => by simp [insert_in_list, insert_in_list_loop, List.lookup] + | .Cons k v tl => + if h: k = key then -- TODO: The order of k/key matters + by + simp [insert_in_list, List.lookup] + rw [insert_in_list_loop] + simp [h] + else + by + simp only [insert_in_list] + rw [insert_in_list_loop] + conv => rhs; ext; simp [*] + progress as ⟨ b hi ⟩ + + -- TODO: use progress: detect that this is a recursive call, or give + -- the possibility of specifying an identifier + have ⟨ b, hi ⟩ := insert_in_list_spec key value tl + exists b + simp [insert_in_list, List.lookup] + rw [insert_in_list_loop] -- TODO: Using it in simp leads to infinite recursion + simp [h] + simp [insert_in_list] at hi + exact hi + @[pspec] theorem insert_in_list_spec2 {α : Type} (key: Usize) (value: α) (ls: List α) : ∃ b, @@ -130,8 +161,7 @@ theorem insert_in_list_spec2 {α : Type} (key: Usize) (value: α) (ls: List α) if h: k = key then simp [h] else - conv => - rhs; ext; left; simp [h] -- TODO: Simplify + conv => rhs; ext; left; simp [h] -- TODO: Simplify simp only [insert_in_list] at ih; -- TODO: give the possibility of using underscores progress as ⟨ b h ⟩ -- cgit v1.2.3 From 6ef1d360b89fd9f9383e63609888bf925a6a16ab Mon Sep 17 00:00:00 2001 From: Son Ho Date: Thu, 20 Jul 2023 12:08:09 +0200 Subject: Improve progress further and move some lemmas --- tests/lean/Hashmap/Properties.lean | 92 ++++---------------------------------- 1 file changed, 8 insertions(+), 84 deletions(-) (limited to 'tests/lean') diff --git a/tests/lean/Hashmap/Properties.lean b/tests/lean/Hashmap/Properties.lean index 3b9b960f..baa8f5c8 100644 --- a/tests/lean/Hashmap/Properties.lean +++ b/tests/lean/Hashmap/Properties.lean @@ -3,75 +3,6 @@ import Hashmap.Funs open Primitives open Result -namespace List - -theorem index_ne - {α : Type u} [Inhabited α] (l: List α) (i: ℤ) (j: ℤ) (x: α) : - 0 ≤ i → i < l.len → 0 ≤ j → j < l.len → j ≠ i → - (l.update i x).index j = l.index j - := - λ _ _ _ _ _ => match l with - | [] => by simp at * - | hd :: tl => - if h: i = 0 then - have : j ≠ 0 := by scalar_tac - by simp [*] - else if h : j = 0 then - have : i ≠ 0 := by scalar_tac - by simp [*] - else - by - simp [*] - simp at * - apply index_ne <;> scalar_tac - -theorem index_eq - {α : Type u} [Inhabited α] (l: List α) (i: ℤ) (x: α) : - 0 ≤ i → i < l.len → - (l.update i x).index i = x - := - fun _ _ => match l with - | [] => by simp at *; exfalso; scalar_tac -- TODO: exfalso needed. Son FIXME - | hd :: tl => - if h: i = 0 then - by - simp [*] - else - by - simp [*] - simp at * - apply index_eq <;> scalar_tac - -def allP {α : Type u} (l : List α) (p: α → Prop) : Prop := - foldr (fun a r => p a ∧ r) True l - -@[simp] -theorem allP_nil {α : Type u} (p: α → Prop) : allP [] p := - by simp [allP, foldr] - -@[simp] -theorem allP_cons {α : Type u} (hd: α) (tl : List α) (p: α → Prop) : - allP (hd :: tl) p ↔ p hd ∧ allP tl p - := by simp [allP, foldr] - -def pairwise_rel - {α : Type u} (rel : α → α → Prop) (l: List α) : Prop - := match l with - | [] => True - | hd :: tl => allP tl (rel hd) ∧ pairwise_rel rel tl - -@[simp] -theorem pairwise_rel_nil {α : Type u} (rel : α → α → Prop) : - pairwise_rel rel [] - := by simp [pairwise_rel] - -@[simp] -theorem pairwise_rel_cons {α : Type u} (rel : α → α → Prop) (hd: α) (tl: List α) : - pairwise_rel rel (hd :: tl) ↔ allP tl (rel hd) ∧ pairwise_rel rel tl - := by simp [pairwise_rel] - -end List - namespace hashmap namespace List @@ -104,18 +35,16 @@ theorem insert_in_list_spec {α : Type} (key: Usize) (value: α) (ls: List α) : rw [insert_in_list_loop] simp [h] else + have ⟨ b, hi ⟩ := insert_in_list_spec key value tl by - -- TODO: use progress: detect that this is a recursive call, or give - -- the possibility of specifying an identifier - have ⟨ b, hi ⟩ := insert_in_list_spec key value tl exists b simp [insert_in_list, List.lookup] - rw [insert_in_list_loop] -- TODO: Using it in simp leads to infinite recursion + rw [insert_in_list_loop] -- TODO: Using simp leads to infinite recursion simp [h] simp [insert_in_list] at hi exact hi -set_option trace.Progress true +-- Variation: use progress @[pspec] theorem insert_in_list_spec1 {α : Type} (key: Usize) (value: α) (ls: List α) : ∃ b, @@ -134,18 +63,13 @@ theorem insert_in_list_spec1 {α : Type} (key: Usize) (value: α) (ls: List α) simp only [insert_in_list] rw [insert_in_list_loop] conv => rhs; ext; simp [*] - progress as ⟨ b hi ⟩ - - -- TODO: use progress: detect that this is a recursive call, or give - -- the possibility of specifying an identifier - have ⟨ b, hi ⟩ := insert_in_list_spec key value tl + progress keep as heq as ⟨ b hi ⟩ + simp only [insert_in_list] at heq exists b - simp [insert_in_list, List.lookup] - rw [insert_in_list_loop] -- TODO: Using it in simp leads to infinite recursion - simp [h] - simp [insert_in_list] at hi - exact hi + simp only [heq, hi] + simp [*, List.lookup] +-- Variation: use tactics from the beginning @[pspec] theorem insert_in_list_spec2 {α : Type} (key: Usize) (value: α) (ls: List α) : ∃ b, -- cgit v1.2.3 From 03492250b45855fe9db5e0a28a96166607cd84a1 Mon Sep 17 00:00:00 2001 From: Son Ho Date: Thu, 20 Jul 2023 14:14:34 +0200 Subject: Make some proofs in Hashmap/Properties.lean and improve progress --- tests/lean/Hashmap/Properties.lean | 76 ++++++++++++++++++++++++++++++++------ 1 file changed, 65 insertions(+), 11 deletions(-) (limited to 'tests/lean') diff --git a/tests/lean/Hashmap/Properties.lean b/tests/lean/Hashmap/Properties.lean index baa8f5c8..e065bb36 100644 --- a/tests/lean/Hashmap/Properties.lean +++ b/tests/lean/Hashmap/Properties.lean @@ -21,8 +21,9 @@ end List namespace HashMap -@[pspec] -theorem insert_in_list_spec {α : Type} (key: Usize) (value: α) (ls: List α) : +abbrev Core.List := _root_.List + +theorem insert_in_list_spec0 {α : Type} (key: Usize) (value: α) (ls: List α) : ∃ b, insert_in_list α key value ls = ret b ∧ (b ↔ List.lookup ls.v key = none) @@ -35,7 +36,7 @@ theorem insert_in_list_spec {α : Type} (key: Usize) (value: α) (ls: List α) : rw [insert_in_list_loop] simp [h] else - have ⟨ b, hi ⟩ := insert_in_list_spec key value tl + have ⟨ b, hi ⟩ := insert_in_list_spec0 key value tl by exists b simp [insert_in_list, List.lookup] @@ -45,7 +46,6 @@ theorem insert_in_list_spec {α : Type} (key: Usize) (value: α) (ls: List α) : exact hi -- Variation: use progress -@[pspec] theorem insert_in_list_spec1 {α : Type} (key: Usize) (value: α) (ls: List α) : ∃ b, insert_in_list α key value ls = ret b ∧ @@ -70,7 +70,6 @@ theorem insert_in_list_spec1 {α : Type} (key: Usize) (value: α) (ls: List α) simp [*, List.lookup] -- Variation: use tactics from the beginning -@[pspec] theorem insert_in_list_spec2 {α : Type} (key: Usize) (value: α) (ls: List α) : ∃ b, insert_in_list α key value ls = ret b ∧ @@ -91,10 +90,10 @@ theorem insert_in_list_spec2 {α : Type} (key: Usize) (value: α) (ls: List α) progress as ⟨ b h ⟩ simp [*] -@[pspec] theorem insert_in_list_back_spec {α : Type} (key: Usize) (value: α) (l0: List α) : ∃ l1, insert_in_list_back α key value l0 = ret l1 ∧ + -- We update the binding List.lookup l1.v key = value ∧ (∀ k, k ≠ key → List.lookup l1.v k = List.lookup l0.v k) := match l0 with @@ -112,11 +111,66 @@ theorem insert_in_list_back_spec {α : Type} (key: Usize) (value: α) (l0: List simp [insert_in_list_back, List.lookup] rw [insert_in_list_loop_back] simp [h, List.lookup] - have ⟨tl0 , _, _ ⟩ := insert_in_list_back_spec key value tl -- TODO: Use progress - simp [insert_in_list_back] at * - simp [List.lookup, *] - simp (config := {contextual := true}) [*] - + progress keep as heq as ⟨ tl hp1 hp2 ⟩ + simp [insert_in_list_back] at heq + simp (config := {contextual := true}) [*, List.lookup] + +def distinct_keys (ls : Core.List (Usize × α)) := ls.pairwise_rel (λ x y => x.fst ≠ y.fst) + +def hash_mod_key (k : Usize) (l : Int) : Int := + match hash_key k with + | .ret k => k.val % l + | _ => 0 + +def slot_s_inv_hash (l i : Int) (ls : Core.List (Usize × α)) : Prop := + ls.allP (λ (k, _) => hash_mod_key k l = i) + +@[simp] +def slot_s_inv (l i : Int) (ls : Core.List (Usize × α)) : Prop := + distinct_keys ls ∧ + slot_s_inv_hash l i ls + +def slot_t_inv (l i : Int) (s : List α) : Prop := slot_s_inv l i s.v + +@[pspec] +theorem insert_in_list_back_spec1 {α : Type} (l : Int) (key: Usize) (value: α) (l0: List α) + (hinv : slot_s_inv_hash l (hash_mod_key key l) l0.v) : + ∃ l1, + insert_in_list_back α key value l0 = ret l1 ∧ + -- We update the binding + List.lookup l1.v key = value ∧ + (∀ k, k ≠ key → List.lookup l1.v k = List.lookup l0.v k) ∧ + -- We preserve part of the key invariant + slot_s_inv_hash l (hash_mod_key key l) l1.v + := match l0 with + | .Nil => by + simp [insert_in_list_back, insert_in_list_loop_back, List.lookup, List.v, slot_s_inv_hash] + tauto + | .Cons k v tl0 => + if h: k = key then + by + simp [insert_in_list_back, List.lookup] + rw [insert_in_list_loop_back] + simp [h, List.lookup] + constructor + . intros; simp [*] + . simp [List.v, slot_s_inv_hash] at * + simp [*] + else + by + simp [insert_in_list_back, List.lookup] + rw [insert_in_list_loop_back] + simp [h, List.lookup] + have : slot_s_inv_hash l (hash_mod_key key l) (List.v tl0) := by + simp_all [List.v, slot_s_inv_hash] + progress keep as heq as ⟨ tl1 hp1 hp2 hp3 ⟩ + simp only [insert_in_list_back] at heq + have : slot_s_inv_hash l (hash_mod_key key l) (List.v (List.Cons k v tl1)) := by + simp [List.v, slot_s_inv_hash] at * + simp [*] + simp (config := {contextual := true}) [*, List.lookup] + + end HashMap end hashmap -- cgit v1.2.3 From e58872aa4dc31f0819fe17b13e6b7e4b0d9635c8 Mon Sep 17 00:00:00 2001 From: Son Ho Date: Thu, 20 Jul 2023 15:46:11 +0200 Subject: Make progress on some of the hashmap proofs --- tests/lean/Hashmap/Properties.lean | 167 ++++++++++++++++++++++++++++--------- 1 file changed, 127 insertions(+), 40 deletions(-) (limited to 'tests/lean') diff --git a/tests/lean/Hashmap/Properties.lean b/tests/lean/Hashmap/Properties.lean index e065bb36..66c386f2 100644 --- a/tests/lean/Hashmap/Properties.lean +++ b/tests/lean/Hashmap/Properties.lean @@ -3,6 +3,18 @@ import Hashmap.Funs open Primitives open Result +namespace List + +-- TODO: we don't want to use the original List.lookup because it uses BEq +-- TODO: rewrite rule: match x == y with ... -> if x = y then ... else ... ? +@[simp] +def lookup' {α : Type} (ls: _root_.List (Usize × α)) (key: Usize) : Option α := + match ls with + | [] => none + | (k, x) :: tl => if k = key then some x else lookup' tl key + +end List + namespace hashmap namespace List @@ -12,10 +24,15 @@ def v {α : Type} (ls: List α) : _root_.List (Usize × α) := | Nil => [] | Cons k x tl => (k, x) :: v tl -def lookup {α : Type} (ls: _root_.List (Usize × α)) (key: Usize) : Option α := - match ls with - | [] => none - | (k, x) :: tl => if k = key then some x else lookup tl key +@[simp] theorem v_nil (α : Type) : (Nil : List α).v = [] := by rfl +@[simp] theorem v_cons {α : Type} k x (tl: List α) : (Cons k x tl).v = (k, x) :: v tl := by rfl + +@[simp] +abbrev lookup {α : Type} (ls: List α) (key: Usize) : Option α := + ls.v.lookup' key + +@[simp] +abbrev len {α : Type} (ls : List α) : Int := ls.v.len end List @@ -23,39 +40,51 @@ namespace HashMap abbrev Core.List := _root_.List +namespace List + +end List + +-- TODO: move +@[simp] theorem neq_imp_nbeq [BEq α] [LawfulBEq α] (x y : α) (heq : ¬ x = y) : ¬ x == y := by simp [*] +@[simp] theorem neq_imp_nbeq_rev [BEq α] [LawfulBEq α] (x y : α) (heq : ¬ x = y) : ¬ y == x := by simp [*] + +-- TODO: move +theorem match_lawful_beq [BEq α] [LawfulBEq α] [DecidableEq α] (x y : α) : + (x == y) = (if x = y then true else false) := by + split <;> simp_all + theorem insert_in_list_spec0 {α : Type} (key: Usize) (value: α) (ls: List α) : ∃ b, insert_in_list α key value ls = ret b ∧ - (b ↔ List.lookup ls.v key = none) + (b ↔ ls.lookup key = none) := match ls with - | .Nil => by simp [insert_in_list, insert_in_list_loop, List.lookup] + | .Nil => by simp [insert_in_list, insert_in_list_loop] | .Cons k v tl => if h: k = key then -- TODO: The order of k/key matters by - simp [insert_in_list, List.lookup] + simp [insert_in_list] rw [insert_in_list_loop] simp [h] else have ⟨ b, hi ⟩ := insert_in_list_spec0 key value tl by exists b - simp [insert_in_list, List.lookup] + simp [insert_in_list] rw [insert_in_list_loop] -- TODO: Using simp leads to infinite recursion - simp [h] - simp [insert_in_list] at hi - exact hi + simp only [insert_in_list] at hi + simp [*] -- Variation: use progress theorem insert_in_list_spec1 {α : Type} (key: Usize) (value: α) (ls: List α) : ∃ b, insert_in_list α key value ls = ret b ∧ - (b ↔ List.lookup ls.v key = none) + (b ↔ ls.lookup key = none) := match ls with - | .Nil => by simp [insert_in_list, insert_in_list_loop, List.lookup] + | .Nil => by simp [insert_in_list, insert_in_list_loop] | .Cons k v tl => if h: k = key then -- TODO: The order of k/key matters by - simp [insert_in_list, List.lookup] + simp [insert_in_list] rw [insert_in_list_loop] simp [h] else @@ -66,19 +95,17 @@ theorem insert_in_list_spec1 {α : Type} (key: Usize) (value: α) (ls: List α) progress keep as heq as ⟨ b hi ⟩ simp only [insert_in_list] at heq exists b - simp only [heq, hi] - simp [*, List.lookup] -- Variation: use tactics from the beginning theorem insert_in_list_spec2 {α : Type} (key: Usize) (value: α) (ls: List α) : ∃ b, insert_in_list α key value ls = ret b ∧ - (b ↔ (List.lookup ls.v key = none)) + (b ↔ (ls.lookup key = none)) := by induction ls - case Nil => simp [insert_in_list, insert_in_list_loop, List.lookup] + case Nil => simp [insert_in_list, insert_in_list_loop] case Cons k v tl ih => - simp only [insert_in_list, List.lookup] + simp only [insert_in_list] rw [insert_in_list_loop] simp only if h: k = key then @@ -94,26 +121,27 @@ theorem insert_in_list_back_spec {α : Type} (key: Usize) (value: α) (l0: List ∃ l1, insert_in_list_back α key value l0 = ret l1 ∧ -- We update the binding - List.lookup l1.v key = value ∧ - (∀ k, k ≠ key → List.lookup l1.v k = List.lookup l0.v k) + l1.lookup key = value ∧ + (∀ k, k ≠ key → l1.lookup k = l0.lookup k) := match l0 with - | .Nil => by simp [insert_in_list_back, insert_in_list_loop_back, List.lookup]; tauto + | .Nil => by + simp (config := {contextual := true}) [insert_in_list_back, insert_in_list_loop_back] | .Cons k v tl => if h: k = key then by - simp [insert_in_list_back, List.lookup] + simp [insert_in_list_back] rw [insert_in_list_loop_back] - simp [h, List.lookup] + simp [h] intro k1 h1 simp [*] else by - simp [insert_in_list_back, List.lookup] + simp [insert_in_list_back] rw [insert_in_list_loop_back] - simp [h, List.lookup] + simp [h] progress keep as heq as ⟨ tl hp1 hp2 ⟩ simp [insert_in_list_back] at heq - simp (config := {contextual := true}) [*, List.lookup] + simp (config := {contextual := true}) [*] def distinct_keys (ls : Core.List (Usize × α)) := ls.pairwise_rel (λ x y => x.fst ≠ y.fst) @@ -132,44 +160,103 @@ def slot_s_inv (l i : Int) (ls : Core.List (Usize × α)) : Prop := def slot_t_inv (l i : Int) (s : List α) : Prop := slot_s_inv l i s.v -@[pspec] -theorem insert_in_list_back_spec1 {α : Type} (l : Int) (key: Usize) (value: α) (l0: List α) +theorem insert_in_list_back_spec_aux {α : Type} (l : Int) (key: Usize) (value: α) (l0: List α) (hinv : slot_s_inv_hash l (hash_mod_key key l) l0.v) : ∃ l1, insert_in_list_back α key value l0 = ret l1 ∧ -- We update the binding - List.lookup l1.v key = value ∧ - (∀ k, k ≠ key → List.lookup l1.v k = List.lookup l0.v k) ∧ + l1.lookup key = value ∧ + (∀ k, k ≠ key → l1.lookup k = l0.lookup k) ∧ -- We preserve part of the key invariant - slot_s_inv_hash l (hash_mod_key key l) l1.v + slot_s_inv_hash l (hash_mod_key key l) l1.v ∧ + -- Reasoning about the length + match l0.lookup key with + | none => l1.len = l0.len + 1 + | some _ => l1.len = l0.len := match l0 with | .Nil => by - simp [insert_in_list_back, insert_in_list_loop_back, List.lookup, List.v, slot_s_inv_hash] - tauto + simp (config := {contextual := true}) [insert_in_list_back, insert_in_list_loop_back, List.v, slot_s_inv_hash] | .Cons k v tl0 => if h: k = key then by - simp [insert_in_list_back, List.lookup] + simp [insert_in_list_back] rw [insert_in_list_loop_back] - simp [h, List.lookup] + simp [h] constructor . intros; simp [*] . simp [List.v, slot_s_inv_hash] at * simp [*] else by - simp [insert_in_list_back, List.lookup] + simp [insert_in_list_back] rw [insert_in_list_loop_back] - simp [h, List.lookup] + simp [h] have : slot_s_inv_hash l (hash_mod_key key l) (List.v tl0) := by simp_all [List.v, slot_s_inv_hash] - progress keep as heq as ⟨ tl1 hp1 hp2 hp3 ⟩ + progress keep as heq as ⟨ tl1 hp1 hp2 hp3 hp4 ⟩ simp only [insert_in_list_back] at heq have : slot_s_inv_hash l (hash_mod_key key l) (List.v (List.Cons k v tl1)) := by simp [List.v, slot_s_inv_hash] at * simp [*] - simp (config := {contextual := true}) [*, List.lookup] + -- TODO: canonize addition by default? + simp_all [Int.add_assoc, Int.add_comm, Int.add_left_comm] +theorem insert_in_list_back_spec_aux1 {α : Type} (l : Int) (key: Usize) (value: α) (l0: List α) + (hinv : slot_s_inv_hash l (hash_mod_key key l) l0.v) + (hdk : distinct_keys l0.v) : + ∃ l1, + insert_in_list_back α key value l0 = ret l1 ∧ + -- We update the binding + l1.lookup key = value ∧ + (∀ k, k ≠ key → l1.lookup k = l0.lookup k) ∧ + -- We preserve part of the key invariant + slot_s_inv_hash l (hash_mod_key key l) l1.v ∧ + -- Reasoning about the length + (match l0.lookup key with + | none => l1.len = l0.len + 1 + | some _ => l1.len = l0.len) ∧ + -- The keys are distinct + distinct_keys l1.v ∧ + -- We need this auxiliary property to prove that the keys distinct properties is preserved + (∀ k, k ≠ key → l0.v.allP (λ (k1, _) => k ≠ k1) → l1.v.allP (λ (k1, _) => k ≠ k1)) + := match l0 with + | .Nil => by + simp (config := {contextual := true}) + [insert_in_list_back, insert_in_list_loop_back, + List.v, slot_s_inv_hash, distinct_keys, List.pairwise_rel] + | .Cons k v tl0 => + if h: k = key then + by + simp [insert_in_list_back] + rw [insert_in_list_loop_back] + simp [h] + split_target_conjs + . intros; simp [*] + . simp [List.v, slot_s_inv_hash] at * + simp [*] + . simp [*, distinct_keys] at * + apply hdk + . tauto + else + by + simp [insert_in_list_back] + rw [insert_in_list_loop_back] + simp [h] + have : slot_s_inv_hash l (hash_mod_key key l) (List.v tl0) := by + simp_all [List.v, slot_s_inv_hash] + have : distinct_keys (List.v tl0) := by + simp [distinct_keys] at hdk + simp [hdk, distinct_keys] + progress keep as heq as ⟨ tl1 hp1 hp2 hp3 hp4 hp5 hp6 ⟩ -- TODO: naming is weird + simp only [insert_in_list_back] at heq + have : slot_s_inv_hash l (hash_mod_key key l) (List.v (List.Cons k v tl1)) := by + simp [List.v, slot_s_inv_hash] at * + simp [*] + have : distinct_keys ((k, v) :: List.v tl1) := by + simp [distinct_keys] at * + simp [*] + -- TODO: canonize addition by default? + simp_all [Int.add_assoc, Int.add_comm, Int.add_left_comm] end HashMap -- cgit v1.2.3 From c5536f372194240d164583cecee5265213b3e671 Mon Sep 17 00:00:00 2001 From: Son Ho Date: Thu, 20 Jul 2023 16:13:47 +0200 Subject: Improve the interactivity of some hashmap proofs --- tests/lean/Hashmap/Properties.lean | 109 ++++++++++++------------------------- 1 file changed, 34 insertions(+), 75 deletions(-) (limited to 'tests/lean') diff --git a/tests/lean/Hashmap/Properties.lean b/tests/lean/Hashmap/Properties.lean index 66c386f2..dce33fa4 100644 --- a/tests/lean/Hashmap/Properties.lean +++ b/tests/lean/Hashmap/Properties.lean @@ -6,7 +6,8 @@ open Result namespace List -- TODO: we don't want to use the original List.lookup because it uses BEq --- TODO: rewrite rule: match x == y with ... -> if x = y then ... else ... ? +-- TODO: rewrite rule: match x == y with ... -> if x = y then ... else ... ? (actually doesn't work because of sugar) +-- TODO: move? @[simp] def lookup' {α : Type} (ls: _root_.List (Usize × α)) (key: Usize) : Option α := match ls with @@ -49,6 +50,7 @@ end List @[simp] theorem neq_imp_nbeq_rev [BEq α] [LawfulBEq α] (x y : α) (heq : ¬ x = y) : ¬ y == x := by simp [*] -- TODO: move +-- TODO: this doesn't work because of sugar theorem match_lawful_beq [BEq α] [LawfulBEq α] [DecidableEq α] (x y : α) : (x == y) = (if x = y then true else false) := by split <;> simp_all @@ -161,47 +163,6 @@ def slot_s_inv (l i : Int) (ls : Core.List (Usize × α)) : Prop := def slot_t_inv (l i : Int) (s : List α) : Prop := slot_s_inv l i s.v theorem insert_in_list_back_spec_aux {α : Type} (l : Int) (key: Usize) (value: α) (l0: List α) - (hinv : slot_s_inv_hash l (hash_mod_key key l) l0.v) : - ∃ l1, - insert_in_list_back α key value l0 = ret l1 ∧ - -- We update the binding - l1.lookup key = value ∧ - (∀ k, k ≠ key → l1.lookup k = l0.lookup k) ∧ - -- We preserve part of the key invariant - slot_s_inv_hash l (hash_mod_key key l) l1.v ∧ - -- Reasoning about the length - match l0.lookup key with - | none => l1.len = l0.len + 1 - | some _ => l1.len = l0.len - := match l0 with - | .Nil => by - simp (config := {contextual := true}) [insert_in_list_back, insert_in_list_loop_back, List.v, slot_s_inv_hash] - | .Cons k v tl0 => - if h: k = key then - by - simp [insert_in_list_back] - rw [insert_in_list_loop_back] - simp [h] - constructor - . intros; simp [*] - . simp [List.v, slot_s_inv_hash] at * - simp [*] - else - by - simp [insert_in_list_back] - rw [insert_in_list_loop_back] - simp [h] - have : slot_s_inv_hash l (hash_mod_key key l) (List.v tl0) := by - simp_all [List.v, slot_s_inv_hash] - progress keep as heq as ⟨ tl1 hp1 hp2 hp3 hp4 ⟩ - simp only [insert_in_list_back] at heq - have : slot_s_inv_hash l (hash_mod_key key l) (List.v (List.Cons k v tl1)) := by - simp [List.v, slot_s_inv_hash] at * - simp [*] - -- TODO: canonize addition by default? - simp_all [Int.add_assoc, Int.add_comm, Int.add_left_comm] - -theorem insert_in_list_back_spec_aux1 {α : Type} (l : Int) (key: Usize) (value: α) (l0: List α) (hinv : slot_s_inv_hash l (hash_mod_key key l) l0.v) (hdk : distinct_keys l0.v) : ∃ l1, @@ -220,43 +181,41 @@ theorem insert_in_list_back_spec_aux1 {α : Type} (l : Int) (key: Usize) (value: -- We need this auxiliary property to prove that the keys distinct properties is preserved (∀ k, k ≠ key → l0.v.allP (λ (k1, _) => k ≠ k1) → l1.v.allP (λ (k1, _) => k ≠ k1)) := match l0 with - | .Nil => by + | .Nil => by checkpoint simp (config := {contextual := true}) [insert_in_list_back, insert_in_list_loop_back, List.v, slot_s_inv_hash, distinct_keys, List.pairwise_rel] | .Cons k v tl0 => - if h: k = key then - by - simp [insert_in_list_back] - rw [insert_in_list_loop_back] - simp [h] - split_target_conjs - . intros; simp [*] - . simp [List.v, slot_s_inv_hash] at * - simp [*] - . simp [*, distinct_keys] at * - apply hdk - . tauto - else - by - simp [insert_in_list_back] - rw [insert_in_list_loop_back] - simp [h] - have : slot_s_inv_hash l (hash_mod_key key l) (List.v tl0) := by - simp_all [List.v, slot_s_inv_hash] - have : distinct_keys (List.v tl0) := by - simp [distinct_keys] at hdk - simp [hdk, distinct_keys] - progress keep as heq as ⟨ tl1 hp1 hp2 hp3 hp4 hp5 hp6 ⟩ -- TODO: naming is weird - simp only [insert_in_list_back] at heq - have : slot_s_inv_hash l (hash_mod_key key l) (List.v (List.Cons k v tl1)) := by - simp [List.v, slot_s_inv_hash] at * - simp [*] - have : distinct_keys ((k, v) :: List.v tl1) := by - simp [distinct_keys] at * - simp [*] - -- TODO: canonize addition by default? - simp_all [Int.add_assoc, Int.add_comm, Int.add_left_comm] + if h: k = key then by checkpoint + simp [insert_in_list_back] + rw [insert_in_list_loop_back] + simp [h] + split_target_conjs + . intros; simp [*] + . simp [List.v, slot_s_inv_hash] at * + simp [*] + . simp [*, distinct_keys] at * + apply hdk + . tauto + else by checkpoint + simp [insert_in_list_back] + rw [insert_in_list_loop_back] + simp [h] + have : slot_s_inv_hash l (hash_mod_key key l) (List.v tl0) := by checkpoint + simp_all [List.v, slot_s_inv_hash] + have : distinct_keys (List.v tl0) := by checkpoint + simp [distinct_keys] at hdk + simp [hdk, distinct_keys] + progress keep as heq as ⟨ tl1 hp1 hp2 hp3 hp4 hp5 hp6 ⟩ + simp only [insert_in_list_back] at heq + have : slot_s_inv_hash l (hash_mod_key key l) (List.v (List.Cons k v tl1)) := by checkpoint + simp [List.v, slot_s_inv_hash] at * + simp [*] + have : distinct_keys ((k, v) :: List.v tl1) := by checkpoint + simp [distinct_keys] at * + simp [*] + -- TODO: canonize addition by default? + simp_all [Int.add_assoc, Int.add_comm, Int.add_left_comm] end HashMap -- cgit v1.2.3 From 2dd56a51df01421fe7766858c9d37998db4123b5 Mon Sep 17 00:00:00 2001 From: Son Ho Date: Tue, 25 Jul 2023 11:53:49 +0200 Subject: Improve the syntax of progress: `as ⟨ x, y .. ⟩` --- tests/lean/Hashmap/Properties.lean | 34 ++++------------------------------ 1 file changed, 4 insertions(+), 30 deletions(-) (limited to 'tests/lean') diff --git a/tests/lean/Hashmap/Properties.lean b/tests/lean/Hashmap/Properties.lean index dce33fa4..de6bf636 100644 --- a/tests/lean/Hashmap/Properties.lean +++ b/tests/lean/Hashmap/Properties.lean @@ -94,7 +94,7 @@ theorem insert_in_list_spec1 {α : Type} (key: Usize) (value: α) (ls: List α) simp only [insert_in_list] rw [insert_in_list_loop] conv => rhs; ext; simp [*] - progress keep as heq as ⟨ b hi ⟩ + progress keep as heq as ⟨ b, hi ⟩ simp only [insert_in_list] at heq exists b @@ -116,35 +116,9 @@ theorem insert_in_list_spec2 {α : Type} (key: Usize) (value: α) (ls: List α) conv => rhs; ext; left; simp [h] -- TODO: Simplify simp only [insert_in_list] at ih; -- TODO: give the possibility of using underscores - progress as ⟨ b h ⟩ + progress as ⟨ b, h ⟩ simp [*] -theorem insert_in_list_back_spec {α : Type} (key: Usize) (value: α) (l0: List α) : - ∃ l1, - insert_in_list_back α key value l0 = ret l1 ∧ - -- We update the binding - l1.lookup key = value ∧ - (∀ k, k ≠ key → l1.lookup k = l0.lookup k) - := match l0 with - | .Nil => by - simp (config := {contextual := true}) [insert_in_list_back, insert_in_list_loop_back] - | .Cons k v tl => - if h: k = key then - by - simp [insert_in_list_back] - rw [insert_in_list_loop_back] - simp [h] - intro k1 h1 - simp [*] - else - by - simp [insert_in_list_back] - rw [insert_in_list_loop_back] - simp [h] - progress keep as heq as ⟨ tl hp1 hp2 ⟩ - simp [insert_in_list_back] at heq - simp (config := {contextual := true}) [*] - def distinct_keys (ls : Core.List (Usize × α)) := ls.pairwise_rel (λ x y => x.fst ≠ y.fst) def hash_mod_key (k : Usize) (l : Int) : Int := @@ -190,7 +164,7 @@ theorem insert_in_list_back_spec_aux {α : Type} (l : Int) (key: Usize) (value: simp [insert_in_list_back] rw [insert_in_list_loop_back] simp [h] - split_target_conjs + split_conjs . intros; simp [*] . simp [List.v, slot_s_inv_hash] at * simp [*] @@ -206,7 +180,7 @@ theorem insert_in_list_back_spec_aux {α : Type} (l : Int) (key: Usize) (value: have : distinct_keys (List.v tl0) := by checkpoint simp [distinct_keys] at hdk simp [hdk, distinct_keys] - progress keep as heq as ⟨ tl1 hp1 hp2 hp3 hp4 hp5 hp6 ⟩ + progress keep as heq as ⟨ tl1 .. ⟩ simp only [insert_in_list_back] at heq have : slot_s_inv_hash l (hash_mod_key key l) (List.v (List.Cons k v tl1)) := by checkpoint simp [List.v, slot_s_inv_hash] at * -- cgit v1.2.3 From 1854c631a6a7a3f8d45ad18e05547f9d3782c3ee Mon Sep 17 00:00:00 2001 From: Son Ho Date: Tue, 25 Jul 2023 16:26:08 +0200 Subject: Make progress on the hashmap properties --- tests/lean/Hashmap/Properties.lean | 92 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) (limited to 'tests/lean') diff --git a/tests/lean/Hashmap/Properties.lean b/tests/lean/Hashmap/Properties.lean index de6bf636..b2d5570a 100644 --- a/tests/lean/Hashmap/Properties.lean +++ b/tests/lean/Hashmap/Properties.lean @@ -136,6 +136,40 @@ def slot_s_inv (l i : Int) (ls : Core.List (Usize × α)) : Prop := def slot_t_inv (l i : Int) (s : List α) : Prop := slot_s_inv l i s.v +-- Interpret the hashmap as a list of lists +def v (hm : HashMap α) : Core.List (Core.List (Usize × α)) := + hm.slots.val.map List.v + +-- Interpret the hashmap as an associative list +def al_v (hm : HashMap α) : Core.List (Usize × α) := + hm.v.flatten + +-- TODO: automatic derivation +instance : Inhabited (List α) where + default := .Nil + +@[simp] +def slots_s_inv (s : Core.List (List α)) : Prop := + ∀ (i : Int), 0 ≤ i → i < s.len → slot_t_inv s.len i (s.index i) + +def slots_t_inv (s : Vec (List α)) : Prop := + slots_s_inv s.v + +@[simp] +def base_inv (hm : HashMap α) : Prop := + -- [num_entries] correctly tracks the number of entries + hm.num_entries.val = hm.al_v.len ∧ + -- Slots invariant + slots_t_inv hm.slots ∧ + -- The capacity must be > 0 (otherwise we can't resize) + 0 < hm.slots.length + -- TODO: load computation + +def inv (hm : HashMap α) : Prop := + -- Base invariant + base_inv hm + -- TODO: either the hashmap is not overloaded, or we can't resize it + theorem insert_in_list_back_spec_aux {α : Type} (l : Int) (key: Usize) (value: α) (l0: List α) (hinv : slot_s_inv_hash l (hash_mod_key key l) l0.v) (hdk : distinct_keys l0.v) : @@ -191,6 +225,64 @@ theorem insert_in_list_back_spec_aux {α : Type} (l : Int) (key: Usize) (value: -- TODO: canonize addition by default? simp_all [Int.add_assoc, Int.add_comm, Int.add_left_comm] +@[pspec] +theorem insert_in_list_back_spec {α : Type} (l : Int) (key: Usize) (value: α) (l0: List α) + (hinv : slot_s_inv_hash l (hash_mod_key key l) l0.v) + (hdk : distinct_keys l0.v) : + ∃ l1, + insert_in_list_back α key value l0 = ret l1 ∧ + -- We update the binding + l1.lookup key = value ∧ + (∀ k, k ≠ key → l1.lookup k = l0.lookup k) ∧ + -- We preserve part of the key invariant + slot_s_inv_hash l (hash_mod_key key l) l1.v ∧ + -- Reasoning about the length + (match l0.lookup key with + | none => l1.len = l0.len + 1 + | some _ => l1.len = l0.len) ∧ + -- The keys are distinct + distinct_keys l1.v + := by + progress with insert_in_list_back_spec_aux as ⟨ l1 .. ⟩ + exists l1 + +def slots_t_lookup (s : Core.List (List α)) (k : Usize) : Option α := + let i := hash_mod_key k s.len + let slot := s.index i + slot.lookup k + +def lookup (hm : HashMap α) (k : Usize) : Option α := + slots_t_lookup hm.slots.val k + +@[simp] +abbrev len_s (hm : HashMap α) : Int := hm.al_v.len + +set_option trace.Progress true +/-set_option pp.explicit true +set_option pp.universes true +set_option pp.notation false-/ + +theorem insert_no_resize_spec {α : Type} (hm : HashMap α) (key : Usize) (value : α) + (hinv : hm.inv) (hnsat : hm.lookup key = none → hm.len_s < Usize.max) : + ∃ nhm, hm.insert_no_resize α key value = ret nhm ∧ + -- We preserve the invariant + nhm.inv ∧ + -- We updated the binding for key + nhm.lookup key = some value ∧ + -- We left the other bindings unchanged + (∀ k, k ≠ key → nhm.lookup k = hm.lookup k) ∧ + -- Reasoning about the length + (match hm.lookup key with + | none => nhm.len_s = hm.len_s + 1 + | some _ => nhm.len_s = hm.len_s) := by + rw [insert_no_resize] + simp [hash_key] + have : (Vec.len (List α) hm.slots).val ≠ 0 := by + intro + simp_all [inv] + progress as ⟨ hash_mod ⟩ + progress + end HashMap end hashmap -- cgit v1.2.3 From 0cc3c78137434d848188eee2a66b1e2cacfd102e Mon Sep 17 00:00:00 2001 From: Son Ho Date: Tue, 25 Jul 2023 19:06:05 +0200 Subject: Make progress on the proofs of the hashmap --- tests/lean/Hashmap/Properties.lean | 116 +++++++++++++++++++++++++++++++++++-- 1 file changed, 112 insertions(+), 4 deletions(-) (limited to 'tests/lean') diff --git a/tests/lean/Hashmap/Properties.lean b/tests/lean/Hashmap/Properties.lean index b2d5570a..92285c0d 100644 --- a/tests/lean/Hashmap/Properties.lean +++ b/tests/lean/Hashmap/Properties.lean @@ -55,6 +55,7 @@ theorem match_lawful_beq [BEq α] [LawfulBEq α] [DecidableEq α] (x y : α) : (x == y) = (if x = y then true else false) := by split <;> simp_all +@[pspec] theorem insert_in_list_spec0 {α : Type} (key: Usize) (value: α) (ls: List α) : ∃ b, insert_in_list α key value ls = ret b ∧ @@ -126,6 +127,10 @@ def hash_mod_key (k : Usize) (l : Int) : Int := | .ret k => k.val % l | _ => 0 +@[simp] +theorem hash_mod_key_eq : hash_mod_key k l = k.val % l := by + simp [hash_mod_key, hash_key] + def slot_s_inv_hash (l i : Int) (ls : Core.List (Usize × α)) : Prop := ls.allP (λ (k, _) => hash_mod_key k l = i) @@ -246,6 +251,7 @@ theorem insert_in_list_back_spec {α : Type} (l : Int) (key: Usize) (value: α) progress with insert_in_list_back_spec_aux as ⟨ l1 .. ⟩ exists l1 +@[simp] def slots_t_lookup (s : Core.List (List α)) (k : Usize) : Option α := let i := hash_mod_key k s.len let slot := s.index i @@ -260,7 +266,15 @@ abbrev len_s (hm : HashMap α) : Int := hm.al_v.len set_option trace.Progress true /-set_option pp.explicit true set_option pp.universes true -set_option pp.notation false-/ +set_option pp.notation false -/ + +-- Remark: α and β must live in the same universe, otherwise the +-- bind doesn't work +theorem if_update_eq + {α β : Type u} (b : Bool) (y : α) (e : Result α) (f : α → Result β) : + (if b then Bind.bind e f else f y) = Bind.bind (if b then e else pure y) f + := by + split <;> simp [Pure.pure] theorem insert_no_resize_spec {α : Type} (hm : HashMap α) (key : Usize) (value : α) (hinv : hm.inv) (hnsat : hm.lookup key = none → hm.len_s < Usize.max) : @@ -270,18 +284,112 @@ theorem insert_no_resize_spec {α : Type} (hm : HashMap α) (key : Usize) (value -- We updated the binding for key nhm.lookup key = some value ∧ -- We left the other bindings unchanged - (∀ k, k ≠ key → nhm.lookup k = hm.lookup k) ∧ + (∀ k, ¬ k = key → nhm.lookup k = hm.lookup k) ∧ -- Reasoning about the length (match hm.lookup key with | none => nhm.len_s = hm.len_s + 1 | some _ => nhm.len_s = hm.len_s) := by rw [insert_no_resize] simp [hash_key] - have : (Vec.len (List α) hm.slots).val ≠ 0 := by + have _ : (Vec.len (List α) hm.slots).val ≠ 0 := by checkpoint intro simp_all [inv] - progress as ⟨ hash_mod ⟩ + -- TODO: progress keep as ⟨ ... ⟩ : conflict + progress keep as h as ⟨ hash_mod, hhm ⟩ + have _ : 0 ≤ hash_mod.val := by checkpoint scalar_tac + have _ : hash_mod.val < Vec.length hm.slots := by sorry + -- have h := Primitives.Vec.index_mut_spec hm.slots hash_mod + -- TODO: change the spec of Vec.index_mut to introduce a let-binding. + -- or: make progress introduce the let-binding by itself (this is clearer) progress + -- TODO: make progress use the names written in the goal + progress as ⟨ inserted ⟩ + rw [if_update_eq] -- TODO: necessary because we don't have a join + -- TODO: progress to ... + have hipost : + ∃ i0, (if inserted = true then hm.num_entries + Usize.ofInt 1 else pure hm.num_entries) = ret i0 ∧ + i0.val = if inserted then hm.num_entries.val + 1 else hm.num_entries.val + := by sorry + progress as ⟨ i0 ⟩ + -- TODO: progress "eager" to match premises with assumptions while instantiating + -- meta-variables + have h_slot : slot_s_inv_hash hm.slots.length hash_mod.val (hm.slots.v.index hash_mod.val).v := by sorry + have hd : distinct_keys (hm.slots.v.index hash_mod.val).v := by checkpoint + simp [inv, slots_t_inv, slot_t_inv] at hinv + have h := hinv.right.left hash_mod.val (by assumption) (by assumption) + simp [h] + -- TODO: hide the variables and only keep the props + -- TODO: allow providing terms to progress to instantiate the meta variables + -- which are not propositions + progress as ⟨ l0, _, _, _, hlen .. ⟩ + . checkpoint exact hm.slots.length + . checkpoint simp_all + . -- Finishing the proof + progress as ⟨ v ⟩ + -- TODO: update progress to automate that + let nhm : HashMap α := { num_entries := i0, max_load_factor := hm.max_load_factor, max_load := hm.max_load, slots := v } + exists nhm + have hupdt : lookup nhm key = some value := by checkpoint + simp [lookup, List.lookup] at * + simp_all + have hlkp : ∀ k, ¬ k = key → nhm.lookup k = hm.lookup k := by checkpoint + simp [lookup, List.lookup] at * + intro k hk + -- We have to make a case disjunction: either the hashes are different, + -- in which case we don't even lookup the same slots, or the hashes + -- are the same, in which case we have to reason about what happens + -- in one slot + let k_hash_mod := k.val % v.val.len + have _ : 0 ≤ k_hash_mod := by sorry + have _ : k_hash_mod < Vec.length hm.slots := by sorry + if h_hm : k_hash_mod = hash_mod.val then + simp_all + else + simp_all + have _ : + match hm.lookup key with + | none => nhm.len_s = hm.len_s + 1 + | some _ => nhm.len_s = hm.len_s := by checkpoint + simp only [lookup, List.lookup, len_s, al_v, HashMap.v, slots_t_lookup] at * + -- We have to do a case disjunction + simp_all + simp [_root_.List.update_map_eq] + -- TODO: dependent rewrites + have _ : key.val % hm.slots.val.len < (List.map List.v hm.slots.val).len := by + simp [*] + simp [_root_.List.len_flatten_update_eq, *] + split <;> + rename_i heq <;> + simp [heq] at hlen <;> + -- TODO: canonize addition by default? We need a tactic to simplify arithmetic equalities + -- with addition and substractions ((ℤ, +) is a ring or something - there should exist a tactic + -- somewhere in mathlib?) + simp [Int.add_assoc, Int.add_comm, Int.add_left_comm] <;> + int_tac + have hinv : inv nhm := by + simp [inv] at * + split_conjs + . match h: lookup hm key with + | none => + simp [h, lookup] at * + simp_all + | some _ => + simp_all [lookup] + . simp [slots_t_inv, slot_t_inv] at * + intro i hipos _ + have hs := hinv.right.left i hipos (by simp_all) + -- We need a case disjunction + if i = key.val % _root_.List.len hm.slots.val then + simp_all + else + simp_all + . match h: lookup hm key with + | none => + simp [h] at * + simp [*] + | some _ => + simp_all + simp_all end HashMap -- cgit v1.2.3 From 9e8fccbe4b667fc341b6544030f85af05fe89307 Mon Sep 17 00:00:00 2001 From: Son Ho Date: Tue, 25 Jul 2023 20:12:48 +0200 Subject: Make progress on the proofs of the hashmap --- tests/lean/Hashmap/Properties.lean | 58 ++++++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 18 deletions(-) (limited to 'tests/lean') diff --git a/tests/lean/Hashmap/Properties.lean b/tests/lean/Hashmap/Properties.lean index 92285c0d..40b5009d 100644 --- a/tests/lean/Hashmap/Properties.lean +++ b/tests/lean/Hashmap/Properties.lean @@ -263,11 +263,6 @@ def lookup (hm : HashMap α) (k : Usize) : Option α := @[simp] abbrev len_s (hm : HashMap α) : Int := hm.al_v.len -set_option trace.Progress true -/-set_option pp.explicit true -set_option pp.universes true -set_option pp.notation false -/ - -- Remark: α and β must live in the same universe, otherwise the -- bind doesn't work theorem if_update_eq @@ -297,7 +292,12 @@ theorem insert_no_resize_spec {α : Type} (hm : HashMap α) (key : Usize) (value -- TODO: progress keep as ⟨ ... ⟩ : conflict progress keep as h as ⟨ hash_mod, hhm ⟩ have _ : 0 ≤ hash_mod.val := by checkpoint scalar_tac - have _ : hash_mod.val < Vec.length hm.slots := by sorry + have _ : hash_mod.val < Vec.length hm.slots := by + have : 0 < hm.slots.val.len := by + simp [inv] at hinv + simp [hinv] + -- TODO: we want to automate that + simp [*, Int.emod_lt_of_pos] -- have h := Primitives.Vec.index_mut_spec hm.slots hash_mod -- TODO: change the spec of Vec.index_mut to introduce a let-binding. -- or: make progress introduce the let-binding by itself (this is clearer) @@ -309,11 +309,26 @@ theorem insert_no_resize_spec {α : Type} (hm : HashMap α) (key : Usize) (value have hipost : ∃ i0, (if inserted = true then hm.num_entries + Usize.ofInt 1 else pure hm.num_entries) = ret i0 ∧ i0.val = if inserted then hm.num_entries.val + 1 else hm.num_entries.val - := by sorry + := by + if inserted then + simp [*] + have : hm.num_entries.val + (Usize.ofInt 1).val ≤ Usize.max := by + simp [lookup] at hnsat + simp_all + simp [inv] at hinv + int_tac + progress + simp_all + else + simp_all [Pure.pure] progress as ⟨ i0 ⟩ -- TODO: progress "eager" to match premises with assumptions while instantiating -- meta-variables - have h_slot : slot_s_inv_hash hm.slots.length hash_mod.val (hm.slots.v.index hash_mod.val).v := by sorry + have h_slot : slot_s_inv_hash hm.slots.length hash_mod.val (hm.slots.v.index hash_mod.val).v := by + simp [inv] at hinv + have h := hinv.right.left hash_mod.val (by assumption) (by assumption) + simp [slot_t_inv] at h + simp [h] have hd : distinct_keys (hm.slots.v.index hash_mod.val).v := by checkpoint simp [inv, slots_t_inv, slot_t_inv] at hinv have h := hinv.right.left hash_mod.val (by assumption) (by assumption) @@ -329,10 +344,11 @@ theorem insert_no_resize_spec {α : Type} (hm : HashMap α) (key : Usize) (value -- TODO: update progress to automate that let nhm : HashMap α := { num_entries := i0, max_load_factor := hm.max_load_factor, max_load := hm.max_load, slots := v } exists nhm + -- TODO: later I don't want to inline nhm - we need to control simp have hupdt : lookup nhm key = some value := by checkpoint simp [lookup, List.lookup] at * simp_all - have hlkp : ∀ k, ¬ k = key → nhm.lookup k = hm.lookup k := by checkpoint + have hlkp : ∀ k, ¬ k = key → nhm.lookup k = hm.lookup k := by simp [lookup, List.lookup] at * intro k hk -- We have to make a case disjunction: either the hashes are different, @@ -340,8 +356,19 @@ theorem insert_no_resize_spec {α : Type} (hm : HashMap α) (key : Usize) (value -- are the same, in which case we have to reason about what happens -- in one slot let k_hash_mod := k.val % v.val.len - have _ : 0 ≤ k_hash_mod := by sorry - have _ : k_hash_mod < Vec.length hm.slots := by sorry + have : 0 < hm.slots.val.len := by simp_all [inv] + have hvpos : 0 < v.val.len := by simp_all + have hvnz: v.val.len ≠ 0 := by + simp_all + have _ : 0 ≤ k_hash_mod := by + -- TODO: we want to automate this + simp + apply Int.emod_nonneg k.val hvnz + have _ : k_hash_mod < Vec.length hm.slots := by + -- TODO: we want to automate this + simp + have h := Int.emod_lt_of_pos k.val hvpos + simp_all if h_hm : k_hash_mod = hash_mod.val then simp_all else @@ -377,18 +404,13 @@ theorem insert_no_resize_spec {α : Type} (hm : HashMap α) (key : Usize) (value simp_all [lookup] . simp [slots_t_inv, slot_t_inv] at * intro i hipos _ - have hs := hinv.right.left i hipos (by simp_all) + have _ := hinv.right.left i hipos (by simp_all) -- We need a case disjunction if i = key.val % _root_.List.len hm.slots.val then simp_all else simp_all - . match h: lookup hm key with - | none => - simp [h] at * - simp [*] - | some _ => - simp_all + . simp_all simp_all end HashMap -- cgit v1.2.3 From 032db82439d9b379b5435d8349c1ecf55eeb2875 Mon Sep 17 00:00:00 2001 From: Son Ho Date: Wed, 26 Jul 2023 11:41:32 +0200 Subject: Fix a proof for the hashmap --- tests/lean/Hashmap/Properties.lean | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'tests/lean') diff --git a/tests/lean/Hashmap/Properties.lean b/tests/lean/Hashmap/Properties.lean index 40b5009d..208875a6 100644 --- a/tests/lean/Hashmap/Properties.lean +++ b/tests/lean/Hashmap/Properties.lean @@ -340,7 +340,7 @@ theorem insert_no_resize_spec {α : Type} (hm : HashMap α) (key : Usize) (value . checkpoint exact hm.slots.length . checkpoint simp_all . -- Finishing the proof - progress as ⟨ v ⟩ + progress keep as hv as ⟨ v, h_veq ⟩ -- TODO: update progress to automate that let nhm : HashMap α := { num_entries := i0, max_load_factor := hm.max_load_factor, max_load := hm.max_load, slots := v } exists nhm @@ -405,12 +405,19 @@ theorem insert_no_resize_spec {α : Type} (hm : HashMap α) (key : Usize) (value . simp [slots_t_inv, slot_t_inv] at * intro i hipos _ have _ := hinv.right.left i hipos (by simp_all) + simp [hhm, h_veq] at * -- TODO: annoying -- We need a case disjunction - if i = key.val % _root_.List.len hm.slots.val then - simp_all + if h_ieq : i = key.val % _root_.List.len hm.slots.val then + -- TODO: simp_all loops (investigate). + -- Also, it is annoying to do this kind + -- of rewritings by hand. We could have a different simp + -- which safely substitutes variables when we have an + -- equality `x = ...` and `x` doesn't appear in the rhs + simp [h_ieq] at * + simp [*] else - simp_all - . simp_all + simp [*] + . simp [*] simp_all end HashMap -- cgit v1.2.3 From 81e991822879a942af34489b7a072f31739f28f6 Mon Sep 17 00:00:00 2001 From: Son Ho Date: Wed, 26 Jul 2023 12:37:17 +0200 Subject: Update the syntax of the progress tactic --- tests/lean/Hashmap/Properties.lean | 171 ++++++++++++++++++------------------- 1 file changed, 85 insertions(+), 86 deletions(-) (limited to 'tests/lean') diff --git a/tests/lean/Hashmap/Properties.lean b/tests/lean/Hashmap/Properties.lean index 208875a6..96b8193d 100644 --- a/tests/lean/Hashmap/Properties.lean +++ b/tests/lean/Hashmap/Properties.lean @@ -95,7 +95,7 @@ theorem insert_in_list_spec1 {α : Type} (key: Usize) (value: α) (ls: List α) simp only [insert_in_list] rw [insert_in_list_loop] conv => rhs; ext; simp [*] - progress keep as heq as ⟨ b, hi ⟩ + progress keep heq as ⟨ b, hi ⟩ simp only [insert_in_list] at heq exists b @@ -219,7 +219,7 @@ theorem insert_in_list_back_spec_aux {α : Type} (l : Int) (key: Usize) (value: have : distinct_keys (List.v tl0) := by checkpoint simp [distinct_keys] at hdk simp [hdk, distinct_keys] - progress keep as heq as ⟨ tl1 .. ⟩ + progress keep heq as ⟨ tl1 .. ⟩ simp only [insert_in_list_back] at heq have : slot_s_inv_hash l (hash_mod_key key l) (List.v (List.Cons k v tl1)) := by checkpoint simp [List.v, slot_s_inv_hash] at * @@ -289,8 +289,7 @@ theorem insert_no_resize_spec {α : Type} (hm : HashMap α) (key : Usize) (value have _ : (Vec.len (List α) hm.slots).val ≠ 0 := by checkpoint intro simp_all [inv] - -- TODO: progress keep as ⟨ ... ⟩ : conflict - progress keep as h as ⟨ hash_mod, hhm ⟩ + progress keep _ as ⟨ hash_mod, hhm ⟩ have _ : 0 ≤ hash_mod.val := by checkpoint scalar_tac have _ : hash_mod.val < Vec.length hm.slots := by have : 0 < hm.slots.val.len := by @@ -324,11 +323,12 @@ theorem insert_no_resize_spec {α : Type} (hm : HashMap α) (key : Usize) (value progress as ⟨ i0 ⟩ -- TODO: progress "eager" to match premises with assumptions while instantiating -- meta-variables - have h_slot : slot_s_inv_hash hm.slots.length hash_mod.val (hm.slots.v.index hash_mod.val).v := by + have h_slot : slot_s_inv_hash hm.slots.length (hash_mod_key key hm.slots.length) + (List.v (List.index (hm.slots.val) hash_mod.val)) := by simp [inv] at hinv - have h := hinv.right.left hash_mod.val (by assumption) (by assumption) - simp [slot_t_inv] at h - simp [h] + have h := (hinv.right.left hash_mod.val (by assumption) (by assumption)).right + simp [slot_t_inv, hhm] at h + simp [h, hhm] have hd : distinct_keys (hm.slots.v.index hash_mod.val).v := by checkpoint simp [inv, slots_t_inv, slot_t_inv] at hinv have h := hinv.right.left hash_mod.val (by assumption) (by assumption) @@ -337,88 +337,87 @@ theorem insert_no_resize_spec {α : Type} (hm : HashMap α) (key : Usize) (value -- TODO: allow providing terms to progress to instantiate the meta variables -- which are not propositions progress as ⟨ l0, _, _, _, hlen .. ⟩ - . checkpoint exact hm.slots.length - . checkpoint simp_all - . -- Finishing the proof - progress keep as hv as ⟨ v, h_veq ⟩ - -- TODO: update progress to automate that - let nhm : HashMap α := { num_entries := i0, max_load_factor := hm.max_load_factor, max_load := hm.max_load, slots := v } - exists nhm - -- TODO: later I don't want to inline nhm - we need to control simp - have hupdt : lookup nhm key = some value := by checkpoint - simp [lookup, List.lookup] at * + -- Finishing the proof + progress keep hv as ⟨ v, h_veq ⟩ + -- TODO: update progress to automate that + let nhm : HashMap α := { num_entries := i0, max_load_factor := hm.max_load_factor, max_load := hm.max_load, slots := v } + exists nhm + -- TODO: later I don't want to inline nhm - we need to control simp + have hupdt : lookup nhm key = some value := by checkpoint + simp [lookup, List.lookup] at * + simp_all + have hlkp : ∀ k, ¬ k = key → nhm.lookup k = hm.lookup k := by + simp [lookup, List.lookup] at * + intro k hk + -- We have to make a case disjunction: either the hashes are different, + -- in which case we don't even lookup the same slots, or the hashes + -- are the same, in which case we have to reason about what happens + -- in one slot + let k_hash_mod := k.val % v.val.len + have : 0 < hm.slots.val.len := by simp_all [inv] + have hvpos : 0 < v.val.len := by simp_all + have hvnz: v.val.len ≠ 0 := by simp_all - have hlkp : ∀ k, ¬ k = key → nhm.lookup k = hm.lookup k := by - simp [lookup, List.lookup] at * - intro k hk - -- We have to make a case disjunction: either the hashes are different, - -- in which case we don't even lookup the same slots, or the hashes - -- are the same, in which case we have to reason about what happens - -- in one slot - let k_hash_mod := k.val % v.val.len - have : 0 < hm.slots.val.len := by simp_all [inv] - have hvpos : 0 < v.val.len := by simp_all - have hvnz: v.val.len ≠ 0 := by - simp_all - have _ : 0 ≤ k_hash_mod := by - -- TODO: we want to automate this - simp - apply Int.emod_nonneg k.val hvnz - have _ : k_hash_mod < Vec.length hm.slots := by - -- TODO: we want to automate this - simp - have h := Int.emod_lt_of_pos k.val hvpos - simp_all - if h_hm : k_hash_mod = hash_mod.val then + have _ : 0 ≤ k_hash_mod := by + -- TODO: we want to automate this + simp + apply Int.emod_nonneg k.val hvnz + have _ : k_hash_mod < Vec.length hm.slots := by + -- TODO: we want to automate this + simp + have h := Int.emod_lt_of_pos k.val hvpos + simp_all + if h_hm : k_hash_mod = hash_mod.val then + simp_all + else + simp_all + have _ : + match hm.lookup key with + | none => nhm.len_s = hm.len_s + 1 + | some _ => nhm.len_s = hm.len_s := by checkpoint + simp only [lookup, List.lookup, len_s, al_v, HashMap.v, slots_t_lookup] at * + -- We have to do a case disjunction + simp_all + simp [_root_.List.update_map_eq] + -- TODO: dependent rewrites + have _ : key.val % hm.slots.val.len < (List.map List.v hm.slots.val).len := by + simp [*] + simp [_root_.List.len_flatten_update_eq, *] + split <;> + rename_i heq <;> + simp [heq] at hlen <;> + -- TODO: canonize addition by default? We need a tactic to simplify arithmetic equalities + -- with addition and substractions ((ℤ, +) is a ring or something - there should exist a tactic + -- somewhere in mathlib?) + simp [Int.add_assoc, Int.add_comm, Int.add_left_comm] <;> + int_tac + have hinv : inv nhm := by + simp [inv] at * + split_conjs + . match h: lookup hm key with + | none => + simp [h, lookup] at * simp_all + | some _ => + simp_all [lookup] + . simp [slots_t_inv, slot_t_inv] at * + intro i hipos _ + have _ := hinv.right.left i hipos (by simp_all) + simp [hhm, h_veq] at * -- TODO: annoying + -- We need a case disjunction + if h_ieq : i = key.val % _root_.List.len hm.slots.val then + -- TODO: simp_all fails: "(deterministic) timeout at 'whnf'" + -- Also, it is annoying to do this kind + -- of rewritings by hand. We could have a different simp + -- which safely substitutes variables when we have an + -- equality `x = ...` and `x` doesn't appear in the rhs + simp [h_ieq] at * + simp [*] else - simp_all - have _ : - match hm.lookup key with - | none => nhm.len_s = hm.len_s + 1 - | some _ => nhm.len_s = hm.len_s := by checkpoint - simp only [lookup, List.lookup, len_s, al_v, HashMap.v, slots_t_lookup] at * - -- We have to do a case disjunction - simp_all - simp [_root_.List.update_map_eq] - -- TODO: dependent rewrites - have _ : key.val % hm.slots.val.len < (List.map List.v hm.slots.val).len := by simp [*] - simp [_root_.List.len_flatten_update_eq, *] - split <;> - rename_i heq <;> - simp [heq] at hlen <;> - -- TODO: canonize addition by default? We need a tactic to simplify arithmetic equalities - -- with addition and substractions ((ℤ, +) is a ring or something - there should exist a tactic - -- somewhere in mathlib?) - simp [Int.add_assoc, Int.add_comm, Int.add_left_comm] <;> - int_tac - have hinv : inv nhm := by - simp [inv] at * - split_conjs - . match h: lookup hm key with - | none => - simp [h, lookup] at * - simp_all - | some _ => - simp_all [lookup] - . simp [slots_t_inv, slot_t_inv] at * - intro i hipos _ - have _ := hinv.right.left i hipos (by simp_all) - simp [hhm, h_veq] at * -- TODO: annoying - -- We need a case disjunction - if h_ieq : i = key.val % _root_.List.len hm.slots.val then - -- TODO: simp_all loops (investigate). - -- Also, it is annoying to do this kind - -- of rewritings by hand. We could have a different simp - -- which safely substitutes variables when we have an - -- equality `x = ...` and `x` doesn't appear in the rhs - simp [h_ieq] at * - simp [*] - else - simp [*] - . simp [*] - simp_all + . -- TODO: simp[*] fails: "(deterministic) timeout at 'whnf'" + simp [hinv, h_veq] + simp_all end HashMap -- cgit v1.2.3 From 3337c4ac3326c3132dcc322f55f23a7d2054ceb0 Mon Sep 17 00:00:00 2001 From: Son Ho Date: Wed, 26 Jul 2023 15:00:11 +0200 Subject: Update some of the Vec function specs --- tests/lean/Hashmap/Properties.lean | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) (limited to 'tests/lean') diff --git a/tests/lean/Hashmap/Properties.lean b/tests/lean/Hashmap/Properties.lean index 96b8193d..5d340b5c 100644 --- a/tests/lean/Hashmap/Properties.lean +++ b/tests/lean/Hashmap/Properties.lean @@ -285,7 +285,7 @@ theorem insert_no_resize_spec {α : Type} (hm : HashMap α) (key : Usize) (value | none => nhm.len_s = hm.len_s + 1 | some _ => nhm.len_s = hm.len_s) := by rw [insert_no_resize] - simp [hash_key] + simp only [hash_key, bind_tc_ret] -- TODO: annoying have _ : (Vec.len (List α) hm.slots).val ≠ 0 := by checkpoint intro simp_all [inv] @@ -297,10 +297,9 @@ theorem insert_no_resize_spec {α : Type} (hm : HashMap α) (key : Usize) (value simp [hinv] -- TODO: we want to automate that simp [*, Int.emod_lt_of_pos] - -- have h := Primitives.Vec.index_mut_spec hm.slots hash_mod -- TODO: change the spec of Vec.index_mut to introduce a let-binding. -- or: make progress introduce the let-binding by itself (this is clearer) - progress + progress as ⟨ l, h_leq ⟩ -- TODO: make progress use the names written in the goal progress as ⟨ inserted ⟩ rw [if_update_eq] -- TODO: necessary because we don't have a join @@ -311,38 +310,42 @@ theorem insert_no_resize_spec {α : Type} (hm : HashMap α) (key : Usize) (value := by if inserted then simp [*] - have : hm.num_entries.val + (Usize.ofInt 1).val ≤ Usize.max := by + have hbounds : hm.num_entries.val + (Usize.ofInt 1).val ≤ Usize.max := by simp [lookup] at hnsat simp_all simp [inv] at hinv int_tac - progress - simp_all + -- TODO: progress fails in command line mode with "index out of bounds" + -- and I have no idea how to fix this. The error happens after progress + -- introduced the new goals. It must be when we exit the "withApp", etc. + -- helpers. + have ⟨ z, hp ⟩ := Usize.add_spec hbounds + simp [hp] else - simp_all [Pure.pure] + simp [*, Pure.pure] progress as ⟨ i0 ⟩ -- TODO: progress "eager" to match premises with assumptions while instantiating -- meta-variables - have h_slot : slot_s_inv_hash hm.slots.length (hash_mod_key key hm.slots.length) - (List.v (List.index (hm.slots.val) hash_mod.val)) := by + have h_slot : slot_s_inv_hash hm.slots.length (hash_mod_key key hm.slots.length) l.v + := by simp [inv] at hinv have h := (hinv.right.left hash_mod.val (by assumption) (by assumption)).right simp [slot_t_inv, hhm] at h - simp [h, hhm] - have hd : distinct_keys (hm.slots.v.index hash_mod.val).v := by checkpoint + simp [h, hhm, h_leq] + have hd : distinct_keys l.v := by checkpoint simp [inv, slots_t_inv, slot_t_inv] at hinv have h := hinv.right.left hash_mod.val (by assumption) (by assumption) - simp [h] + simp [h, h_leq] -- TODO: hide the variables and only keep the props -- TODO: allow providing terms to progress to instantiate the meta variables -- which are not propositions progress as ⟨ l0, _, _, _, hlen .. ⟩ - -- Finishing the proof progress keep hv as ⟨ v, h_veq ⟩ -- TODO: update progress to automate that let nhm : HashMap α := { num_entries := i0, max_load_factor := hm.max_load_factor, max_load := hm.max_load, slots := v } exists nhm - -- TODO: later I don't want to inline nhm - we need to control simp + -- TODO: later I don't want to inline nhm - we need to control simp: deactivate + -- zeta reduction? have hupdt : lookup nhm key = some value := by checkpoint simp [lookup, List.lookup] at * simp_all @@ -387,7 +390,7 @@ theorem insert_no_resize_spec {α : Type} (hm : HashMap α) (key : Usize) (value rename_i heq <;> simp [heq] at hlen <;> -- TODO: canonize addition by default? We need a tactic to simplify arithmetic equalities - -- with addition and substractions ((ℤ, +) is a ring or something - there should exist a tactic + -- with addition and substractions ((ℤ, +) is a group or something - there should exist a tactic -- somewhere in mathlib?) simp [Int.add_assoc, Int.add_comm, Int.add_left_comm] <;> int_tac @@ -403,7 +406,7 @@ theorem insert_no_resize_spec {α : Type} (hm : HashMap α) (key : Usize) (value . simp [slots_t_inv, slot_t_inv] at * intro i hipos _ have _ := hinv.right.left i hipos (by simp_all) - simp [hhm, h_veq] at * -- TODO: annoying + simp [hhm, h_veq] at * -- TODO: annoying, we do that because simp_all fails below -- We need a case disjunction if h_ieq : i = key.val % _root_.List.len hm.slots.val then -- TODO: simp_all fails: "(deterministic) timeout at 'whnf'" -- cgit v1.2.3 From 31de6eb8a82e17b4113262a18abf61e6233b55df Mon Sep 17 00:00:00 2001 From: Son Ho Date: Wed, 26 Jul 2023 15:00:29 +0200 Subject: Make a minor modification to a hashmap proof --- tests/lean/Hashmap/Properties.lean | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'tests/lean') diff --git a/tests/lean/Hashmap/Properties.lean b/tests/lean/Hashmap/Properties.lean index 5d340b5c..ee63a065 100644 --- a/tests/lean/Hashmap/Properties.lean +++ b/tests/lean/Hashmap/Properties.lean @@ -271,6 +271,10 @@ theorem if_update_eq := by split <;> simp [Pure.pure] +-- TODO: move: small helper +def mk_opaque {α : Sort u} (x : α) : { y : α // y = x} := + ⟨ x, by simp ⟩ + theorem insert_no_resize_spec {α : Type} (hm : HashMap α) (key : Usize) (value : α) (hinv : hm.inv) (hnsat : hm.lookup key = none → hm.len_s < Usize.max) : ∃ nhm, hm.insert_no_resize α key value = ret nhm ∧ @@ -342,10 +346,10 @@ theorem insert_no_resize_spec {α : Type} (hm : HashMap α) (key : Usize) (value progress as ⟨ l0, _, _, _, hlen .. ⟩ progress keep hv as ⟨ v, h_veq ⟩ -- TODO: update progress to automate that - let nhm : HashMap α := { num_entries := i0, max_load_factor := hm.max_load_factor, max_load := hm.max_load, slots := v } - exists nhm -- TODO: later I don't want to inline nhm - we need to control simp: deactivate - -- zeta reduction? + -- zeta reduction? For now I have to do this peculiar manipulation + have ⟨ nhm, nhm_eq ⟩ := @mk_opaque (HashMap α) { num_entries := i0, max_load_factor := hm.max_load_factor, max_load := hm.max_load, slots := v } + exists nhm have hupdt : lookup nhm key = some value := by checkpoint simp [lookup, List.lookup] at * simp_all @@ -406,7 +410,7 @@ theorem insert_no_resize_spec {α : Type} (hm : HashMap α) (key : Usize) (value . simp [slots_t_inv, slot_t_inv] at * intro i hipos _ have _ := hinv.right.left i hipos (by simp_all) - simp [hhm, h_veq] at * -- TODO: annoying, we do that because simp_all fails below + simp [hhm, h_veq, nhm_eq] at * -- TODO: annoying, we do that because simp_all fails below -- We need a case disjunction if h_ieq : i = key.val % _root_.List.len hm.slots.val then -- TODO: simp_all fails: "(deterministic) timeout at 'whnf'" @@ -419,7 +423,7 @@ theorem insert_no_resize_spec {α : Type} (hm : HashMap α) (key : Usize) (value else simp [*] . -- TODO: simp[*] fails: "(deterministic) timeout at 'whnf'" - simp [hinv, h_veq] + simp [hinv, h_veq, nhm_eq] simp_all end HashMap -- cgit v1.2.3 From 42551283ecab981b9bb646cab2e8da5491d71b17 Mon Sep 17 00:00:00 2001 From: Son Ho Date: Wed, 26 Jul 2023 15:07:09 +0200 Subject: Make minor modifications --- tests/lean/Hashmap/Properties.lean | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'tests/lean') diff --git a/tests/lean/Hashmap/Properties.lean b/tests/lean/Hashmap/Properties.lean index ee63a065..e1bd8669 100644 --- a/tests/lean/Hashmap/Properties.lean +++ b/tests/lean/Hashmap/Properties.lean @@ -271,7 +271,8 @@ theorem if_update_eq := by split <;> simp [Pure.pure] --- TODO: move: small helper +-- Small helper +-- TODO: move, and introduce a better solution with nice syntax def mk_opaque {α : Sort u} (x : α) : { y : α // y = x} := ⟨ x, by simp ⟩ @@ -328,8 +329,6 @@ theorem insert_no_resize_spec {α : Type} (hm : HashMap α) (key : Usize) (value else simp [*, Pure.pure] progress as ⟨ i0 ⟩ - -- TODO: progress "eager" to match premises with assumptions while instantiating - -- meta-variables have h_slot : slot_s_inv_hash hm.slots.length (hash_mod_key key hm.slots.length) l.v := by simp [inv] at hinv -- cgit v1.2.3 From 9b3a58e423333fc9a4a5a264c3beb0a3d951e86b Mon Sep 17 00:00:00 2001 From: Son Ho Date: Mon, 31 Jul 2023 15:56:52 +0200 Subject: Make minor modifications --- tests/lean/Hashmap/Properties.lean | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'tests/lean') diff --git a/tests/lean/Hashmap/Properties.lean b/tests/lean/Hashmap/Properties.lean index e1bd8669..3652f608 100644 --- a/tests/lean/Hashmap/Properties.lean +++ b/tests/lean/Hashmap/Properties.lean @@ -276,6 +276,14 @@ theorem if_update_eq def mk_opaque {α : Sort u} (x : α) : { y : α // y = x} := ⟨ x, by simp ⟩ +--set_option profiler true +--set_option profiler.threshold 10 +--set_option trace.profiler true + +-- For pretty printing (useful when copy-pasting goals) +attribute [pp_dot] List.length -- use the dot notation when printing +set_option pp.coercions false -- do not print coercions with ↑ (this doesn't parse) + theorem insert_no_resize_spec {α : Type} (hm : HashMap α) (key : Usize) (value : α) (hinv : hm.inv) (hnsat : hm.lookup key = none → hm.len_s < Usize.max) : ∃ nhm, hm.insert_no_resize α key value = ret nhm ∧ @@ -324,6 +332,7 @@ theorem insert_no_resize_spec {α : Type} (hm : HashMap α) (key : Usize) (value -- and I have no idea how to fix this. The error happens after progress -- introduced the new goals. It must be when we exit the "withApp", etc. -- helpers. + -- progress as ⟨ z, hp ⟩ have ⟨ z, hp ⟩ := Usize.add_spec hbounds simp [hp] else -- cgit v1.2.3