1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
; Standard IO Library
; read-file-bytes.lisp -- originally part of the Unicode library
; Copyright (C) 2005-2013 Kookamara LLC
;
; Contact:
;
; Kookamara LLC
; 11410 Windermere Meadows
; Austin, TX 78759, USA
; http://www.kookamara.com/
;
; License: (An MIT/X11-style license)
;
; Permission is hereby granted, free of charge, to any person obtaining a
; copy of this software and associated documentation files (the "Software"),
; to deal in the Software without restriction, including without limitation
; the rights to use, copy, modify, merge, publish, distribute, sublicense,
; and/or sell copies of the Software, and to permit persons to whom the
; Software is furnished to do so, subject to the following conditions:
;
; The above copyright notice and this permission notice shall be included in
; all copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
; DEALINGS IN THE SOFTWARE.
;
; Original author: Jared Davis <jared@kookamara.com>
(in-package "ACL2")
(include-book "file-measure")
(include-book "std/lists/list-defuns" :dir :system)
(include-book "std/typed-lists/unsigned-byte-listp" :dir :system)
(include-book "std/util/bstar" :dir :system)
(local (include-book "base"))
(local (include-book "std/lists/rev" :dir :system))
(local (include-book "std/lists/append" :dir :system))
(local (include-book "std/lists/revappend" :dir :system))
(local (include-book "tools/mv-nth" :dir :system))
(set-state-ok t)
(defsection read-byte$-all
:parents (read-file-bytes)
:short "@(call read-byte$-all) reads all remaining bytes from a file."
:long "<p>This is the main loop inside @(see read-file-bytes). It reads
everything in the file, but doesn't handle opening or closing the file.</p>"
(defund tr-read-byte$-all (channel state acc)
(declare (xargs :guard (and (state-p state)
(symbolp channel)
(open-input-channel-p channel :byte state))
:measure (file-measure channel state)))
(b* (((unless (mbt (state-p state)))
(mv acc state))
((mv byte state) (read-byte$ channel state))
((unless byte)
(mv acc state))
(acc (cons (mbe :logic (if (unsigned-byte-p 8 byte)
byte
0)
:exec byte)
acc)))
(tr-read-byte$-all channel state acc)))
(defund read-byte$-all (channel state)
(declare (xargs :guard (and (state-p state)
(symbolp channel)
(open-input-channel-p channel :byte state))
:measure (file-measure channel state)
:verify-guards nil))
(mbe :logic
(b* (((unless (state-p state))
(mv nil state))
((mv byte state) (read-byte$ channel state))
((unless byte)
(mv nil state))
((mv rest state) (read-byte$-all channel state))
(byte (if (unsigned-byte-p 8 byte) byte 0)))
(mv (cons byte rest) state))
:exec
(b* (((mv contents state)
(tr-read-byte$-all channel state nil)))
(mv (reverse contents) state))))
(local (in-theory (enable tr-read-byte$-all
read-byte$-all)))
(local (defthm lemma-decompose-impl
(equal (tr-read-byte$-all channel state acc)
(list (mv-nth 0 (tr-read-byte$-all channel state acc))
(mv-nth 1 (tr-read-byte$-all channel state acc))))
:rule-classes nil))
(local (defthm lemma-decompose-spec
(equal (read-byte$-all channel state)
(list (mv-nth 0 (read-byte$-all channel state))
(mv-nth 1 (read-byte$-all channel state))))
:rule-classes nil))
(local (defthm lemma-data-equiv
(equal (mv-nth 0 (tr-read-byte$-all channel state acc))
(revappend (mv-nth 0 (read-byte$-all channel state)) acc))))
(local (defthm lemma-state-equiv
(equal (mv-nth 1 (tr-read-byte$-all channel state acc))
(mv-nth 1 (read-byte$-all channel state)))))
(defthm tr-read-byte$-all-removal
(equal (tr-read-byte$-all channel state nil)
(mv (rev (mv-nth 0 (read-byte$-all channel state)))
(mv-nth 1 (read-byte$-all channel state))))
:hints(("Goal" :in-theory (disable tr-read-byte$-all read-byte$-all)
:use ((:instance lemma-decompose-impl (acc nil))
(:instance lemma-decompose-spec)
(:instance lemma-data-equiv (acc nil))))))
(defthm true-listp-of-read-byte$-all
(true-listp (mv-nth 0 (read-byte$-all channel state)))
:rule-classes :type-prescription)
(verify-guards read-byte$-all)
(defthm state-p1-of-read-byte$-all
(implies (and (force (state-p1 state))
(force (symbolp channel))
(force (open-input-channel-p1 channel :byte state)))
(state-p1 (mv-nth 1 (read-byte$-all channel state)))))
(defthm open-input-channel-p1-of-read-byte$-all
(implies (and (force (state-p1 state))
(force (symbolp channel))
(force (open-input-channel-p1 channel :byte state)))
(open-input-channel-p1 channel :byte
(mv-nth 1 (read-byte$-all channel state)))))
(defthm integer-listp-of-read-byte$-all
(integer-listp (mv-nth 0 (read-byte$-all channel state))))
(defthm nat-listp-of-read-byte$-all
(nat-listp (mv-nth 0 (read-byte$-all channel state))))
(defthm unsigned-byte-listp-of-read-byte$-all
(unsigned-byte-listp 8 (mv-nth 0 (read-byte$-all channel state)))))
(defsection read-file-bytes
:parents (std/io)
:short "Read an entire file into a list of (unsigned) bytes."
:long "<p><b>Signature:</b> @(call read-file-bytes) returns @('(mv contents
state)').</p>
<p>On success, @('contents') is a list of bytes, 0-255, which captures the
contents of the file.</p>
<p>On failure, e.g., perhaps @('filename') does not exist, @('contents') will
be a @(see stringp) saying that we failed to open the file.</p>"
(defund read-file-bytes (filename state)
"Returns (MV ERRMSG/BYTES STATE)"
(declare (xargs :guard (and (state-p state)
(stringp filename))))
(b* ((filename (mbe :logic (if (stringp filename) filename "")
:exec filename))
((mv channel state)
(open-input-channel filename :byte state))
((unless channel)
(mv (concatenate 'string "Error opening file " filename)
state))
((mv data state)
(read-byte$-all channel state))
(state (close-input-channel channel state)))
(mv data state)))
(local (in-theory (enable read-file-bytes)))
(defthm state-p1-of-read-file-bytes
(implies (force (state-p1 state))
(state-p1 (mv-nth 1 (read-file-bytes filename state)))))
(defthm integer-listp-of-read-file-bytes
(equal (integer-listp (mv-nth 0 (read-file-bytes filename state)))
(not (stringp (mv-nth 0 (read-file-bytes filename state))))))
(defthm nat-listp-of-read-file-bytes
(equal (nat-listp (mv-nth 0 (read-file-bytes filename state)))
(not (stringp (mv-nth 0 (read-file-bytes filename state))))))
(defthm unsigned-byte-listp-of-read-file-bytes
(equal (unsigned-byte-listp 8 (mv-nth 0 (read-file-bytes filename state)))
(not (stringp (mv-nth 0 (read-file-bytes filename state))))))
(defthm type-of-read-file-bytes
(or (true-listp (mv-nth 0 (read-file-bytes filename state)))
(stringp (mv-nth 0 (read-file-bytes filename state))))
:rule-classes :type-prescription))
|