From 4a34537a7fe33fa33d618b153832f9e750276480 Mon Sep 17 00:00:00 2001 From: Son Ho Date: Fri, 14 Jun 2024 10:41:06 +0200 Subject: Update the code of the hashmap --- tests/lean/Hashmap/Funs.lean | 247 +++++++++++++------------- tests/lean/Hashmap/FunsExternal_Template.lean | 4 +- tests/lean/Hashmap/Types.lean | 14 +- 3 files changed, 135 insertions(+), 130 deletions(-) (limited to 'tests/lean/Hashmap') diff --git a/tests/lean/Hashmap/Funs.lean b/tests/lean/Hashmap/Funs.lean index 612e1848..7d8f73a7 100644 --- a/tests/lean/Hashmap/Funs.lean +++ b/tests/lean/Hashmap/Funs.lean @@ -8,41 +8,41 @@ open Primitives namespace hashmap /- [hashmap::hash_key]: - Source: 'tests/src/hashmap.rs', lines 37:0-37:32 -/ + Source: 'tests/src/hashmap.rs', lines 38:0-38:32 -/ def hash_key (k : Usize) : Result Usize := Result.ok k /- [hashmap::{hashmap::HashMap}::allocate_slots]: loop 0: - Source: 'tests/src/hashmap.rs', lines 60:4-66:5 -/ + Source: 'tests/src/hashmap.rs', lines 61:4-67:5 -/ divergent def HashMap.allocate_slots_loop - (T : Type) (slots : alloc.vec.Vec (List T)) (n : Usize) : - Result (alloc.vec.Vec (List T)) + (T : Type) (slots : alloc.vec.Vec (AList T)) (n : Usize) : + Result (alloc.vec.Vec (AList T)) := if n > 0#usize then do - let slots1 ← alloc.vec.Vec.push (List T) slots List.Nil + let slots1 ← alloc.vec.Vec.push (AList T) slots AList.Nil let n1 ← n - 1#usize HashMap.allocate_slots_loop T slots1 n1 else Result.ok slots /- [hashmap::{hashmap::HashMap}::allocate_slots]: - Source: 'tests/src/hashmap.rs', lines 60:4-60:76 -/ + Source: 'tests/src/hashmap.rs', lines 61:4-61:78 -/ def HashMap.allocate_slots - (T : Type) (slots : alloc.vec.Vec (List T)) (n : Usize) : - Result (alloc.vec.Vec (List T)) + (T : Type) (slots : alloc.vec.Vec (AList T)) (n : Usize) : + Result (alloc.vec.Vec (AList T)) := HashMap.allocate_slots_loop T slots n /- [hashmap::{hashmap::HashMap}::new_with_capacity]: - Source: 'tests/src/hashmap.rs', lines 69:4-73:13 -/ + Source: 'tests/src/hashmap.rs', lines 70:4-74:13 -/ def HashMap.new_with_capacity (T : Type) (capacity : Usize) (max_load_dividend : Usize) (max_load_divisor : Usize) : Result (HashMap T) := do - let slots ← HashMap.allocate_slots T (alloc.vec.Vec.new (List T)) capacity + let slots ← HashMap.allocate_slots T (alloc.vec.Vec.new (AList T)) capacity let i ← capacity * max_load_dividend let i1 ← i / max_load_divisor Result.ok @@ -54,141 +54,142 @@ def HashMap.new_with_capacity } /- [hashmap::{hashmap::HashMap}::new]: - Source: 'tests/src/hashmap.rs', lines 85:4-85:24 -/ + Source: 'tests/src/hashmap.rs', lines 86:4-86:24 -/ def HashMap.new (T : Type) : Result (HashMap T) := HashMap.new_with_capacity T 32#usize 4#usize 5#usize /- [hashmap::{hashmap::HashMap}::clear]: loop 0: - Source: 'tests/src/hashmap.rs', lines 90:4-98:5 -/ + Source: 'tests/src/hashmap.rs', lines 91:4-99:5 -/ divergent def HashMap.clear_loop - (T : Type) (slots : alloc.vec.Vec (List T)) (i : Usize) : - Result (alloc.vec.Vec (List T)) + (T : Type) (slots : alloc.vec.Vec (AList T)) (i : Usize) : + Result (alloc.vec.Vec (AList T)) := - let i1 := alloc.vec.Vec.len (List T) slots + let i1 := alloc.vec.Vec.len (AList T) slots if i < i1 then do let (_, index_mut_back) ← - alloc.vec.Vec.index_mut (List T) Usize - (core.slice.index.SliceIndexUsizeSliceTInst (List T)) slots i + alloc.vec.Vec.index_mut (AList T) Usize + (core.slice.index.SliceIndexUsizeSliceTInst (AList T)) slots i let i2 ← i + 1#usize - let slots1 ← index_mut_back List.Nil + let slots1 ← index_mut_back AList.Nil HashMap.clear_loop T slots1 i2 else Result.ok slots /- [hashmap::{hashmap::HashMap}::clear]: - Source: 'tests/src/hashmap.rs', lines 90:4-90:27 -/ + Source: 'tests/src/hashmap.rs', lines 91:4-91:27 -/ def HashMap.clear (T : Type) (self : HashMap T) : Result (HashMap T) := do let hm ← HashMap.clear_loop T self.slots 0#usize Result.ok { self with num_entries := 0#usize, slots := hm } /- [hashmap::{hashmap::HashMap}::len]: - Source: 'tests/src/hashmap.rs', lines 100:4-100:30 -/ + Source: 'tests/src/hashmap.rs', lines 101:4-101:30 -/ def HashMap.len (T : Type) (self : HashMap T) : Result Usize := Result.ok self.num_entries /- [hashmap::{hashmap::HashMap}::insert_in_list]: loop 0: - Source: 'tests/src/hashmap.rs', lines 107:4-124:5 -/ + Source: 'tests/src/hashmap.rs', lines 108:4-125:5 -/ divergent def HashMap.insert_in_list_loop - (T : Type) (key : Usize) (value : T) (ls : List T) : - Result (Bool × (List T)) + (T : Type) (key : Usize) (value : T) (ls : AList T) : + Result (Bool × (AList T)) := match ls with - | List.Cons ckey cvalue tl => + | AList.Cons ckey cvalue tl => if ckey = key - then Result.ok (false, List.Cons ckey value tl) + then Result.ok (false, AList.Cons ckey value tl) else do let (b, tl1) ← HashMap.insert_in_list_loop T key value tl - Result.ok (b, List.Cons ckey cvalue tl1) - | List.Nil => Result.ok (true, List.Cons key value List.Nil) + Result.ok (b, AList.Cons ckey cvalue tl1) + | AList.Nil => Result.ok (true, AList.Cons key value AList.Nil) /- [hashmap::{hashmap::HashMap}::insert_in_list]: - Source: 'tests/src/hashmap.rs', lines 107:4-107:71 -/ + Source: 'tests/src/hashmap.rs', lines 108:4-108:72 -/ def HashMap.insert_in_list - (T : Type) (key : Usize) (value : T) (ls : List T) : - Result (Bool × (List T)) + (T : Type) (key : Usize) (value : T) (ls : AList T) : + Result (Bool × (AList T)) := HashMap.insert_in_list_loop T key value ls /- [hashmap::{hashmap::HashMap}::insert_no_resize]: - Source: 'tests/src/hashmap.rs', lines 127:4-127:54 -/ + Source: 'tests/src/hashmap.rs', lines 128:4-128:54 -/ def HashMap.insert_no_resize (T : Type) (self : HashMap T) (key : Usize) (value : T) : Result (HashMap T) := do let hash ← hash_key key - let i := alloc.vec.Vec.len (List T) self.slots + let i := alloc.vec.Vec.len (AList T) self.slots let hash_mod ← hash % i - let (l, index_mut_back) ← - alloc.vec.Vec.index_mut (List T) Usize - (core.slice.index.SliceIndexUsizeSliceTInst (List T)) self.slots hash_mod - let (inserted, l1) ← HashMap.insert_in_list T key value l + let (a, index_mut_back) ← + alloc.vec.Vec.index_mut (AList T) Usize + (core.slice.index.SliceIndexUsizeSliceTInst (AList T)) self.slots + hash_mod + let (inserted, a1) ← HashMap.insert_in_list T key value a if inserted then do let i1 ← self.num_entries + 1#usize - let v ← index_mut_back l1 + let v ← index_mut_back a1 Result.ok { self with num_entries := i1, slots := v } else do - let v ← index_mut_back l1 + let v ← index_mut_back a1 Result.ok { self with slots := v } /- [hashmap::{hashmap::HashMap}::move_elements_from_list]: loop 0: - Source: 'tests/src/hashmap.rs', lines 193:4-206:5 -/ + Source: 'tests/src/hashmap.rs', lines 194:4-207:5 -/ divergent def HashMap.move_elements_from_list_loop - (T : Type) (ntable : HashMap T) (ls : List T) : Result (HashMap T) := + (T : Type) (ntable : HashMap T) (ls : AList T) : Result (HashMap T) := match ls with - | List.Cons k v tl => + | AList.Cons k v tl => do let ntable1 ← HashMap.insert_no_resize T ntable k v HashMap.move_elements_from_list_loop T ntable1 tl - | List.Nil => Result.ok ntable + | AList.Nil => Result.ok ntable /- [hashmap::{hashmap::HashMap}::move_elements_from_list]: - Source: 'tests/src/hashmap.rs', lines 193:4-193:72 -/ + Source: 'tests/src/hashmap.rs', lines 194:4-194:73 -/ def HashMap.move_elements_from_list - (T : Type) (ntable : HashMap T) (ls : List T) : Result (HashMap T) := + (T : Type) (ntable : HashMap T) (ls : AList T) : Result (HashMap T) := HashMap.move_elements_from_list_loop T ntable ls /- [hashmap::{hashmap::HashMap}::move_elements]: loop 0: - Source: 'tests/src/hashmap.rs', lines 181:4-190:5 -/ + Source: 'tests/src/hashmap.rs', lines 182:4-191:5 -/ divergent def HashMap.move_elements_loop - (T : Type) (ntable : HashMap T) (slots : alloc.vec.Vec (List T)) (i : Usize) + (T : Type) (ntable : HashMap T) (slots : alloc.vec.Vec (AList T)) (i : Usize) : - Result ((HashMap T) × (alloc.vec.Vec (List T))) + Result ((HashMap T) × (alloc.vec.Vec (AList T))) := - let i1 := alloc.vec.Vec.len (List T) slots + let i1 := alloc.vec.Vec.len (AList T) slots if i < i1 then do - let (l, index_mut_back) ← - alloc.vec.Vec.index_mut (List T) Usize - (core.slice.index.SliceIndexUsizeSliceTInst (List T)) slots i - let (ls, l1) := core.mem.replace (List T) l List.Nil + let (a, index_mut_back) ← + alloc.vec.Vec.index_mut (AList T) Usize + (core.slice.index.SliceIndexUsizeSliceTInst (AList T)) slots i + let (ls, a1) := core.mem.replace (AList T) a AList.Nil let ntable1 ← HashMap.move_elements_from_list T ntable ls let i2 ← i + 1#usize - let slots1 ← index_mut_back l1 + let slots1 ← index_mut_back a1 HashMap.move_elements_loop T ntable1 slots1 i2 else Result.ok (ntable, slots) /- [hashmap::{hashmap::HashMap}::move_elements]: - Source: 'tests/src/hashmap.rs', lines 181:4-181:95 -/ + Source: 'tests/src/hashmap.rs', lines 182:4-182:96 -/ def HashMap.move_elements - (T : Type) (ntable : HashMap T) (slots : alloc.vec.Vec (List T)) (i : Usize) + (T : Type) (ntable : HashMap T) (slots : alloc.vec.Vec (AList T)) (i : Usize) : - Result ((HashMap T) × (alloc.vec.Vec (List T))) + Result ((HashMap T) × (alloc.vec.Vec (AList T))) := HashMap.move_elements_loop T ntable slots i /- [hashmap::{hashmap::HashMap}::try_resize]: - Source: 'tests/src/hashmap.rs', lines 150:4-150:28 -/ + Source: 'tests/src/hashmap.rs', lines 151:4-151:28 -/ def HashMap.try_resize (T : Type) (self : HashMap T) : Result (HashMap T) := do let max_usize ← Scalar.cast .Usize core_u32_max - let capacity := alloc.vec.Vec.len (List T) self.slots + let capacity := alloc.vec.Vec.len (AList T) self.slots let n1 ← max_usize / 2#usize let (i, i1) := self.max_load_factor let i2 ← n1 / i @@ -208,7 +209,7 @@ def HashMap.try_resize (T : Type) (self : HashMap T) : Result (HashMap T) := else Result.ok { self with max_load_factor := (i, i1) } /- [hashmap::{hashmap::HashMap}::insert]: - Source: 'tests/src/hashmap.rs', lines 139:4-139:48 -/ + Source: 'tests/src/hashmap.rs', lines 140:4-140:48 -/ def HashMap.insert (T : Type) (self : HashMap T) (key : Usize) (value : T) : Result (HashMap T) @@ -221,74 +222,76 @@ def HashMap.insert else Result.ok self1 /- [hashmap::{hashmap::HashMap}::contains_key_in_list]: loop 0: - Source: 'tests/src/hashmap.rs', lines 216:4-229:5 -/ + Source: 'tests/src/hashmap.rs', lines 217:4-230:5 -/ divergent def HashMap.contains_key_in_list_loop - (T : Type) (key : Usize) (ls : List T) : Result Bool := + (T : Type) (key : Usize) (ls : AList T) : Result Bool := match ls with - | List.Cons ckey _ tl => + | AList.Cons ckey _ tl => if ckey = key then Result.ok true else HashMap.contains_key_in_list_loop T key tl - | List.Nil => Result.ok false + | AList.Nil => Result.ok false /- [hashmap::{hashmap::HashMap}::contains_key_in_list]: - Source: 'tests/src/hashmap.rs', lines 216:4-216:68 -/ + Source: 'tests/src/hashmap.rs', lines 217:4-217:69 -/ def HashMap.contains_key_in_list - (T : Type) (key : Usize) (ls : List T) : Result Bool := + (T : Type) (key : Usize) (ls : AList T) : Result Bool := HashMap.contains_key_in_list_loop T key ls /- [hashmap::{hashmap::HashMap}::contains_key]: - Source: 'tests/src/hashmap.rs', lines 209:4-209:49 -/ + Source: 'tests/src/hashmap.rs', lines 210:4-210:49 -/ def HashMap.contains_key (T : Type) (self : HashMap T) (key : Usize) : Result Bool := do let hash ← hash_key key - let i := alloc.vec.Vec.len (List T) self.slots + let i := alloc.vec.Vec.len (AList T) self.slots let hash_mod ← hash % i - let l ← - alloc.vec.Vec.index (List T) Usize - (core.slice.index.SliceIndexUsizeSliceTInst (List T)) self.slots hash_mod - HashMap.contains_key_in_list T key l + let a ← + alloc.vec.Vec.index (AList T) Usize + (core.slice.index.SliceIndexUsizeSliceTInst (AList T)) self.slots + hash_mod + HashMap.contains_key_in_list T key a /- [hashmap::{hashmap::HashMap}::get_in_list]: loop 0: - Source: 'tests/src/hashmap.rs', lines 234:4-247:5 -/ + Source: 'tests/src/hashmap.rs', lines 235:4-248:5 -/ divergent def HashMap.get_in_list_loop - (T : Type) (key : Usize) (ls : List T) : Result T := + (T : Type) (key : Usize) (ls : AList T) : Result T := match ls with - | List.Cons ckey cvalue tl => + | AList.Cons ckey cvalue tl => if ckey = key then Result.ok cvalue else HashMap.get_in_list_loop T key tl - | List.Nil => Result.fail .panic + | AList.Nil => Result.fail .panic /- [hashmap::{hashmap::HashMap}::get_in_list]: - Source: 'tests/src/hashmap.rs', lines 234:4-234:70 -/ -def HashMap.get_in_list (T : Type) (key : Usize) (ls : List T) : Result T := + Source: 'tests/src/hashmap.rs', lines 235:4-235:71 -/ +def HashMap.get_in_list (T : Type) (key : Usize) (ls : AList T) : Result T := HashMap.get_in_list_loop T key ls /- [hashmap::{hashmap::HashMap}::get]: - Source: 'tests/src/hashmap.rs', lines 249:4-249:55 -/ + Source: 'tests/src/hashmap.rs', lines 250:4-250:55 -/ def HashMap.get (T : Type) (self : HashMap T) (key : Usize) : Result T := do let hash ← hash_key key - let i := alloc.vec.Vec.len (List T) self.slots + let i := alloc.vec.Vec.len (AList T) self.slots let hash_mod ← hash % i - let l ← - alloc.vec.Vec.index (List T) Usize - (core.slice.index.SliceIndexUsizeSliceTInst (List T)) self.slots hash_mod - HashMap.get_in_list T key l + let a ← + alloc.vec.Vec.index (AList T) Usize + (core.slice.index.SliceIndexUsizeSliceTInst (AList T)) self.slots + hash_mod + HashMap.get_in_list T key a /- [hashmap::{hashmap::HashMap}::get_mut_in_list]: loop 0: - Source: 'tests/src/hashmap.rs', lines 255:4-264:5 -/ + Source: 'tests/src/hashmap.rs', lines 256:4-265:5 -/ divergent def HashMap.get_mut_in_list_loop - (T : Type) (ls : List T) (key : Usize) : - Result (T × (T → Result (List T))) + (T : Type) (ls : AList T) (key : Usize) : + Result (T × (T → Result (AList T))) := match ls with - | List.Cons ckey cvalue tl => + | AList.Cons ckey cvalue tl => if ckey = key then - let back := fun ret => Result.ok (List.Cons ckey ret tl) + let back := fun ret => Result.ok (AList.Cons ckey ret tl) Result.ok (cvalue, back) else do @@ -297,92 +300,94 @@ divergent def HashMap.get_mut_in_list_loop fun ret => do let tl1 ← back ret - Result.ok (List.Cons ckey cvalue tl1) + Result.ok (AList.Cons ckey cvalue tl1) Result.ok (t, back1) - | List.Nil => Result.fail .panic + | AList.Nil => Result.fail .panic /- [hashmap::{hashmap::HashMap}::get_mut_in_list]: - Source: 'tests/src/hashmap.rs', lines 255:4-255:86 -/ + Source: 'tests/src/hashmap.rs', lines 256:4-256:87 -/ def HashMap.get_mut_in_list - (T : Type) (ls : List T) (key : Usize) : - Result (T × (T → Result (List T))) + (T : Type) (ls : AList T) (key : Usize) : + Result (T × (T → Result (AList T))) := HashMap.get_mut_in_list_loop T ls key /- [hashmap::{hashmap::HashMap}::get_mut]: - Source: 'tests/src/hashmap.rs', lines 267:4-267:67 -/ + Source: 'tests/src/hashmap.rs', lines 268:4-268:67 -/ def HashMap.get_mut (T : Type) (self : HashMap T) (key : Usize) : Result (T × (T → Result (HashMap T))) := do let hash ← hash_key key - let i := alloc.vec.Vec.len (List T) self.slots + let i := alloc.vec.Vec.len (AList T) self.slots let hash_mod ← hash % i - let (l, index_mut_back) ← - alloc.vec.Vec.index_mut (List T) Usize - (core.slice.index.SliceIndexUsizeSliceTInst (List T)) self.slots hash_mod - let (t, get_mut_in_list_back) ← HashMap.get_mut_in_list T l key + let (a, index_mut_back) ← + alloc.vec.Vec.index_mut (AList T) Usize + (core.slice.index.SliceIndexUsizeSliceTInst (AList T)) self.slots + hash_mod + let (t, get_mut_in_list_back) ← HashMap.get_mut_in_list T a key let back := fun ret => do - let l1 ← get_mut_in_list_back ret - let v ← index_mut_back l1 + let a1 ← get_mut_in_list_back ret + let v ← index_mut_back a1 Result.ok { self with slots := v } Result.ok (t, back) /- [hashmap::{hashmap::HashMap}::remove_from_list]: loop 0: - Source: 'tests/src/hashmap.rs', lines 275:4-301:5 -/ + Source: 'tests/src/hashmap.rs', lines 276:4-302:5 -/ divergent def HashMap.remove_from_list_loop - (T : Type) (key : Usize) (ls : List T) : Result ((Option T) × (List T)) := + (T : Type) (key : Usize) (ls : AList T) : Result ((Option T) × (AList T)) := match ls with - | List.Cons ckey t tl => + | AList.Cons ckey t tl => if ckey = key then let (mv_ls, _) := - core.mem.replace (List T) (List.Cons ckey t tl) List.Nil + core.mem.replace (AList T) (AList.Cons ckey t tl) AList.Nil match mv_ls with - | List.Cons _ cvalue tl1 => Result.ok (some cvalue, tl1) - | List.Nil => Result.fail .panic + | AList.Cons _ cvalue tl1 => Result.ok (some cvalue, tl1) + | AList.Nil => Result.fail .panic else do let (o, tl1) ← HashMap.remove_from_list_loop T key tl - Result.ok (o, List.Cons ckey t tl1) - | List.Nil => Result.ok (none, List.Nil) + Result.ok (o, AList.Cons ckey t tl1) + | AList.Nil => Result.ok (none, AList.Nil) /- [hashmap::{hashmap::HashMap}::remove_from_list]: - Source: 'tests/src/hashmap.rs', lines 275:4-275:69 -/ + Source: 'tests/src/hashmap.rs', lines 276:4-276:70 -/ def HashMap.remove_from_list - (T : Type) (key : Usize) (ls : List T) : Result ((Option T) × (List T)) := + (T : Type) (key : Usize) (ls : AList T) : Result ((Option T) × (AList T)) := HashMap.remove_from_list_loop T key ls /- [hashmap::{hashmap::HashMap}::remove]: - Source: 'tests/src/hashmap.rs', lines 304:4-304:52 -/ + Source: 'tests/src/hashmap.rs', lines 305:4-305:52 -/ def HashMap.remove (T : Type) (self : HashMap T) (key : Usize) : Result ((Option T) × (HashMap T)) := do let hash ← hash_key key - let i := alloc.vec.Vec.len (List T) self.slots + let i := alloc.vec.Vec.len (AList T) self.slots let hash_mod ← hash % i - let (l, index_mut_back) ← - alloc.vec.Vec.index_mut (List T) Usize - (core.slice.index.SliceIndexUsizeSliceTInst (List T)) self.slots hash_mod - let (x, l1) ← HashMap.remove_from_list T key l + let (a, index_mut_back) ← + alloc.vec.Vec.index_mut (AList T) Usize + (core.slice.index.SliceIndexUsizeSliceTInst (AList T)) self.slots + hash_mod + let (x, a1) ← HashMap.remove_from_list T key a match x with | none => do - let v ← index_mut_back l1 + let v ← index_mut_back a1 Result.ok (none, { self with slots := v }) | some x1 => do let i1 ← self.num_entries - 1#usize - let v ← index_mut_back l1 + let v ← index_mut_back a1 Result.ok (some x1, { self with num_entries := i1, slots := v }) /- [hashmap::insert_on_disk]: - Source: 'tests/src/hashmap.rs', lines 335:0-335:43 -/ + Source: 'tests/src/hashmap.rs', lines 336:0-336:43 -/ def insert_on_disk (key : Usize) (value : U64) (st : State) : Result (State × Unit) := do @@ -391,7 +396,7 @@ def insert_on_disk utils.serialize hm1 st1 /- [hashmap::test1]: - Source: 'tests/src/hashmap.rs', lines 350:0-350:10 -/ + Source: 'tests/src/hashmap.rs', lines 351:0-351:10 -/ def test1 : Result Unit := do let hm ← HashMap.new U64 diff --git a/tests/lean/Hashmap/FunsExternal_Template.lean b/tests/lean/Hashmap/FunsExternal_Template.lean index 2af57d10..80362a92 100644 --- a/tests/lean/Hashmap/FunsExternal_Template.lean +++ b/tests/lean/Hashmap/FunsExternal_Template.lean @@ -7,10 +7,10 @@ open Primitives open hashmap /- [hashmap::utils::deserialize]: - Source: 'tests/src/hashmap.rs', lines 330:4-330:47 -/ + Source: 'tests/src/hashmap.rs', lines 331:4-331:47 -/ axiom utils.deserialize : State → Result (State × (HashMap U64)) /- [hashmap::utils::serialize]: - Source: 'tests/src/hashmap.rs', lines 325:4-325:46 -/ + Source: 'tests/src/hashmap.rs', lines 326:4-326:46 -/ axiom utils.serialize : HashMap U64 → State → Result (State × Unit) diff --git a/tests/lean/Hashmap/Types.lean b/tests/lean/Hashmap/Types.lean index b4301106..93af883e 100644 --- a/tests/lean/Hashmap/Types.lean +++ b/tests/lean/Hashmap/Types.lean @@ -6,18 +6,18 @@ open Primitives namespace hashmap -/- [hashmap::List] - Source: 'tests/src/hashmap.rs', lines 29:0-29:16 -/ -inductive List (T : Type) := -| Cons : Usize → T → List T → List T -| Nil : List T +/- [hashmap::AList] + Source: 'tests/src/hashmap.rs', lines 30:0-30:17 -/ +inductive AList (T : Type) := +| Cons : Usize → T → AList T → AList T +| Nil : AList T /- [hashmap::HashMap] - Source: 'tests/src/hashmap.rs', lines 45:0-45:21 -/ + Source: 'tests/src/hashmap.rs', lines 46:0-46:21 -/ structure HashMap (T : Type) where num_entries : Usize max_load_factor : (Usize × Usize) max_load : Usize - slots : alloc.vec.Vec (List T) + slots : alloc.vec.Vec (AList T) end hashmap -- cgit v1.2.3 From 68e623b037a07c986f1a84e21196b9eee29a0d8e Mon Sep 17 00:00:00 2001 From: Son Ho Date: Mon, 17 Jun 2024 06:39:16 +0200 Subject: Update Hashmap/Properties.lean --- tests/lean/Hashmap/Properties.lean | 70 ++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 36 deletions(-) (limited to 'tests/lean/Hashmap') diff --git a/tests/lean/Hashmap/Properties.lean b/tests/lean/Hashmap/Properties.lean index 5be778e7..a2fa0d7d 100644 --- a/tests/lean/Hashmap/Properties.lean +++ b/tests/lean/Hashmap/Properties.lean @@ -9,7 +9,7 @@ namespace List -- 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 α := +def lookup' {α : Type} (ls: List (Usize × α)) (key: Usize) : Option α := match ls with | [] => none | (k, x) :: tl => if k = key then some x else lookup' tl key @@ -18,29 +18,27 @@ end List namespace hashmap -namespace List +namespace AList -def v {α : Type} (ls: List α) : _root_.List (Usize × α) := +def v {α : Type} (ls: AList α) : List (Usize × α) := match ls with | Nil => [] | Cons k x tl => (k, x) :: v tl -@[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] theorem v_nil (α : Type) : (Nil : AList α).v = [] := by rfl +@[simp] theorem v_cons {α : Type} k x (tl: AList α) : (Cons k x tl).v = (k, x) :: v tl := by rfl @[simp] -abbrev lookup {α : Type} (ls: List α) (key: Usize) : Option α := +abbrev lookup {α : Type} (ls: AList α) (key: Usize) : Option α := ls.v.lookup' key @[simp] -abbrev len {α : Type} (ls : List α) : Int := ls.v.len +abbrev len {α : Type} (ls : AList α) : Int := ls.v.len -end List +end AList namespace HashMap -abbrev Core.List := _root_.List - namespace List end List @@ -55,7 +53,7 @@ theorem match_lawful_beq [BEq α] [LawfulBEq α] [DecidableEq α] (x y : α) : (x == y) = (if x = y then true else false) := by split <;> simp_all -def distinct_keys (ls : Core.List (Usize × α)) := ls.pairwise_rel (λ x y => x.fst ≠ y.fst) +def distinct_keys (ls : 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 @@ -66,33 +64,33 @@ def hash_mod_key (k : Usize) (l : Int) : Int := 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 := +def slot_s_inv_hash (l i : Int) (ls : 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 := +def slot_s_inv (l i : Int) (ls : 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 +def slot_t_inv (l i : Int) (s : AList α) : 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 +def v (hm : HashMap α) : List (List (Usize × α)) := + hm.slots.val.map AList.v -- Interpret the hashmap as an associative list -def al_v (hm : HashMap α) : Core.List (Usize × α) := +def al_v (hm : HashMap α) : List (Usize × α) := hm.v.flatten -- TODO: automatic derivation -instance : Inhabited (List α) where +instance : Inhabited (AList α) where default := .Nil @[simp] -def slots_s_inv (s : Core.List (List α)) : Prop := +def slots_s_inv (s : List (AList α)) : Prop := ∀ (i : Int), 0 ≤ i → i < s.len → slot_t_inv s.len i (s.index i) -def slots_t_inv (s : alloc.vec.Vec (List α)) : Prop := +def slots_t_inv (s : alloc.vec.Vec (AList α)) : Prop := slots_s_inv s.v @[simp] @@ -117,7 +115,7 @@ attribute [-simp] Bool.exists_bool -- of heart beats set_option maxHeartbeats 1000000 -theorem insert_in_list_spec_aux {α : Type} (l : Int) (key: Usize) (value: α) (l0: List α) +theorem insert_in_list_spec_aux {α : Type} (l : Int) (key: Usize) (value: α) (l0: AList α) (hinv : slot_s_inv_hash l (hash_mod_key key l) l0.v) (hdk : distinct_keys l0.v) : ∃ b l1, @@ -144,7 +142,7 @@ theorem insert_in_list_spec_aux {α : Type} (l : Int) (key: Usize) (value: α) ( simp [insert_in_list] rw [insert_in_list_loop] simp (config := {contextual := true}) - [List.v, slot_s_inv_hash, distinct_keys, List.pairwise_rel] + [AList.v, slot_s_inv_hash, distinct_keys, List.pairwise_rel] | Cons k v tl0 => if h: k = key then rw [insert_in_list] @@ -153,7 +151,7 @@ theorem insert_in_list_spec_aux {α : Type} (l : Int) (key: Usize) (value: α) ( exists false; simp -- TODO: why do we need to do this? split_conjs . intros; simp [*] - . simp [List.v, slot_s_inv_hash] at * + . simp [AList.v, slot_s_inv_hash] at * simp [*] . simp [*, distinct_keys] at * apply hdk @@ -162,17 +160,17 @@ theorem insert_in_list_spec_aux {α : Type} (l : Int) (key: Usize) (value: α) ( rw [insert_in_list] rw [insert_in_list_loop] 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 + have : slot_s_inv_hash l (hash_mod_key key l) (AList.v tl0) := by + simp_all [AList.v, slot_s_inv_hash] + have : distinct_keys (AList.v tl0) := by simp [distinct_keys] at hdk simp [hdk, distinct_keys] progress keep heq as ⟨ b, tl1 .. ⟩ simp only [insert_in_list] 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 * + have : slot_s_inv_hash l (hash_mod_key key l) (AList.v (AList.Cons k v tl1)) := by + simp [AList.v, slot_s_inv_hash] at * simp [*] - have : distinct_keys ((k, v) :: List.v tl1) := by + have : distinct_keys ((k, v) :: AList.v tl1) := by simp [distinct_keys] at * simp [*] -- TODO: canonize addition by default? @@ -180,7 +178,7 @@ theorem insert_in_list_spec_aux {α : Type} (l : Int) (key: Usize) (value: α) ( simp_all [Int.add_assoc, Int.add_comm, Int.add_left_comm] @[pspec] -theorem insert_in_list_spec {α : Type} (l : Int) (key: Usize) (value: α) (l0: List α) +theorem insert_in_list_spec {α : Type} (l : Int) (key: Usize) (value: α) (l0: AList α) (hinv : slot_s_inv_hash l (hash_mod_key key l) l0.v) (hdk : distinct_keys l0.v) : ∃ b l1, @@ -203,7 +201,7 @@ theorem insert_in_list_spec {α : Type} (l : Int) (key: Usize) (value: α) (l0: exists l1 @[simp] -def slots_t_lookup (s : Core.List (List α)) (k : Usize) : Option α := +def slots_t_lookup (s : List (AList α)) (k : Usize) : Option α := let i := hash_mod_key k s.len let slot := s.index i slot.lookup k @@ -254,7 +252,7 @@ theorem insert_no_resize_spec {α : Type} (hm : HashMap α) (key : Usize) (value rw [insert_no_resize] -- Simplify. Note that this also simplifies some function calls, like array index simp [hash_key, bind_tc_ok] - have _ : (alloc.vec.Vec.len (List α) hm.slots).val ≠ 0 := by + have _ : (alloc.vec.Vec.len (AList α) hm.slots).val ≠ 0 := by intro simp_all [inv] progress as ⟨ hash_mod, hhm ⟩ @@ -344,11 +342,11 @@ theorem insert_no_resize_spec {α : Type} (hm : HashMap α) (key : Usize) (value 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] + simp [List.update_map_eq] -- TODO: dependent rewrites - have _ : key.val % hm.slots.val.len < (List.map List.v hm.slots.val).len := by + have _ : key.val % hm.slots.val.len < (List.map AList.v hm.slots.val).len := by simp [*] - simp [_root_.List.len_flatten_update_eq, *] + simp [List.len_flatten_update_eq, *] split <;> rename_i heq <;> simp [heq] at hlen <;> @@ -371,7 +369,7 @@ theorem insert_no_resize_spec {α : Type} (hm : HashMap α) (key : Usize) (value have _ := hinv.right.left i hipos (by simp_all) 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 + if h_ieq : i = key.val % 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 -- cgit v1.2.3 From 1021cdea98043dd935dbc8dbe633b90fda68047d Mon Sep 17 00:00:00 2001 From: Son Ho Date: Mon, 17 Jun 2024 07:15:26 +0200 Subject: Update the tests --- tests/lean/Hashmap/Funs.lean | 8 ++++++++ tests/lean/Hashmap/Properties.lean | 3 +-- 2 files changed, 9 insertions(+), 2 deletions(-) (limited to 'tests/lean/Hashmap') diff --git a/tests/lean/Hashmap/Funs.lean b/tests/lean/Hashmap/Funs.lean index 7d8f73a7..f5d028db 100644 --- a/tests/lean/Hashmap/Funs.lean +++ b/tests/lean/Hashmap/Funs.lean @@ -28,6 +28,7 @@ divergent def HashMap.allocate_slots_loop /- [hashmap::{hashmap::HashMap}::allocate_slots]: Source: 'tests/src/hashmap.rs', lines 61:4-61:78 -/ +@[reducible] def HashMap.allocate_slots (T : Type) (slots : alloc.vec.Vec (AList T)) (n : Usize) : Result (alloc.vec.Vec (AList T)) @@ -106,6 +107,7 @@ divergent def HashMap.insert_in_list_loop /- [hashmap::{hashmap::HashMap}::insert_in_list]: Source: 'tests/src/hashmap.rs', lines 108:4-108:72 -/ +@[reducible] def HashMap.insert_in_list (T : Type) (key : Usize) (value : T) (ls : AList T) : Result (Bool × (AList T)) @@ -150,6 +152,7 @@ divergent def HashMap.move_elements_from_list_loop /- [hashmap::{hashmap::HashMap}::move_elements_from_list]: Source: 'tests/src/hashmap.rs', lines 194:4-194:73 -/ +@[reducible] def HashMap.move_elements_from_list (T : Type) (ntable : HashMap T) (ls : AList T) : Result (HashMap T) := HashMap.move_elements_from_list_loop T ntable ls @@ -177,6 +180,7 @@ divergent def HashMap.move_elements_loop /- [hashmap::{hashmap::HashMap}::move_elements]: Source: 'tests/src/hashmap.rs', lines 182:4-182:96 -/ +@[reducible] def HashMap.move_elements (T : Type) (ntable : HashMap T) (slots : alloc.vec.Vec (AList T)) (i : Usize) : @@ -234,6 +238,7 @@ divergent def HashMap.contains_key_in_list_loop /- [hashmap::{hashmap::HashMap}::contains_key_in_list]: Source: 'tests/src/hashmap.rs', lines 217:4-217:69 -/ +@[reducible] def HashMap.contains_key_in_list (T : Type) (key : Usize) (ls : AList T) : Result Bool := HashMap.contains_key_in_list_loop T key ls @@ -265,6 +270,7 @@ divergent def HashMap.get_in_list_loop /- [hashmap::{hashmap::HashMap}::get_in_list]: Source: 'tests/src/hashmap.rs', lines 235:4-235:71 -/ +@[reducible] def HashMap.get_in_list (T : Type) (key : Usize) (ls : AList T) : Result T := HashMap.get_in_list_loop T key ls @@ -306,6 +312,7 @@ divergent def HashMap.get_mut_in_list_loop /- [hashmap::{hashmap::HashMap}::get_mut_in_list]: Source: 'tests/src/hashmap.rs', lines 256:4-256:87 -/ +@[reducible] def HashMap.get_mut_in_list (T : Type) (ls : AList T) (key : Usize) : Result (T × (T → Result (AList T))) @@ -356,6 +363,7 @@ divergent def HashMap.remove_from_list_loop /- [hashmap::{hashmap::HashMap}::remove_from_list]: Source: 'tests/src/hashmap.rs', lines 276:4-276:70 -/ +@[reducible] def HashMap.remove_from_list (T : Type) (key : Usize) (ls : AList T) : Result ((Option T) × (AList T)) := HashMap.remove_from_list_loop T key ls diff --git a/tests/lean/Hashmap/Properties.lean b/tests/lean/Hashmap/Properties.lean index a2fa0d7d..29b5834b 100644 --- a/tests/lean/Hashmap/Properties.lean +++ b/tests/lean/Hashmap/Properties.lean @@ -165,8 +165,7 @@ theorem insert_in_list_spec_aux {α : Type} (l : Int) (key: Usize) (value: α) ( have : distinct_keys (AList.v tl0) := by simp [distinct_keys] at hdk simp [hdk, distinct_keys] - progress keep heq as ⟨ b, tl1 .. ⟩ - simp only [insert_in_list] at heq + progress as ⟨ b, tl1 .. ⟩ have : slot_s_inv_hash l (hash_mod_key key l) (AList.v (AList.Cons k v tl1)) := by simp [AList.v, slot_s_inv_hash] at * simp [*] -- cgit v1.2.3 From cf7cd476b32cd562ca90950e4b3c29c9fc42028a Mon Sep 17 00:00:00 2001 From: Son Ho Date: Mon, 17 Jun 2024 07:25:17 +0200 Subject: Regenerate the tests --- tests/lean/Hashmap/Funs.lean | 3 +++ tests/lean/Hashmap/FunsExternal_Template.lean | 3 +++ tests/lean/Hashmap/Types.lean | 3 +++ tests/lean/Hashmap/TypesExternal_Template.lean | 3 +++ 4 files changed, 12 insertions(+) (limited to 'tests/lean/Hashmap') diff --git a/tests/lean/Hashmap/Funs.lean b/tests/lean/Hashmap/Funs.lean index f5d028db..7972b715 100644 --- a/tests/lean/Hashmap/Funs.lean +++ b/tests/lean/Hashmap/Funs.lean @@ -4,6 +4,9 @@ import Base import Hashmap.Types import Hashmap.FunsExternal open Primitives +set_option linter.dupNamespace false +set_option linter.hashCommand false +set_option linter.unusedVariables false namespace hashmap diff --git a/tests/lean/Hashmap/FunsExternal_Template.lean b/tests/lean/Hashmap/FunsExternal_Template.lean index 80362a92..ea5ceed3 100644 --- a/tests/lean/Hashmap/FunsExternal_Template.lean +++ b/tests/lean/Hashmap/FunsExternal_Template.lean @@ -4,6 +4,9 @@ import Base import Hashmap.Types open Primitives +set_option linter.dupNamespace false +set_option linter.hashCommand false +set_option linter.unusedVariables false open hashmap /- [hashmap::utils::deserialize]: diff --git a/tests/lean/Hashmap/Types.lean b/tests/lean/Hashmap/Types.lean index 93af883e..6f5d99a5 100644 --- a/tests/lean/Hashmap/Types.lean +++ b/tests/lean/Hashmap/Types.lean @@ -3,6 +3,9 @@ import Base import Hashmap.TypesExternal open Primitives +set_option linter.dupNamespace false +set_option linter.hashCommand false +set_option linter.unusedVariables false namespace hashmap diff --git a/tests/lean/Hashmap/TypesExternal_Template.lean b/tests/lean/Hashmap/TypesExternal_Template.lean index 03c3d157..b6f24513 100644 --- a/tests/lean/Hashmap/TypesExternal_Template.lean +++ b/tests/lean/Hashmap/TypesExternal_Template.lean @@ -3,6 +3,9 @@ -- This is a template file: rename it to "TypesExternal.lean" and fill the holes. import Base open Primitives +set_option linter.dupNamespace false +set_option linter.hashCommand false +set_option linter.unusedVariables false /- The state type used in the state-error monad -/ axiom State : Type -- cgit v1.2.3