Write a value for a key in an MMP tree.
(mmp-write key value root database) → (mv error? new-root new-database)
We require (in the guard) that the MMP tree is valid, i.e. it encodes a finite map.
We provide a high-level definition here. We decode the MMP tree, we update the key, and we re-encode the map. The key may already be present, in which case the old value is overwritten. If the key is not present, it is added to the map.
The
If the re-encoding of the new finite map causes a collision
or produces some non-RLP-encodable data,
we forward the error flag returned by mmp-encode.
We also check for collision when taking the union of
the initial database and the one returned by mmp-encode,
returning
Function:
(defun mmp-write (key value root database) (declare (xargs :guard (and (byte-listp key) (byte-listp value) (byte-listp root) (databasep database)))) (declare (xargs :guard (mmp-encoding-p root database))) (b* (((mv & map) (mmp-decode root database)) (new-map (omap::update (byte-list-fix key) (byte-list-fix value) map)) ((mv error? new-root new-database-min) (mmp-encode new-map)) ((when error?) (mv error? nil nil)) ((unless (omap::compatiblep new-database-min (database-fix database))) (mv :collision nil nil)) (new-database (omap::update* new-database-min (database-fix database)))) (mv nil new-root new-database)))
Theorem:
(defthm return-type-of-mmp-write.error? (b* (((mv ?error? ?new-root ?new-database) (mmp-write key value root database))) (member-eq error? '(nil :collision :rlp))) :rule-classes :rewrite)
Theorem:
(defthm byte-listp-of-mmp-write.new-root (b* (((mv ?error? ?new-root ?new-database) (mmp-write key value root database))) (byte-listp new-root)) :rule-classes :rewrite)
Theorem:
(defthm databasep-of-mmp-write.new-database (b* (((mv ?error? ?new-root ?new-database) (mmp-write key value root database))) (databasep new-database)) :rule-classes :rewrite)
Theorem:
(defthm mmp-write-of-byte-list-fix-key (equal (mmp-write (byte-list-fix key) value root database) (mmp-write key value root database)))
Theorem:
(defthm mmp-write-byte-list-equiv-congruence-on-key (implies (byte-list-equiv key key-equiv) (equal (mmp-write key value root database) (mmp-write key-equiv value root database))) :rule-classes :congruence)
Theorem:
(defthm mmp-write-of-byte-list-fix-value (equal (mmp-write key (byte-list-fix value) root database) (mmp-write key value root database)))
Theorem:
(defthm mmp-write-byte-list-equiv-congruence-on-value (implies (byte-list-equiv value value-equiv) (equal (mmp-write key value root database) (mmp-write key value-equiv root database))) :rule-classes :congruence)
Theorem:
(defthm mmp-write-of-byte-list-fix-root (equal (mmp-write key value (byte-list-fix root) database) (mmp-write key value root database)))
Theorem:
(defthm mmp-write-byte-list-equiv-congruence-on-root (implies (byte-list-equiv root root-equiv) (equal (mmp-write key value root database) (mmp-write key value root-equiv database))) :rule-classes :congruence)
Theorem:
(defthm mmp-write-of-database-fix-database (equal (mmp-write key value root (database-fix database)) (mmp-write key value root database)))
Theorem:
(defthm mmp-write-database-equiv-congruence-on-database (implies (database-equiv database database-equiv) (equal (mmp-write key value root database) (mmp-write key value root database-equiv))) :rule-classes :congruence)