This file is indexed.

/usr/share/acl2-6.3/books/tools/bstar.lisp is in acl2-books-source 6.3-5.

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
 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
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
(in-package "ACL2")

;; Contact: Sol Swords <sswords@cs.utexas.edu>

;; This book contains the macro b*, which acts like let* with certain
;; extensions.  Some of its features:
;; - Can bind single values and MVs within the same let*-like sequence
;; of assignments, without nesting
;; - Can bind variables using user-defined pattern-matching idioms
;; - Eliminates ignore and ignorable declarations

(defdoc b* ":DOC-SECTION B*
Flexible let*-like macro for variable bindings~/
Usage:
~bv[]
 (b* ;; let*-like binding to a single variable:
     ((x (cons 'a 'b))

     ;; mv binding
      ((mv y z) (return-two-values x x))

     ;; No binding: expression evaluated for side effects
      (- (cw \"Hello\")) ;; prints \"Hello\"

     ;; Binding with type declaration:
      ((the (integer 0 100) n) (foo z))

     ;; MV which ignores a value:
      ((mv & a) (return-garbage-in-first-mv y z))

     ;; Binds value 0 to C and value 1 to D,
     ;; declares (ignorable C) and (ignore D)
      ((mv ?c ?!d) (another-mv a z))

     ;; Bind V to the middle value of an error triple,
     ;; quitting if there is an error condition (a la er-let*)
      ((er v) (trans-eval '(len (list 'a 1 2 3)) 'foo state))

     ;; The WHEN, IF, and UNLESS constructs insert an IF in the
     ;; binding stream.  The WHEN and IF constructs are equivalent.
      ((when v) (finish-early-because-of v))
      ((if v)   (finish-early-because-of v))
      ((unless c) (finish-early-unless c))

     ;; Pattern-based binding using cons, where D is ignorable
      ((cons (cons b c) ?d) (must-return-nested-conses a))

     ;; Alternate form of pattern binding with cons nests, where G is
     ;; ignored and f has a type declaration:
      (`(,e (,(the (signed-byte 8) f) . ,?!g))
       (makes-a-list-of-conses b))

     ;; LIST and LIST* are also supported:
      ((list a b) '((1 2) (3 4)))
      ((list* a (the string b) c) '((1 2) \"foo\" 5 6 7))

     ;; Pattern with user-defined constructor:
      ((my-tuple foo bar hum) (something-of-type-my-tuple e c g))

     ;; Don't-cares with pattern bindings:
      ((my-tuple & (cons carbar &) hum) (something-else foo f hum))

     ;; Pattern inside an mv:
      ((mv a (cons & c)) (make-mv-with-cons)))
   (some-expression .....))
~ev[]
~/

B* is a macro for binding variables in sequence, like let*.  However, it
contains some additional features to allow it to bind multi-values, error
triples, and subtrees of CONS structures, run forms for side-effects, ignore
variables or declare them ignorable.  It can also be extended by the user to
support more binding constructs.

Its syntax is
 (b* <list-of-bindings> . <list-of-result-forms>)
where a result form is any ACL2 term, and a binding is
 (<binder-form> [<expression>]).  See below for discussion of binder forms.
Depending on the binder form, it may be that multiple expressions are allowed
or only a single one.

B* expands to multiple nestings of another macro PATBIND, analogously to how
LET* expands to multiple nestings of LET.

-- Basic let*-like usage --

B* can be used exactly like LET*, except that it does not yet support DECLARE
forms.  However, IGNORE or IGNORABLE declarations are inserted by B* according
to the syntax of the variables bound\; see below.  B* also supports multiple
result expressions after the binding list\; these are run in sequence and the
result of the last such form is returned.

-- Binder Forms --

The following binder forms are supported by this book alone, but support may
be added by the user for other binding forms.  In most cases, these binding
forms may be nested.  See ~il[b*-binders] for a comprehensive list of available
binder forms.

 ~c[(mv a b ...)] produces an MV-LET binding

 ~c[(cons a b)] produces a binding of the CAR and CDR of the corresponding expression

 ~c[(er a)] produces an ER-LET* binding

 ~c[(list a b ...)] produces a binding of (car val), (cadr val), etc, where val
is the result of the corresponding expression

 ~c[(nths a b ...)] produces a binding of (nth 0 val), (nth 1 val), etc, where val
is the result of the corresponding expression

 ~c[(list* a b)], ~c[`(,a . ,b)] are alternatives to the CONS binder.

 ~c[(the type-spec var)] produces a binding of var to the result of the
corresponding expression, and a declaration that var is of the given
type-spec.  A THE pattern may be nested inside other patterns, but VAR must
itself be a symbol and not a nested pattern, and type-spec must be a type-spec.

 ~c[(if test)], ~c[(when test)], and ~c[(unless test)] don't actually produce bindings at
all.  Instead, they insert an IF where one branch is the rest of the B* form and
the other is the \"bound\" expression.  For example,
~bv[]
 (b* (((if (atom a)) 0)
      (rest (of-bindings)))
   final-expr)
~ev[]
expands to something like this:
~bv[]
 (if (atom a)
     0
   (b* ((rest (of-bindings)))
     final-expr))
~ev[]
These forms also create an \"implicit progn\" with multiple expressions, like
this:
~bv[]
 (b* (((if (atom a)) (cw \"a is an atom, returning 0\") 0)
      ...)
   ...)
~ev[]

-- Nesting Binders --

The CONS, LIST, LIST*, and backtick binders may be nested arbitrarily inside
other binders.  Often user-defined binders may also be arbitrarily nested.  For
example,

~c[((mv (list `(,a . ,b)) (cons c d)) <form>)]

will result in the following (logical) bindings:

~c[a] bound to ~c[(car (nth 0 (mv-nth 0 <form>)))]

~c[b] bound to ~c[(cdr (nth 0 (mv-nth 0 <form>)))]

~c[c] bound to ~c[(car (mv-nth 1 <form>))]

~c[d] bound to ~c[(cdr (mv-nth 1 <form>))].

-- Ignore, Ignorable, and Side-effect Only Bindings --

The following constructs may be used in place of variables:

Dash (-), used as a top-level binding form, will run the corresponding
expressions (in an implicit progn) for side-effects without binding its value.
Used as a lower-level binding form, it will cause the binding to be ignored or
not created.

Ampersand (&), used as a top-level binding form, will cause the corresponding
expression to be ignored and not run at all.  Used as a lower-level binding
form, it will cause the binding to be ignored or not created.

Any symbol beginning with ?! works similarly to the & form.  It is ignored
or not evaluated at all.

Any symbol beginning with ? but not ?! will make a binding of the symbol
obtained by removing the ?, and will make an IGNORABLE declaration for this
variable.

-- User-Defined Binders --

A new binder form may be created by defining a macro named PATBIND-<name>.  We
discuss the detailed interface of user-defined binders below.  First,
DEF-PATBIND-MACRO provides a simple way to define certain user binders.  For
example, this form is used to define the binder for CONS:
~bv[]
 (def-patbind-macro cons (car cdr))
~ev[]
This defines a binder macro ~c[PATBIND-CONS] which enables ~c[(cons a b)] to be used as
a binder form.  This binder form must take two arguments since two destructor
functions (car cdr) are given to def-patbind-macro.  The destructor functions
are each applied to the form to produce the bindings for the corresponding
arguments of the binder.

There are many cases in which DEF-PATBIND-MACRO is not powerful enough.  For
example, a binder produced by DEF-PATBIND-MACRO may only take a fixed number of
arguments.  More flexible operations may be defined by hand-defining the binder
macro using the form DEF-B*-BINDER; see ~il[DEF-B*-BINDER].

A binder macro PATBIND-<NAME> must take three arguments ARGS, FORMS,
and REST-EXPR.  The form
~bv[]
 (b* (((binder arg1 arg2) binding1 binding2))
    expr)
~ev[]
translates to a macro call
~bv[]
 (patbind-binder (arg1 arg2) (binding1 binding2) expr).
~ev[]
That is, ARGS is the list of arguments given to the binder form, BINDINGS is
the list of expressions bound to them, and EXPR is the result expression to be
run once the bindings are in place.  The definition of the patbind-binder macro
determines how this gets further expanded.  Some informative examples of these
binder macros may be found in bstar.lisp\; simply search for uses of
DEF-B*-BINDER. Here are some notes on defining binder macros.

Often the simplest way to accomplish the intended effect of a patbind macro is
to have it construct another B* form to be recursively expanded, or to call
other patbind macros.  See, for example, the definition of PATBIND-LIST.

Patbind macros for forms that are truly creating bindings should indeed use B*
 (or PATBIND, which is what B* expands to) to create these bindings, so that
ignores and nestings are dealt with uniformly.  See, for example, the
definition of PATBIND-NTHS.

In order to get good performance, destructuring binders such as are produced by
DEF-PATBIND-MACRO bind a variable to any binding that isn't already a variable
or quoted constant.  This is important so that in the following form,
~c[(foo x y)] is run only once:
~bv[]
 (b* (((cons a b) (foo x y))) ...)
~ev[]
In these cases, it is good discipline to check the new variables introduced
using the macro CHECK-VARS-NOT-FREE\; since ACL2 does not have gensym, this is
the best option we have. See any definition produced by DEF-PATBIND-MACRO for
examples, and additionally PATBIND-NTHS, PATBIND-ER, and so forth.
")



(include-book "pack")

(defun macro-name-for-patbind (binder)
  (intern-in-package-of-symbol
   (concatenate 'string
                "PATBIND-"
                (symbol-name binder))
   (if (equal (symbol-package-name binder) "COMMON-LISP")
       'acl2::foo
     binder)))

(defconst *patbind-special-syms* '(t nil & -))

(defun int-string (n)
  (coerce (explode-nonnegative-integer n 10 nil) 'string))

(defun str-num-sym (str n)
  (intern (concatenate 'string str (int-string n)) "ACL2"))

(defun ignore-var-name (n)
  (str-num-sym "IGNORE-" n))


(defun debuggable-binder-list-p (x)
  (declare (xargs :guard t))
  (cond ((atom x) 
         (or (equal x nil)
             (cw "; Not a binder list; ends with ~x0, instead of nil.~%" x)))
        ;; This used to check that the cdar was also a cons and a true-list,
        ;; but this can be left up to the individual binders.
        ((consp (car x))
         (debuggable-binder-list-p (cdr x)))
        (t
         (cw "; Not a binder list; first bad entry is ~x0.~%" (car x)))))

(defun debuggable-binders-p (x)
  (declare (xargs :guard t))
  (cond ((atom x) 
         (or (equal x nil)
             (cw "; Not a binder list; ends with ~x0, instead of nil.~%" x)))
        ;; This used to check that the cdar was also a cons and a true-list,
        ;; but this can be left up to the individual binders.
        ((consp (car x)) t)
        (t
         (cw "; Not a binder list; first bad entry is ~x0.~%" (car x)))))

(defun decode-varname-for-patbind (pattern)
  (let* ((name (symbol-name pattern))
         (len (length name))
         (?p (and (<= 1 len)
                  (eql (char name 0) #\?)))
         (?!p (and ?p
                   (<= 2 len)
                   (eql (char name 1) #\!)))
         (sym (cond 
               (?!p (intern-in-package-of-symbol
                     (subseq name 2 nil) pattern))
               (?p (intern-in-package-of-symbol
                    (subseq name 1 nil) pattern))
               (t pattern)))
         (ignorep (cond
                   (?!p 'ignore)
                   (?p 'ignorable))))
    (mv sym ignorep)))



(defun patbindfn (pattern assign-exprs nested-expr)
  (cond ((eq pattern '-)
         ;; A dash means "run this for side effects."  In this case we allow
         ;; multiple terms; these form an implicit progn, in the common-lisp sense.
         `(prog2$ (progn$ . ,assign-exprs)
                  ,nested-expr))
        ((member pattern *patbind-special-syms*)
         ;; &, T, NIL mean "don't bother evaluating this."
         nested-expr)
        ((atom pattern)
         ;; A binding to a single variable.  Here we don't allow multiple
         ;; expressions; we believe it's more readable to use - to run things
         ;; for side effects, and this might catch some paren errors.
         (if (cdr assign-exprs)
             (er hard 'b* "~
The B* binding of ~x0 to ~x1 isn't allowed because the binding of a variable must be a
single term." pattern assign-exprs)
           (mv-let (sym ignorep)
             (decode-varname-for-patbind pattern)
             ;; Can we just refuse to bind a variable marked ignored?
             (if (eq ignorep 'ignore)
                 nested-expr
               `(let ((,sym ,(car assign-exprs)))
                  ,@(and ignorep `((declare (,ignorep ,sym))))
                  ,nested-expr)))))
        ((eq (car pattern) 'quote)
         ;; same idea as &, t, nil
         nested-expr)
        (t ;; Binding macro call.
         (let* ((binder (car pattern))
                (patbind-macro (macro-name-for-patbind binder))
                (args (cdr pattern)))
           `(,patbind-macro ,args ,assign-exprs ,nested-expr)))))
             
        

         
         
(defmacro patbind (pattern assign-exprs nested-expr)
  (patbindfn pattern assign-exprs nested-expr))


;; (defun b*-fn1 (bindlist expr)
;;   (declare (xargs :guard (debuggable-binders-p bindlist)))
;;   (if (atom bindlist)
;;       expr
;;     `(patbind ,(caar bindlist) ,(cdar bindlist)
;;               ,(b*-fn1 (cdr bindlist) expr))))

;; (defun b*-fn (bindlist exprs)
;;   (declare (xargs :guard (and (debuggable-binders-p bindlist)
;;                               (consp exprs))))
;;   (b*-fn1 bindlist `(progn$ . ,exprs)))

(defun b*-fn (bindlist exprs)
  (declare (xargs :guard (and (debuggable-binders-p bindlist)
                              (consp exprs))))
  (if (atom bindlist)
      (cons 'progn$ exprs)
    `(patbind ,(caar bindlist) ,(cdar bindlist)
              (b* ,(cdr bindlist) . ,exprs))))

(defmacro b* (bindlist expr &rest exprs)
  (declare (xargs :guard (debuggable-binders-p bindlist)))
  (b*-fn bindlist (cons expr exprs)))


(defdoc b*-binders ":Doc-section b*
B*-BINDERS: List of the available directives usable in B*.~/
~/~/")


(defmacro def-b*-binder (name &rest rest)
  (let* ((macro-name (macro-name-for-patbind name))
         (decls-and-doc (butlast rest 1))
         (body (car (last rest)))
         (decls (remove-strings decls-and-doc))
         (doc (car (get-string decls-and-doc)))
         (doc-sectionp (and doc 
                            (equal (string-upcase (subseq doc 0 12))
                                   ":DOC-SECTION"))))
    `(progn
       (defmacro ,macro-name (args forms rest-expr)
         ,(if doc
              (if doc-sectionp
                  doc
                (concatenate 'string ":DOC-SECTION ACL2::B*-BINDERS
" doc))
            (concatenate 'string
                         ":DOC-SECTION ACL2::B*-BINDERS
B* binder form " (symbol-name name) "~/
 (placeholder)~/~/"))
         ,@decls
         ,body)
       (table b*-binder-table ',name ',macro-name))))


(defdoc def-b*-binder
  ":doc-section b*
DEF-B*-BINDER: Introduce a new form usable inside B*.~/
Usage:
~bv[]
 (def-b*-binder <name> <declare> <doc> <body>)
~ev[]
Introduces a B* binder form of the given name.  The given body, which may use
the variables args, forms, and rest-expr, is used to
macroexpand a form like the following:
~bv[]
 (b* (((<name> . <args>) . <forms>)) <rest-expr>)
~ev[]
The documentation string and declaration forms are optional\; a placeholder doc
string will be produced under :doc-section B*-BINDERS if none is provided.  It
is recommended that the string use that :doc-section, since this provides a
single location where the user may see all of the available binder forms.  If
no doc-section is provided, B*-BINDERS will be used.~/

This works by introducing a macro named PATBIND-<name>.  See ~il[b*] for more
details.~/")









(defmacro destructure-guard (binder args bindings len)
  `(and (or (and (true-listp ,args)
                 . ,(and len `((= (length ,args) ,len))))
            (cw "~%~%**** ERROR ****
Pattern constructor ~x0 needs a true-list of ~@1arguments, but was given ~x2~%~%"
                ',binder ,(if len `(msg "~x0 " ,len) "")
                ,args))
        (or (and (consp ,bindings)
                 (eq (cdr ,bindings) nil))
            (cw "~%~%**** ERROR ****
Pattern constructor ~x0 needs exactly one binding expression, but was given ~x1~%~%"
                ',binder ,bindings))))



(defun destructor-binding-list (args destructors binding)
  (if (atom args)
      nil
    (cons (list (car args) (list (car destructors) binding))
          (destructor-binding-list (cdr args) (cdr destructors) binding))))

(defmacro def-patbind-macro (binder destructors &rest rst)
  (let ((len (length destructors))
        (doc (get-string rst)))
    `(def-b*-binder ,binder ,@doc
       (declare (xargs :guard
                       (destructure-guard ,binder args forms ,len)))
       (let* ((binding (car forms))
              (computedp (or (atom binding)
                             (eq (car binding) 'quote)))
              (bexpr (if computedp binding (pack binding)))
              (binders (destructor-binding-list args ',destructors bexpr)))
         (if computedp
             `(b* ,binders ,rest-expr)
           `(let ((,bexpr ,binding))
              (declare (ignorable ,bexpr))
              (b* ,binders
                (check-vars-not-free (,bexpr) ,rest-expr))))))))

;; The arg might be a plain variable, an ignored or ignorable variable, or a
;; binding expression.
(defun var-ignore-list-for-patbind-mv (args igcount mv-vars binders ignores ignorables freshvars)
  (if (atom args)
      (mv (reverse mv-vars)
          (reverse binders)
          (reverse ignores)
          (reverse ignorables)
          (reverse freshvars))
    (mv-let (mv-var binder freshp ignorep)
      (cond ((or (member (car args) *patbind-special-syms*)
                 (quotep (car args))
                 (and (atom (car args)) (not (symbolp (car args)))))
             (let ((var (ignore-var-name igcount)))
               (mv var nil nil 'ignore)))
            ((symbolp (car args))
             (mv-let (sym ignorep)
               (decode-varname-for-patbind (car args))
               (case ignorep
                 (ignore (mv sym nil nil 'ignore))
                 (ignorable (mv sym nil nil 'ignorable))
                 (t (mv sym nil nil nil)))))
            (t ;; (and (consp (car args))
             ;;                   (not (eq (caar args) 'quote)))
             (let ((var (pack (car args))))
               (mv var (list (car args) var) t nil))))
      (var-ignore-list-for-patbind-mv
       (cdr args)
       (if (eq ignorep 'ignore) (1+ igcount) igcount)
       (cons mv-var mv-vars)
       (if binder (cons binder binders) binders)
       (if (eq ignorep 'ignore) (cons mv-var ignores) ignores)
       (if (eq ignorep 'ignorable) (cons mv-var ignorables) ignorables)
       (if freshp (cons mv-var freshvars) freshvars)))))
          


(def-b*-binder mv
  " B* binder for multiple values~/
Usage example:
~bv[]
 (b* (((mv a b c) (form-returning-three-values)))
    form)
~ev[]
is equivalent to
~bv[]
 (mv-let (a b c) (form-returning-three-values)
   form).
~ev[]

~l[B*] for background.

The MV binder only makes sense as a top-level binding, but each of its
arguments may be a recursive binding.~/~/"
  (declare (xargs :guard (destructure-guard mv args forms nil)))
  (mv-let (vars binders ignores ignorables freshvars)
    (var-ignore-list-for-patbind-mv args 0 nil nil nil nil nil)
    `(mv-let ,vars ,(car forms)
       (declare (ignore . ,ignores))
       (declare (ignorable . ,ignorables))
       (check-vars-not-free
        ,ignores
        (b* ,binders
          (check-vars-not-free ,freshvars ,rest-expr))))))

(def-patbind-macro cons (car cdr)
  "B* binder for CONS decomposition using CAR/CDR~/
Usage:
~bv[]
 (b* (((cons a b) (binding-form))) (result-form))
~ev[]
is equivalent to
~bv[]
 (let* ((tmp (binding-form))
        (a (car tmp))
        (b (cdr tmp)))
    (result-form))
~ev[]

~l[B*] for background.

Each of the arguments to the CONS binder may be a recursive binder, and CONS
may be nested inside other bindings.~/~/")

(defun nths-binding-list (args n form)
  (if (atom args)
      nil
    (cons (list (car args) `(nth ,n ,form))
          (nths-binding-list (cdr args) (1+ n) form))))


(def-b*-binder nths
  "B* binder for list decomposition using NTH~/
Usage:
~bv[]
 (b* (((nths a b c) lst)) form)
~ev[]
is equivalent to
~bv[]
 (b* ((a (nth 0 lst))
      (b (nth 1 lst))
      (c (nth 2 lst)))
   form).
~ev[]

~l[B*] for background.

Each of the arguments to the NTHS binder may be a recursive binder, and NTHS
may be nested inside other bindings.~/~/"
  (declare (xargs :guard (destructure-guard nths args forms nil)))
  (let* ((binding (car forms))
         (evaledp (or (atom binding) (eq (car binding) 'quote)))
         (form (if evaledp binding (pack binding)))
         (binders (nths-binding-list args 0 form)))
    (if evaledp
        `(b* ,binders ,rest-expr)
      `(let ((,form ,binding))
         (declare (ignorable ,form))
         (b* ,binders
           (check-vars-not-free (,form) ,rest-expr))))))



(def-b*-binder nths*
  "B* binder for list decomposition using NTH, with one final NTHCDR~/
Usage:
~bv[]
 (b* (((nths* a b c d) lst)) form)
~ev[]
is equivalent to
~bv[]
 (b* ((a (nth 0 lst))
      (b (nth 1 lst))
      (c (nth 2 lst))
      (d (nthcdr 3 lst)))
   form).
~ev[]

~l[B*] for background.

Each of the arguments to the NTHS binder may be a recursive binder, and NTHS
may be nested inside other bindings.~/~/"
  (declare (xargs :guard (and (destructure-guard nths args forms nil)
                              (< 0 (len args)))))
  (let* ((binding (car forms))
         (evaledp (or (atom binding) (eq (car binding) 'quote)))
         (form (if evaledp binding (pack binding)))
         (binders (append (nths-binding-list (butlast args 1) 0 form)
                          `((,(car (last args)) (nthcdr ,(1- (len args)) ,form))))))
    (if evaledp
        `(b* ,binders ,rest-expr)
      `(let ((,form ,binding))
         (declare (ignorable ,form))
         (b* ,binders
           (check-vars-not-free (,form) ,rest-expr))))))
  




(def-b*-binder list
  "B* binder for list decomposition using CAR/CDR~/
Usage:
~bv[]
 (b* (((list a b c) lst)) form)
~ev[]
is equivalent to
~bv[]
 (b* ((a (car lst))
      (tmp1 (cdr lst))
      (b (car tmp1))
      (tmp2 (cdr tmp1))
      (c (car tmp2)))
   form).
~ev[]

~l[B*] for background.

Each of the arguments to the LIST binder may be a recursive binder, and LIST
may be nested inside other bindings.~/~/"
  (declare (xargs :guard (destructure-guard list args forms nil)))
  (if (atom args)
      rest-expr
    `(patbind-cons (,(car args) (list . ,(cdr args))) ,forms ,rest-expr)))

(def-b*-binder list*
  "B* binder for list* decomposition using CAR/CDR~/
Usage:
~bv[]
 (b* (((list* a b c) lst)) form)
~ev[]
is equivalent to
~bv[]
 (b* ((a (car lst))
      (tmp1 (cdr lst))
      (b (car tmp1))
      (c (cdr tmp1)))
   form).
~ev[]

~l[B*] for background.

Each of the arguments to the LIST* binder may be a recursive binder, and LIST*
may be nested inside other bindings.~/~/"
  (declare (xargs :guard (and (consp args)
                              (destructure-guard list* args forms nil))))
  (if (atom (cdr args))
      `(patbind ,(car args) ,forms ,rest-expr)
    `(patbind-cons (,(car args) (list* . ,(cdr args))) ,forms ,rest-expr)))



(defun assigns-for-assocs (args alist)
  (if (atom args)
      nil
    (cons (if (consp (car args))
              `(,(caar args) (cdr (assoc ,(cadar args) ,alist)))
            (mv-let (sym ign)
              (decode-varname-for-patbind (car args))
              (declare (ignore ign))
              `(,(car args) (cdr (assoc ',sym ,alist)))))
          (assigns-for-assocs (cdr args) alist))))

(def-b*-binder assocs
  "B* binder for alist values~/
Usage:
~bv[]
 (b* (((assocs (a akey) b (c 'foo)) alst)) form)
~ev[]
is equivalent to
~bv[]
 (b* ((a (cdr (assoc akey alst)))
      (b (cdr (assoc 'b alst)))
      (c (cdr (assoc 'foo alst))))
   form).
~ev[]

~l[B*] for background.

The arguments to the ASSOCS binder should be either single symbols or pairs
~c[(VAR KEY)].  In the pair form, ~c[VAR] is assigned to the associated value
of ~c[KEY] in the bound object, which should be an alist.  Note that ~c[KEY]
does not get quoted; it may itself be some expression.  An argument consisting
of the single symbol ~c[VAR] is equivalent to the pair ~c[(VAR 'VAR)].

Each of the arguments in the ~c[VAR] position of the pair form may be a
recursive binder, and ASSOCS may be nested inside other bindings.~/~/"
  (mv-let (pre-bindings name rest)
    (if (and (consp (car forms))
             (not (eq (caar forms) 'quote)))
        (mv `((?tmp-for-assocs ,(car forms)))
            'tmp-for-assocs
            `(check-vars-not-free (tmp-for-assocs)
                            ,rest-expr))
      (mv nil (car forms) rest-expr))
    `(b* (,@pre-bindings
          . ,(assigns-for-assocs args name))
       ,rest)))


(def-b*-binder er
  "B* binder for error triples~/
Usage:
~bv[]
 (b* (((er x) (error-triple-form))) (result-form))
~ev[]
is equivalent to
~bv[]
 (er-let* ((x (error-triple-form))) (result-form)),
~ev[]
which itself is approximately equivalent to
~bv[]
 (mv-let (erp x state)
         (error-triple-form)
     (if erp
         (mv erp x state)
       (result-form)))
~ev[]

~l[B*] for background.

The ER binder only makes sense as a top-level binding, but its argument may be
a recursive binding.~/~/"
  (declare (xargs :guard (destructure-guard er args forms 1)))
  `(mv-let (patbind-er-fresh-variable-for-erp
            patbind-er-fresh-variable-for-val
            state)
     ,(car forms)
     (if patbind-er-fresh-variable-for-erp
         (mv patbind-er-fresh-variable-for-erp
             patbind-er-fresh-variable-for-val
             state)
       (patbind ,(car args)
                (patbind-er-fresh-variable-for-val)
                (check-vars-not-free
                 (patbind-er-fresh-variable-for-val
                  patbind-er-fresh-variable-for-erp)
                 ,rest-expr)))))

(def-b*-binder cmp
  "B* binder for context-message pairs~/
Usage:
~bv[]
 (b* (((cmp x) (cmp-returning-form))) (result-form))
~ev[]
is equivalent to
~bv[]
 (er-let*-cmp ((x (cmp-returning-form))) (result-form)),
~ev[]
which itself is approximately equivalent to
~bv[]
 (mv-let (ctx x)
         (cmp-returning-form)
     (if ctx
         (mv ctx x)
       (result-form)))
~ev[]

~l[B*] for background.

The CMP binder only makes sense as a top-level binding, but its argument may be
a recursive binding.~/~/"
  (declare (xargs :guard (destructure-guard cmp args forms 1)))
  `(mv-let (patbind-cmp-fresh-variable-for-ctx
            patbind-cmp-fresh-variable-for-val)
     ,(car forms)
     (if patbind-cmp-fresh-variable-for-ctx
         (mv patbind-cmp-fresh-variable-for-ctx
             patbind-cmp-fresh-variable-for-val)
       (patbind ,(car args)
                (patbind-cmp-fresh-variable-for-val)
                (check-vars-not-free
                 (patbind-cmp-fresh-variable-for-val
                  patbind-cmp-fresh-variable-for-ctx)
                 ,rest-expr)))))


(def-b*-binder state-global
  "B* binder for state globals~/
Usage:
~bv[]
 (b* (((state-global x) (value-form))) (result-form))
~ev[]
is equivalent to
~bv[]
 (state-global-let* ((x (value-form))) (result-form)).
~ev[]

~l[B*] for background.~/~/"
  (declare (xargs :guard
                  (and (destructure-guard
                        state-global args forms 1)
                       (or (symbolp (car args))
                           (cw "~%~%**** ERROR ****
Pattern constructor ~x0 needs a single argument which is a symbol, but got ~x1~%~%"
                               'state-global args)))))
  `(state-global-let*
    ((,(car args) ,(car forms)))
    ,rest-expr))


(def-b*-binder when
  "B* control flow operator~/
Usage:
~bv[]
 (b* (((when (condition-form))
        (early-form1) ... (early-formN))
      ... rest of bindings ...)
   (late-result-form))
~ev[]
is equivalent to
~bv[]
 (if (condition-form)
     (progn$ (early-form1) ... (early-formN))
   (b* (... rest of bindings ...)
     (late-result-form)))
~ev[]

~l[B*] for background.

Effectively, this provides a way to exit early from the sequence of
computations represented by a list of B* binders.

In the special case where no early-forms are provided, the condition itself is
returned.  I.e.,
~bv[]
 (b* (((when (condition-form)))
      ... rest of bindings)
    (late-result-form))
~ev[]
is equivalent to
~bv[]
 (or (condition-form)
     (b* (... rest of bindings ...)
        (late-result-form)))
~ev[]
~/~/"
  (declare (xargs :guard (and (consp args) (eq (cdr args) nil))))
  (if forms
      `(if ,(car args)
           (progn$ . , forms)
         ,rest-expr)
    `(or ,(car args) ,rest-expr)))

(def-b*-binder if
  "B* control flow operator~/
The B* binders IF and WHEN are exactly equivalent.  ~l[PATBIND-WHEN].~/~/"
  (declare (xargs :guard (and (consp args) (eq (cdr args) nil))))
  `(if ,(car args)
       (progn$ . ,forms)
     ,rest-expr))

(def-b*-binder unless
  "B* control flow operator~/
The B* binder UNLESS is exactly like WHEN, but negates the condition so that
the early exit is taken when the condition is false.  ~l[PATBIND-WHEN].~/~/"
  (declare (xargs :guard (and (consp args) (eq (cdr args) nil))))
  `(if ,(car args)
       ,rest-expr
     (progn$ . ,forms)))
  
(def-b*-binder run-when
  "B* conditional execution operator~/
Usage:
~bv[]
 (b* (((run-when (condition-form)) (run-form1) ... (run-formn)))
   (result-form))
~ev[]
is equivalent to
~bv[]
 (prog2$ (and (condition-form)
              (progn$ (run-form1) ... (run-formn)))
         (result-form)).
~ev[]

~l[B*] for background.~/~/"
  (declare (xargs :guard (and (consp args) (eq (cdr args) nil))))
  `(prog2$ (and ,(car args)
               (progn$ . , forms))
           ,rest-expr))

(def-b*-binder run-if
  "B* conditional execution operator~/
The B* binders RUN-IF and RUN-WHEN are exactly equivalent.
~l[PATBIND-RUN-WHEN].~/~/"
  (declare (xargs :guard (and (consp args) (eq (cdr args) nil))))
  `(prog2$ (and ,(car args)
                (progn$ . ,forms))
           ,rest-expr))

(def-b*-binder run-unless
  "B* control flow operator~/
The B* binder RUN-UNLESS is exactly like RUN-WHEN, but negates the condition so
that the extra forms are run when the condition is false.
~l[PATBIND-RUN-WHEN].~/~/"
  (declare (Xargs :guard (and (consp args) (eq (cdr args) nil))))
  `(prog2$ (or ,(car args)
               (progn$ . ,forms))
           ,rest-expr))

(def-b*-binder the
  "B* type declaration operator~/
Usage:
~bv[]
 (b* (((the integer x) (form))) (result-form))
~ev[]
is equivalent to
~bv[]
 (let ((x (form)))
   (declare (type integer x))
   (result-form))
~ev[]

~l[B*] for background.

The THE binder form only makes sense on variables, though those variables may
be prefixed with the ? or ?! operators to make them ignorable or ignored.
However, it may be nested within other binder forms.~/~/"
  (declare (xargs :guard
                  (and (destructure-guard the args forms 2)
                       (or (translate-declaration-to-guard
                            (car args) 'var nil)
                           (cw "~%~%**** ERROR ****
The first argument to pattern constructor ~x0 must be a type-spec, but is ~x1~%~%"
                               'the (car args)))
                       (or (symbolp (cadr args))
                           (cw "~%~%**** ERROR ****
The second argument to pattern constructor ~x0 must be a symbol, but is ~x1~%~%"
                               'the (cadr args))))))
  (mv-let (sym ignorep)
    (decode-varname-for-patbind (cadr args))
    (if (eq ignorep 'ignore)
        rest-expr
      `(let ((,sym ,(car forms)))
         ,@(and ignorep `((declare (ignorable ,sym))))
         (declare (type ,(car args) ,sym))
         ,rest-expr))))
      

      


;; Find a pair in the alist whose key is a symbol whose name is str.
(defun b*-assoc-symbol-name (str alist)
  (if (atom alist)
      nil
    (if (and (consp (car alist))
             (equal str (symbol-name (caar alist))))
        (car alist)
      (b*-assoc-symbol-name str (cdr alist))))) 

(defun b*-decomp-err (arg binder component-alist)
  (er hard? 'b*-decomp-bindings
      "Bad ~s0 binding: ~x2.~%For a ~s0 binding you may use the following ~
       kinds of arguments: keyword/value list form :field binder ..., ~
       name-only where the variable bound is the same as a field name, ~
       or parenthseized (binder :field).  The possible fields are ~v1."
      binder (strip-cars component-alist) arg))

;; Makes b* bindings for a decomposition specified by component-alist.
;; Component-alist binds field names to their accessor functions.
;; Accepts a number of forms of bindings:
(defun b*-decomp-bindings (args binder component-alist var)
  (b* (((when (atom args)) nil)
       ((when (keywordp (car args)))
        (b* ((look (b*-assoc-symbol-name (symbol-name (car args))
                                         component-alist))
             ((unless look)
              (b*-decomp-err (car args) binder component-alist))
             ((unless (consp (cdr args)))
              (b*-decomp-err args binder component-alist)))
          (cons `(,(cadr args) (,(cdr look) ,var))
                (b*-decomp-bindings (cddr args) binder component-alist var))))
       ((when (symbolp (car args)))
        (b* ((look (b*-assoc-symbol-name (symbol-name (car args))
                                         component-alist))
             ((unless look)
              (b*-decomp-err (car args) binder component-alist)))
          (cons `(,(car args) (,(cdr look) ,var))
                (b*-decomp-bindings (cdr args) binder component-alist var))))
       ((unless (and (true-listp (car args))
                     (equal (length (car args)) 2)
                     (symbolp (cadar args))))
        (b*-decomp-err (car args) binder component-alist))
       (look (b*-assoc-symbol-name (symbol-name (cadar args)) component-alist))
       ((unless look)
        (b*-decomp-err (car args) binder component-alist)))
    (cons `(,(caar args) (,(cdr look) ,var))
          (b*-decomp-bindings (cdr args) binder component-alist var))))

(defun b*-decomp-fn (args forms rest-expr binder component-alist)
  (b* (((unless (and (true-listp forms)
                     (= (length forms) 1)))
        (er hard? 'b*-decomp-fn
            "Too many RHS forms in ~x0 binder: ~x1~%" binder forms))
       (rhs (car forms))
       (var (if (symbolp rhs) rhs 'b*-decomp-temp-var))
       (bindings (b*-decomp-bindings args binder component-alist var)))
    `(b* ,(if (symbolp rhs)
              bindings
            (cons `(,var ,rhs) bindings))
       ,rest-expr)))

(defmacro def-b*-decomp (name &rest component-alist)
  `(def-b*-binder ,name
     (b*-decomp-fn args forms rest-expr ',name ',component-alist)))

(defun patbind-local-stobjs-helper (stobjs retvals form)
  (declare (xargs :mode :program))
  (if (atom stobjs)
      form
    (let* ((stobj (if (consp (car stobjs))
                      (caar stobjs)
                    (car stobjs)))
           (rest-retvals (remove-eq stobj retvals)))
      (patbind-local-stobjs-helper
       (cdr stobjs)
       rest-retvals
       `(with-local-stobj
          ,stobj
          (mv-let ,retvals
            ,form
            ,(if (consp (cdr rest-retvals))
                 `(mv . ,rest-retvals)
               (car rest-retvals)))
          . ,(and (consp (car stobjs))
                  (cdar stobjs)))))))
          
(defun patbind-local-stobj-arglistp (args)
  (declare (xargs :mode :program))
  (if (atom args)
      (eq args nil)
    (and (let ((x (car args)))
           (or (symbolp x)
               (case-match x
                 ((stobj creator)
                  (and (symbolp stobj)
                       (symbolp creator))))))
         (patbind-local-stobj-arglistp (cdr args)))))

(defun patbind-local-stobjs-fn (args forms rest-expr)
  (declare (xargs :mode :program))
  (b* (((unless (patbind-local-stobj-arglistp args))
        (er hard? 'patbind-local-stobjs-fn
            "In local-stobjs b* binder, arguments must be symbols or
      (stobj creator) pairs"))
       ((unless (and (= (len forms) 1)
                     (symbol-listp (car forms))
                     (eq (caar forms) 'mv)))
        (er hard? 'patbind-local-stobjs-fn
            "In local-stobjs b* binder, bound form must be an MV
              of some symbols, giving the return values"))
       (retvals (cdar forms)))
    (patbind-local-stobjs-helper
     args retvals rest-expr)))


(def-b*-binder local-stobjs
  (patbind-local-stobjs-fn args forms rest-expr))





(set-state-ok t)

(with-output 
 :off warning
 (local
  (progn
    (defun return-two-values (a b)
      (mv a b))

    (defun transl-equal-tests-fn (tests)
      (if (atom tests)
          `(mv nil state)
        `(mv-let (err val state)
           (transl-equal ',(caar tests) ',(cadar tests))
           (if (or err (not val))
               (mv ',(car tests) state)
             ,(transl-equal-tests-fn (cdr tests))))))

    (defmacro transl-equal-tests (&rest tests)
      (transl-equal-tests-fn tests))


    (defstobj st1 (arr1 :type (array t (0)) :resizable t))
    (defstobj st2 (arr2 :type (array t (0)) :resizable t))


    (defun patbind-tests-fn (tests state)
      (declare (xargs :mode :program))
      (if (atom tests)
          (value '(value-triple :invisible))
        (mv-let (erp val state)
          (thm-fn `(equal ,(caar tests) ,(cadar tests))
                  state '(("goal" :in-theory nil)) nil nil)
          (if erp
              (mv (msg "~% ****** ERROR ******~%~
Testing of the patbind macro failed on expression ~x0~%~%" (car tests))
                  val state)
            (patbind-tests-fn (cdr tests) state)))))

    (defmacro patbind-run-tests (&rest tests)
      `(make-event
        (patbind-tests-fn ',tests state)))
    ;;        (mv-let (err val state)
    ;;          (transl-equal-tests ,@tests)
    ;;          (declare (ignore val))
    ;;          (if err
    ;;              (mv t
    ;;                  (er hard? 'patbind-run-tests
    ;;                      "~% ****** ERROR ******~%~
    ;; Testing of the patbind macro failed on expression ~x0~%~%" err)
    ;;                  state)
    ;;            (value (prog2$ (cw "
    ;; Testing of the patbind macro passed.~%")
    ;;                           `(value-triple 'tests-ok)))))
    ;;        :check-expansion (value-triple 'tests-ok)))


    (patbind-run-tests
     ;; TEST CASES

     ((patbind (cons (cons a b) c) (x) (list a b c))
      (let* ((|(CAR X)| (car x))
             (c (cdr x)))
        (let* ((a (car |(CAR X)|))
               (b (cdr |(CAR X)|)))
          (list a b c))))

     ((patbind x ((cons 'a 'b)) x)
      (let ((x (cons 'a 'b))) x))

     ((patbind (mv a b) ((mv 'a 'b)) (list a b))
      (mv-let (a b) (mv 'a 'b) (list a b)))

     ((patbind & ((cw "Hello")) nil)
      nil)

     ((patbind - ((cw "Hello")) nil)
      (prog2$ (cw "Hello")
              nil))

     ((patbind (cons a &) ('(a b)) a)
      (let ((a (car '(a b))))
        a))

     ((patbind (cons (cons a b) c) (x)
               (list a b c))
      (let ((|(CONS A B)| (car x))
            (c (cdr x)))
        (let ((a (car |(CONS A B)|))
              (b (cdr |(CONS A B)|)))
          (list a b c))))

     ((patbind (cons (cons a b) c) ((cons x y))
               (list a b c))
      (let ((|(CONS (CONS A B) C)| (cons x y)))
        (let ((|(CONS A B)| (car |(CONS (CONS A B) C)|))
              (c (cdr |(CONS (CONS A B) C)|)))
          (let ((a (car |(CONS A B)|))
                (b (cdr |(CONS A B)|)))
            (list a b c)))))

     ((patbind (cons (cons & b) c) ((cons x y))
               (list b c))
      (let ((|(CONS (CONS & B) C)| (cons x y)))
        (let ((|(CONS & B)| (car |(CONS (CONS & B) C)|))
              (c (cdr |(CONS (CONS & B) C)|)))
          (let ((b (cdr |(CONS & B)|)))
            (list b c)))))

     ((patbind (cons (cons a &) c) ((cons x y))
               (list a c))
      (let ((|(CONS (CONS A &) C)| (cons x y)))
        (let ((|(CONS A &)| (car |(CONS (CONS A &) C)|))
              (c (cdr |(CONS (CONS A &) C)|)))
          (let ((a (car |(CONS A &)|)))
            (list a c)))))


     ((patbind (mv (cons a (cons b c)) d)
               ((return-two-values x y))
               (list a b c d))
      (mv-let (|(CONS A (CONS B C))| d)
        (return-two-values x y)
        (let ((a (car |(CONS A (CONS B C))|))
              (|(CONS B C)| 
               (cdr |(CONS A (CONS B C))|)))
          (let ((b (car |(CONS B C)|))
                (c (cdr |(CONS B C)|)))
            (list a b c d)))))

     ((patbind (mv (cons a (cons b c)) &)
               ((return-two-values x y))
               (list a b c d))
      (mv-let (|(CONS A (CONS B C))| ignore-0)
        (return-two-values x y)
        (declare (ignore ignore-0))
        (let ((a (car |(CONS A (CONS B C))|))
              (|(CONS B C)| 
               (cdr |(CONS A (CONS B C))|)))
          (let ((b (car |(CONS B C)|))
                (c (cdr |(CONS B C)|)))
            (list a b c d)))))

     ((patbind (mv (cons a (cons & c)) &)
               ((return-two-values x y))
               (list a c d))
      (mv-let (|(CONS A (CONS & C))| ignore-0)
        (return-two-values x y)
        (declare (ignore ignore-0))
        (let ((a (car |(CONS A (CONS & C))|))
              (|(CONS & C)| 
               (cdr |(CONS A (CONS & C))|)))
          (let ((c (cdr |(CONS & C)|)))
            (list a c d)))))
 
     ((patbind `(,a ,b) ((cons x y)) (list a b))
      (let ((|(CONS A (CONS B (QUOTE NIL)))| (cons x y)))
        (let ((a (car |(CONS A (CONS B (QUOTE NIL)))|))
              (|(CONS B (QUOTE NIL))|
               (cdr |(CONS A (CONS B (QUOTE NIL)))|)))
          (let ((b (car |(CONS B (QUOTE NIL))|)))
            (list a b)))))

     )
    
    (defthm len-resize-list
      (equal (len (resize-list a b c))
             (nfix b)))

    (defun localstobjtest (a b c)
      (declare (xargs :guard t))
      (b* ((d (cons b c))
           ((local-stobjs st1 st2)
            (mv g st2 h st1))
           (a (nfix a))
           (st1 (resize-arr1 (+ 1 a) st1))
           (st2 (resize-arr2 (+ 1 (nfix b)) st2))
           (st1 (update-arr1i a d st1))
           (st2 (update-arr2i (nfix b) a st2)))
        (mv (arr2i (nfix b) st2)
            st2
            (arr1i a st1)
            st1)))

    (make-event
     (mv-let (res1 res2)
       (localstobjtest nil 10 'c)
       (if (and (equal res1 0)
                (equal res2 '(10 . c)))
           '(value-triple :passed)
         (er hard 'test-local-stobj
             "test failed")))))))

(def-b*-binder fun
  "B* binder to produce FLET~/
Usage:
~bv[]
 (b* (((fun (foo a b c)) (body-form))) (result-form))
~ev[]
is equivalent to
~bv[]
 (flet ((foo (a b c) (body-form))) (result-form)).
~ev[]

~l[B*] for background.~/~/"
  (declare (xargs :guard
                  (and
                   ;; only arg is a symbol-list (foo a b c)
                   (consp args)
                   (symbol-listp (car args))
                   (consp (car args))
                   (eq (cdr args) nil)
                   ;; body is implicit progn$?
                   (true-listp forms)
                   )))
  `(flet ((,(caar args) ,(cdar args) (progn$ . ,forms)))
    ,rest-expr))