/usr/share/acl2-4.3/books/str/strline.lisp is in acl2-books-source 4.3-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 208 209 210 211 212 213 214 | ; ACL2 String Library
; Copyright (C) 2009-2010 Centaur Technology
;
; Contact:
; Centaur Technology Formal Verification Group
; 7600-C N. Capital of Texas Highway, Suite 300, Austin, TX 78731, USA.
; http://www.centtech.com/
;
; This program is free software; you can redistribute it and/or modify it under
; the terms of the GNU General Public License as published by the Free Software
; Foundation; either version 2 of the License, or (at your option) any later
; version. This program is distributed in the hope that it will be useful but
; WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
; more details. You should have received a copy of the GNU General Public
; License along with this program; if not, write to the Free Software
; Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
;
; Original author: Jared Davis <jared@centtech.com>
(in-package "STR")
(include-book "doc")
(local (include-book "make-event/assert" :dir :system))
(local (include-book "arithmetic"))
(defund charpos-aux (x n xl char)
(declare (type string x)
(type integer n)
(type integer xl)
(type character char)
(xargs :guard (and (stringp x)
(natp n)
(natp xl)
(<= n xl)
(= xl (length x)))
:measure (nfix (- (nfix xl) (nfix n)))))
(cond ((mbe :logic (zp (- (nfix xl) (nfix n)))
:exec (= n xl))
nil)
((eql (char x n) char)
(mbe :logic (nfix n)
:exec n))
(t
(charpos-aux x
(mbe :logic (+ 1 (nfix n)) :exec (+ 1 n))
xl
char))))
(defthm charpos-aux-is-position-ac-of-nthcdr
(implies (and (stringp x)
(natp n)
(= xl (length x)))
(equal (charpos-aux x n xl char)
(position-ac char (nthcdr n (coerce x 'list)) n)))
:hints(("Goal" :in-theory (enable charpos-aux position-ac))))
(defthm position-ac-lower-bound
(implies (and (position-ac item x acc)
(natp acc))
(<= acc (position-ac item x acc)))
:rule-classes ((:rewrite) (:linear))
:hints(("Goal" :in-theory (enable position-ac))))
(defthm position-ac-upper-bound
(implies (natp acc)
(<= (position-ac item x acc)
(+ acc (len x))))
:rule-classes ((:rewrite) (:linear))
:hints(("Goal" :in-theory (enable position-ac))))
(defund go-to-line (x n xl curr goal)
(declare (xargs :guard (and (stringp x)
(natp n)
(natp xl)
(<= n xl)
(= xl (length x))
(natp curr)
(natp goal))
:measure (nfix (- (nfix xl) (nfix n))))
(type string x)
(type integer n xl curr goal))
(cond ((mbe :logic (zp (- (nfix xl) (nfix n)))
:exec (= n xl))
nil)
((= curr goal)
(mbe :logic (nfix n) :exec n))
(t
(go-to-line x
(mbe :logic (+ 1 (nfix n)) :exec (+ 1 n))
xl
(if (eql (char x n) #\Newline)
(+ 1 curr)
curr)
goal))))
(defthm type-of-go-to-line
(or (not (go-to-line x n xl curr goal))
(and (integerp (go-to-line x n xl curr goal))
(<= 0 (go-to-line x n xl curr goal))))
:rule-classes :type-prescription)
(defthm go-to-line-lower-bound
(implies (and (go-to-line x n xl curr goal)
(natp n))
(<= n (go-to-line x n xl curr goal)))
:rule-classes ((:rewrite) (:linear))
:hints(("Goal" :in-theory (enable go-to-line))))
(defthm go-to-line-upper-bound
(implies (natp xl)
(<= (go-to-line x n xl curr goal)
xl))
:rule-classes ((:rewrite) (:linear))
:hints(("Goal" :in-theory (enable go-to-line))))
(defund strline (n x)
":Doc-Section Str
Extract a line from a string by its line number~/
~c[(strline n x)] extracts the ~c[n]th line from the string ~c[x] and returns
it as a string. It returns the empty string if ~c[n] does not refer to a
valid line. Note that the first line is 1 (not 0).~/~/"
(declare (xargs :guard (and (posp n)
(stringp x))))
(let* ((x (mbe :logic (if (stringp x) x "")
:exec x))
(xl (length x))
(start (go-to-line x 0 xl 1 n)))
(if (not start)
""
(let ((end (charpos-aux x start xl #\Newline)))
(subseq x start end)))))
(defthm stringp-of-strline
(stringp (strline n x))
:rule-classes :type-prescription
:hints(("Goal" :in-theory (enable strline))))
(local (acl2::assert! (equal "foo" (strline 1 "foo
bar
baz"))))
(local (acl2::assert! (equal "bar" (strline 2 "foo
bar
baz"))))
(local (acl2::assert! (equal "baz" (strline 3 "foo
bar
baz"))))
(local (acl2::assert! (equal "" (strline 4 "foo
bar
baz"))))
(defund strlines (a b x)
(declare (type integer a)
(type integer b)
(type string x)
(xargs :guard (and (posp a)
(posp b)
(stringp x))))
(let* ((x (mbe :logic (if (stringp x) x "")
:exec x))
(xl (length x)))
(mv-let
(a b)
(if (<= a b) (mv a b) (mv b a))
(let ((start (go-to-line x 0 xl 1 a)))
(if (not start)
""
(let ((end (go-to-line x start xl a (+ 1 b))))
(subseq x start end)))))))
(defthm stringp-of-strlines
(stringp (strlines a b x))
:rule-classes :type-prescription
:hints(("Goal" :in-theory (enable strlines))))
#||
(defconst *txt* "Line 1
Line 2
Line 3
Line 4
Line 5
Line 6"))
(strlines 1 1 *txt*)
(strlines 1 2 *txt*)
(strlines 1 3 *txt*)
(strlines 1 100 *txt*)
(strlines 2 2 *txt*)
(strlines 2 3 *txt*)
(strlines 2 100 *txt*)
(strlines 2 4 *txt*)
(strlines 2 5 *txt*)
(strlines 2 6 *txt*)
(strlines 2 7 *txt*)
(strlines 7 7 *txt*)
(strlines 7 4 *txt*)
||#
|