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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
|
; ACL2 Standard Library
; Copyright (c) 2008-2015 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/
;
; 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@centtech.com>
(in-package "STD")
(include-book "defaggregate")
(include-book "deflist")
(set-state-ok t)
(program)
(defxdoc defsum
:parents (std/util)
:short "(Beta) Introduce a tagged union data type, also commonly called a
variant or sum type."
:long "<box><p>Note: Defsum is currently under development. You are welcome
to use it, but please be advised that we may drastically change its interface
and implementation.</p></box>
<h3>Introduction</h3>
<p><b>Defsum</b> is a macro that automates introducing a recognizer,
pattern-binding macro, and certain supporting theorems for new <a
href='https://en.wikipedia.org/wiki/Tagged_union'>tagged union</a> data
types.</p>
<p>Defsum is currently restricted to unions of tagged aggregates, which must be
introduced ahead of time using @(see defaggregate), and does not currently
support mutual recursive data types. (In the future we may work to lift these
restrictions).</p>")
(defconst *defsum-valid-keywords*
'(:mode
:parents
:short
:long
))
#|| ;; For testing
(encapsulate
()
(logic)
(defaggregate foo :tag :foo (a b c))
(defaggregate bar :tag :bar (x y z))
(defaggregate baz :tag :baz (w)))
||#
(defun ds-find-aggregates
(names ;; aggregate names, e.g., (FOO BAR) for (defaggregate foo ...) (defaggregate bar ...)
agginfo-alist ;; alist of name -> agginfo returned by get-aggregates
)
;; Returns a list of agginfo structures
(b* (((when (atom names))
nil)
((cons name rest) names)
(look (assoc name agginfo-alist))
((unless look)
(er hard? 'ds-find-aggregates
"Aggregate ~x0 not found; has it been defined with defaggregate?"
name)))
(cons (cdr look)
(ds-find-aggregates rest agginfo-alist))))
#||
(make-event
`(defconst *agginfos*
',(ds-find-aggregates '(foo bar baz) (get-aggregates (w state)))))
||#
(defun ds-x (basename)
(intern-in-package-of-symbol "X" basename))
(defun ds-recognizer-name (basename)
(intern-in-package-of-symbol (concatenate 'string (symbol-name basename) "-P")
basename))
(defun ds-recognizer-logic-def-aux (agginfos xvar)
(if (atom agginfos)
nil
(cons `(,(da-recognizer-name (agginfo->name (car agginfos))
(agginfo->pred (car agginfos))) ,xvar)
(ds-recognizer-logic-def-aux (cdr agginfos) xvar))))
(defun ds-recognizer-logic-def (name agginfos)
(cons 'or
(ds-recognizer-logic-def-aux agginfos (ds-x name))))
#||
(ds-recognizer-logic-def 'mysum *agginfos*)
||#
(defun ds-recognizer-exec-def-aux (agginfos xvar)
(cond ((atom agginfos)
nil)
((atom (cdr agginfos))
;; last one, just use "otherwise"
`((otherwise
(,(da-recognizer-name (agginfo->name (car agginfos))
(agginfo->pred (car agginfos))) ,xvar))))
(t
(cons `(,(agginfo->tag (car agginfos))
(,(da-recognizer-name (agginfo->name (car agginfos))
(agginfo->pred (car agginfos))) ,xvar))
(ds-recognizer-exec-def-aux (cdr agginfos) xvar)))))
(defun ds-recognizer-exec-def (name agginfos)
(let ((xvar (ds-x name)))
`(case (tag ,xvar)
. ,(ds-recognizer-exec-def-aux agginfos xvar))))
#||
(ds-recognizer-exec-def 'mysum *agginfos*)
||#
(defun ds-recognizer-def (name agginfos)
(let ((xvar (ds-x name)))
`(defund ,(ds-recognizer-name name) (,xvar)
(declare (xargs :guard t))
(mbe :logic ,(ds-recognizer-logic-def name agginfos)
:exec ,(ds-recognizer-exec-def name agginfos)))))
#||
(ds-recognizer-def 'mysum *agginfos*)
||#
(defun ds-member-implies-sum-thm (name agginfo)
;; This produces theorems like this:
;; (defthm vl-atomguts-p-when-vl-constint-p
;; (implies (vl-constint-p x)
;; (vl-atomguts-p x)))
(b* ((xvar (ds-x name))
(sum-name (ds-recognizer-name name))
((agginfo agginfo) agginfo)
(mem-name (da-recognizer-name agginfo.name agginfo.pred))
(thm-name (intern-in-package-of-symbol
(concatenate 'string (symbol-name sum-name) "-WHEN-"
(symbol-name mem-name))
name)))
`(defthm ,thm-name
(implies (,mem-name ,xvar)
(,sum-name ,xvar)))))
(defun ds-member-implies-sum-thms (name agginfos)
(if (atom agginfos)
nil
(cons (ds-member-implies-sum-thm name (car agginfos))
(ds-member-implies-sum-thms name (cdr agginfos)))))
#||
(ds-member-implies-sum-thms 'mysum *agginfos*)
||#
(defun ds-by-tag-thm (name agginfo)
;; This produces theorems like this:
;; (defthm vl-constint-p-by-tag-when-vl-atomguts-p
;; (implies (and (equal (tag x) :vl-constint)
;; (vl-atomguts-p x))
;; (vl-constint-p x)))
(b* ((xvar (ds-x name))
(sum-name (ds-recognizer-name name))
((agginfo agginfo) agginfo)
(mem-name (da-recognizer-name agginfo.name agginfo.pred))
(thm-name (intern-in-package-of-symbol
(concatenate 'string (symbol-name mem-name)
"-BY-TAG-WHEN-"
(symbol-name sum-name))
name)))
`(defthm ,thm-name
(implies (and (equal (tag ,xvar) ,agginfo.tag)
(,sum-name ,xvar))
(,mem-name ,xvar)))))
(defun ds-by-tag-thms (name agginfos)
(if (atom agginfos)
nil
(cons (ds-by-tag-thm name (car agginfos))
(ds-by-tag-thms name (cdr agginfos)))))
#||
(ds-by-tag-thms 'mysum *agginfos*)
||#
(defun ds-when-invalid-tag-hyps (name agginfos)
(b* (((when (atom agginfos))
nil)
(xvar (ds-x name))
((agginfo agginfo) (car agginfos)))
(cons `(not (equal (tag ,xvar) ,agginfo.tag))
(ds-when-invalid-tag-hyps name (cdr agginfos)))))
(defun ds-when-invalid-tag-thm (name agginfos)
;; Generates a theorem like:
;; (defthm vl-atomicstmt-p-when-invalid-tag
;; (implies (and (not (equal (tag x) :vl-nullstmt))
;; (not (equal (tag x) :vl-assignstmt))
;; (not (equal (tag x) :vl-deassignstmt))
;; (not (equal (tag x) :vl-enablestmt))
;; (not (equal (tag x) :vl-disablestmt))
;; (not (equal (tag x) :vl-eventtriggerstmt)))
;; (not (vl-atomicstmt-p x)))
;; :rule-classes ((:rewrite :backchain-limit-lst 0)))
(b* ((xvar (ds-x name))
(sum-name (ds-recognizer-name name))
(thm-name (intern-in-package-of-symbol
(concatenate 'string (symbol-name sum-name)
"-WHEN-INVALID-TAG")
name)))
`(defthm ,thm-name
(implies (and . ,(ds-when-invalid-tag-hyps name agginfos))
(not (,sum-name ,xvar)))
:rule-classes ((:rewrite :backchain-limit-lst 0)))))
#||
(ds-when-invalid-tag-thm 'mysum *agginfos*)
||#
(defun defsum-fn (name other-args kwd-alist other-events state)
(b* ((__function__ 'deflist)
(?mksym-package-symbol name)
(short (getarg :short nil kwd-alist))
(long (getarg :long nil kwd-alist))
(mode (getarg :mode (default-defun-mode (w state)) kwd-alist))
(parents-p (assoc :parents kwd-alist))
(parents (cdr parents-p))
(parents (if parents-p
parents
(or (xdoc::get-default-parents (w state))
'(acl2::undocumented))))
((unless (or (eq mode :logic)
(eq mode :program)))
(raise ":mode must be one of :logic or :program, but is ~x0." mode))
((unless (or (not short)
(stringp short)))
(raise ":short must be a string or nil, but is ~x0." short))
((unless (or (not long)
(stringp long)))
(raise ":long must be a string or nil, but is ~x0." long))
(long (or long
(and parents
"<p>This is an ordinary @(see std::defsum) of aggregates.</p>")))
((unless (tuplep 1 other-args))
(raise "defsum should be given exactly one non-keyword argument, but got ~x0"
other-args))
(aggnames (first other-args))
(agginfos (ds-find-aggregates aggnames (get-aggregates (w state))))
(def (ds-recognizer-def name agginfos))
(name-p (ds-recognizer-name name))
(x (ds-x name))
((when (eq mode :program))
`(defsection ,name
,@(and parents `(:parents ,parents))
,@(and short `(:short ,short))
,@(and long `(:long ,long))
(program)
,def
. ,other-events))
(events
`((logic)
,def
(local (in-theory (enable ,name-p)))
(defthm ,(mksym 'consp-when- name-p)
(implies (,name-p ,x)
(consp ,x))
:rule-classes :compound-recognizer)
,@(ds-member-implies-sum-thms name agginfos)
,@(ds-by-tag-thms name agginfos)
,(ds-when-invalid-tag-thm name agginfos)
;; BOZO maybe generate fast functions?
;; BOZO some kind of pattern-match macros?
)))
`(defsection ,name
,@(and parents `(:parents ,parents))
,@(and short `(:short ,short))
,@(and long `(:long ,long))
(encapsulate () . ,events)
. ,other-events)))
(defmacro defsum (name &rest args)
(b* ((__function__ 'defsum)
((unless (symbolp name))
(raise "Name must be a symbol."))
(ctx (list 'defsum name))
((mv main-stuff other-events) (split-/// ctx args))
((mv kwd-alist other-args)
(extract-keywords ctx *defsum-valid-keywords* main-stuff nil)))
;; BOZO Add with-output stuff eventually
`(make-event
`(progn ,(defsum-fn ',name ',other-args ',kwd-alist ',other-events state)
(value-triple '(defsum ,',name))))))
#||
(defsum mysum
:mode :logic
(foo bar baz))
||#
|