Function:
(defun bytes-to-charlist (bytes) (declare (xargs :guard (byte-listp bytes))) (let ((__function__ 'bytes-to-charlist)) (declare (ignorable __function__)) (if (endp bytes) nil (cons (code-char (car bytes)) (bytes-to-charlist (cdr bytes))))))
Theorem:
(defthm character-listp-of-bytes-to-charlist (implies (force (byte-listp bytes)) (b* ((lst (bytes-to-charlist bytes))) (character-listp lst))) :rule-classes :type-prescription)
Function:
(defun charlist-to-bytes (charlist) (declare (xargs :guard (character-listp charlist))) (let ((__function__ 'charlist-to-bytes)) (declare (ignorable __function__)) (if (endp charlist) nil (cons (char-code (car charlist)) (charlist-to-bytes (cdr charlist))))))
Theorem:
(defthm byte-listp-of-charlist-to-bytes (implies (force (character-listp bytes)) (b* ((lst (charlist-to-bytes charlist))) (byte-listp lst))) :rule-classes :type-prescription)
Theorem:
(defthm same-length-of-byte-and-character-lists (implies (character-listp charlist) (equal (len (charlist-to-bytes charlist)) (len charlist))))
Function:
(defun string-to-bytes (str) (declare (xargs :guard (stringp str))) (let ((__function__ 'string-to-bytes)) (declare (ignorable __function__)) (charlist-to-bytes (coerce str 'list))))
Theorem:
(defthm byte-listp-of-string-to-bytes (implies (force (stringp str)) (b* ((lst (string-to-bytes str))) (byte-listp lst))) :rule-classes :type-prescription)
Function:
(defun bytes-to-string (bytes) (declare (xargs :guard (byte-listp bytes))) (let ((__function__ 'bytes-to-string)) (declare (ignorable __function__)) (coerce (bytes-to-charlist bytes) 'string)))
Theorem:
(defthm stringp-of-bytes-to-string (implies (force (byte-listp bytes)) (b* ((lst (bytes-to-string bytes))) (stringp lst))) :rule-classes :type-prescription)
Function:
(defun read-n-bytes-from-string (n str) (declare (xargs :guard (and (natp n) (stringp str)))) (let ((__function__ 'read-n-bytes-from-string)) (declare (ignorable __function__)) (let* ((bytes (string-to-bytes str)) (n-bytes (take n bytes))) n-bytes)))
Theorem:
(defthm take-byte-listp (implies (and (byte-listp xs) (natp n) (<= n (len xs))) (byte-listp (take n xs))))
Theorem:
(defthm byte-listp-read-n-bytes-from-string (implies (and (natp n) (< n (len (string-to-bytes str))) (stringp str)) (byte-listp (read-n-bytes-from-string n str))) :rule-classes :type-prescription)
Function:
(defun read-n-bytes-from-string-as-string (n str) (declare (xargs :guard (and (natp n) (stringp str)))) (let ((__function__ 'read-n-bytes-from-string-as-string)) (declare (ignorable __function__)) (b* ((n-bytes (read-n-bytes-from-string n str)) ((when (not (byte-listp n-bytes))) nil) (new-charlist (bytes-to-charlist n-bytes)) (new-str (coerce new-charlist 'string))) new-str)))
Theorem:
(defthm stringp-read-n-bytes-from-string-as-string (implies (and (natp n) (< n (len (string-to-bytes str))) (stringp str)) (stringp (read-n-bytes-from-string-as-string n str))) :rule-classes :type-prescription)