module scheme import hash.fnv import math import math.big // All eqv?/equal? numeric hashing goes through this helper. New exact-number // tags must emit the same signed numerator/positive denominator word encoding. fn numeric_equivalence_hash(value Value) ?u64 { match value.tag { .integer { return hash_small_exact_parts(value.whole(), 1) } .rational { part := value.rational_pointer() return hash_small_exact_parts(part.numerator, part.denominator) } .big_integer, .big_rational { numerator, denominator := big_exact_parts(value) or { return none } return hash_big_component(hash_big_component(hash_word(fnv.offset64, 0x4558414354), numerator), denominator) } .exact_complex, .inexact_complex { re, im := complex_parts(value) rh := numeric_equivalence_hash(re) or { return none } ih := numeric_equivalence_hash(im) or { return none } return hash_word(hash_word(hash_word(fnv.offset64, 0x434f4d504c4558), rh), ih) } .real { // eqv? identifies both signed zeros in this dialect. bits := if value.floating() == 0 { u64(0) } else { math.f64_bits(value.floating()) } return hash_word(hash_word(fnv.offset64, 0x5245414c), bits) } else { return none } } } fn hash_word(hash u64, word u64) u64 { mut bytes := [8]u8{} for index in 0 .. 8 { bytes[index] = u8(word >> (index * 8)) } return fnv.write64a(hash, unsafe { (&u8(bytes)).vbytes(8) }) } fn hash_small_component(hash u64, value i64) u64 { negative := value < 0 absolute := if negative { u64(-(value + 1)) + 1 } else { u64(value) } words := if absolute == 0 { 0 } else if absolute <= 0xffffffff { 1 } else { 2 } mut result := hash_word(hash_word(hash, u64(negative)), u64(words)) if words > 0 { result = hash_word(result, u64(u32(absolute))) } if words > 1 { result = hash_word(result, absolute >> 32) } return result } // The same signed magnitude stream as hash_small_component, read through // approved immutable public-header views. Fixed work also keeps every bit index // below 65536, even when bit_len itself exceeds the u32 get_bit index range. const big_hash_word_limit = 2048 fn hash_big_component(hash u64, value big.Integer) u64 { absolute := big_abs_view(value) bits := absolute.bit_len() words := bits / 32 + int(bits % 32 != 0) mut result := hash_word(hash_word(hash, u64(value.signum < 0)), u64(words)) observed := if words < big_hash_word_limit { words } else { big_hash_word_limit } for index in 0 .. observed { mut word := u64(0) for bit in 0 .. 32 { // index < 2048 proves this conversion is lossless and in range. if absolute.get_bit(u32(index * 32 + bit)) { word |= u64(1) << bit } } result = hash_word(result, word) } if words > observed { result = hash_word(result, 0x424947435554) } return result } fn hash_small_exact_parts(numerator i64, denominator i64) u64 { return hash_small_component(hash_small_component(hash_word(fnv.offset64, 0x4558414354), numerator), denominator) } fn hash_raw_text(text string) u64 { return fnv.write64a(fnv.offset64, unsafe { text.str.vbytes(text.len) }) } fn identity_key_hash(value Value) u64 { if numeric := numeric_equivalence_hash(value) { return numeric } hash := hash_word(fnv.offset64, u64(value.tag)) return match value.tag { .unspecified, .nil { hash } .boolean, .character, .codec, .transcoder { hash_word(hash, u64(value.whole())) } .symbol, .keyword { hash_word(hash, hash_raw_text(value.text())) } .syntax { hash_word(hash_word(hash, u64(value.pointer())), u64(u32(value.syntax_index))) } else { hash_word(hash, u64(value.pointer())) } } } fn equal_leaf_hash(value Value) u64 { if value.tag == .string { return hash_word(hash_word(fnv.offset64, u64(value.tag)), hash_raw_text(value.text())) } if value.tag == .bytevector { return hash_word(hash_word(fnv.offset64, u64(value.tag)), fnv.sum64a(value.bytevector_pointer().bytes)) } return identity_key_hash(value) } struct StructuralHashFrame { value Value mut: next int = -1 } // These deterministic bounds describe the unfolded ordered tree, never graph // identity or sharing. Thus coinductively equal cycles receive equal hashes. // Fixed stack frames and a visit cap avoid both graph maps and exponential DAG // unfolding. Long/deep keys with equal observed prefixes may collide. @[direct_array_access] fn bounded_structural_hash(root Value) u64 { mut frames := [64]StructuralHashFrame{} frames[0] = StructuralHashFrame{ value: root } mut top := 0 mut visited := 0 mut hash := fnv.offset64 for top >= 0 { value := frames[top].value if frames[top].next < 0 { if visited == 1024 { return hash_word(hash, 0x574f524b435554) } visited++ if value.tag !in [.pair, .vector] { hash = hash_word(hash, equal_leaf_hash(value)) top-- continue } hash = hash_word(hash, u64(value.tag)) if value.tag == .vector { hash = hash_word(hash, u64(value.vector_pointer().items.len)) } if top == frames.len - 1 { hash = hash_word(hash, 0x4445505448435554) top-- continue } frames[top].next = 0 } index := frames[top].next mut child := unspecified() if value.tag == .pair { if index == 2 { top-- continue } pair := value.pair_pointer() child = if index == 0 { pair.car } else { pair.cdr } } else { items := value.vector_pointer().items if index == items.len { top-- continue } child = items[index] } frames[top].next++ top++ frames[top] = StructuralHashFrame{ value: child } } return hash } fn hashtable_equal_hash(mut _vm VM, args []Value) !Value { check_arity('equal-hash', args.len, 1, 1)! hash := if args[0].tag in [.pair, .vector] { bounded_structural_hash(args[0]) } else { equal_leaf_hash(args[0]) } return integer(i64(hash & 0x7fffffffffffffff)) } fn string_key_hash(value Value, fold bool) !Value { text := string_data(value)! mut hash := fnv.offset64 // Match string=? / string-ci=? exactly, including replacement runes in // malformed host UTF-8. equal? itself compares raw string bytes instead. if fold { mut cursor := UnicodeFoldCursor{ text: text } for { ch := cursor.next() or { break } hash = hash_word(hash, u64(ch)) } } else { for character in text.runes_iterator() { hash = hash_word(hash, u64(character)) } } return integer(i64(hash & 0x7fffffffffffffff)) } fn hashtable_string_hash(mut _vm VM, args []Value) !Value { check_arity('string-hash', args.len, 1, 1)! return string_key_hash(args[0], false) } fn hashtable_string_ci_hash(mut _vm VM, args []Value) !Value { check_arity('string-ci-hash', args.len, 1, 1)! return string_key_hash(args[0], true) } fn hashtable_symbol_hash(mut _vm VM, args []Value) !Value { check_arity('symbol-hash', args.len, 1, 1)! if args[0].tag != .symbol { return failure('symbol-hash needs a symbol') } return integer(i64(hash_raw_text(args[0].text()) & 0x7fffffffffffffff)) } // Hash callbacks may return arbitrarily large nonnegative exact integers. // Internal bucket hashes reduce them modulo 2^64 without allocating or narrowing // through the lossy math.big.Integer.int() conversion. fn hashtable_callback_hash(result Value) !u64 { if !is_exact_integer(result) || exact_sign(result) < 0 { return failure('hashtable hash must return a non-negative exact integer') } if result.tag == .integer { return u64(result.whole()) } absolute := big_abs_view(result.big_integer_pointer().value) mut hash := u64(0) for bit in 0 .. 64 { if absolute.get_bit(u32(bit)) { hash |= u64(1) << bit } } return hash }