-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathxm.lsp
2767 lines (2343 loc) · 97.9 KB
/
xm.lsp
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
;; X-Music, inspired by Commmon Music
#|
PATTERN SEMANTICS
Patterns are objects that are generally accessed by calling (next
pattern). Each call returns the next item in an infinite sequence
generated by the pattern. Items are organized into periods. You can
access all (remaining) items in the current period using (next pattern
t).
Patterns mark the end-of-period with +eop+, a distinguished atom. The
+eop+ markers are filtered out by the next() function but returned by
the :next method.
Pattern items may be patterns. This is called a nested pattern. When
patterns are nested, you return a period from the innermost pattern,
i.e. traversal is depth-first. This means when you are using something
like random, you select a random pattern and get an item from it. The
next time you handle :next, you get another item from the same pattern
until the pattern returns +eonp+, which you can read as "end of nested
pattern". Random would then advance to the next random pattern and get
an item from it.
While generating from a nested pattern, you might return many periods
including +eop+, but you do not advance to the next pattern at any
given level until that level receives +eonp+ from the next level down.
With nested patterns, i.e. patterns with items that are patterns, the
implementation requires that *all* items must be patterns. The
application does *not* have to make every item a pattern, so the
implementation "cleans up" the item list: Any item that is not a
pattern is be replaced with a cycle pattern whose list contains just
the one item.
PATTERN LENGTH
There are two sorts of cycles and lengths. The nominal pattern
behavior, illustrated by cycle patterns, is to cycle through a
list. There is a "natural" length computed by :start-period and stored
in count that keeps track of this.
The second cycle and length is established by the :for parameter,
which is optional. If a number or pattern is provided, it controls the
period length and overrides any default periods. When :for is given,
count is set and used as a counter to count the items remaining in
a period.
To summarize, there are 3 ways to determine lengths:
1) The length is implicit. The length can be computed by :start-period
and turned into an explicit length stored in count.
2) The length is explicitly set with :for. This overrides the implicit
length. The explicit length is stored as count that tells how many
more items to generate in the current period.
3) The length can be generated by a pattern. The pattern is evaluated
in :start-period to generate an explicit length.
In case (1), a pattern object does not return +eonp+ to the next level
up unless it receives an +eonp+ from one level down *and* is at the
end of its period. E.g. in the random pattern, if there are three
nested patterns, random must see +eonp+ three times and make three
random pattern selections before returning +eonp+ to the next level
up. This is the basic mechanism for achieving a "depth-first"
expansion of patterns.
However, there is still the question of periods. When a nested pattern
returns a period, do the containing pattern return that period or
merge the period with other periods from other nested patterns? The
default is to return periods as they are generated by sub-patterns. In
other words, when a nested pattern returns +eop+ (end of period), that
token is returned by the :next message. Thus, in one "natural" cycle
of a pattern of patterns, there can be many periods (+eop+'s) before
+eonp+ is returned, marking the end of the "natural" pattern at this
level.
The alternative strategy, which is to filter out all the +eop+'s and
form one giant pattern that runs up to the natural length (+eonp+) for
this level, can be selected by setting the :merge parameter to true.
Note that :merge is ignored if :for is specified because :for says
exactly how many items are in each period.
The Copier pattern is an interesting case. It's :start-pattern should
get the next period from its sub-pattern, a repeat count from the
:repeat pattern, and a boolean from the :merge pattern. Then, it
should make that many copies, returning them as multiple periods or as
one merged one, depending on :merge, followed by +eonp+, after which
:start-pattern is called and the process repeats. But if :for 10 is
provided, this means we need to return a single period of 10 items. We
call :start-pattern, then repeat the sub-pattern's period until we
have 10 items. Thus, we ignore the :merge flag and :repeat count.
This makes Copier with a :for parameter equivalent to Cycle with a
single sub-pattern in a list. If you think :for should not override
these parameters (:repeat and :merge), you can probably get what you
want by using a Length pattern to regroup the output of a Copier.
IMPLEMENTATION
Most pattern behavior is implemented in a few inherited methods.
:next gets the next item or period. If there is a length-pattern
(from :for), :next groups items into periods, filtering out +eop+ and
+eonp+. If there is no length-pattern, :next passes +eop+ through and
watches for +eonp+ to cause the pattern to re-evaluate pattern
parameters.
Several methods are implemented by subclasses of pattern-class:
:START-PERIOD is called before the first advance and before the first
item of a period controlled by :for. It sets count to the "natural"
length of the period. HAVE-CURRENT will be set to false.
:ADVANCE advances to the next item in the pattern. If there are nested
patterns, advance is called to select the first nested pattern, then
items are returned until +eonp+ is seen, then we advance to the next
pattern, etc. After :ADVANCE, HAVE-CURRENT is true.
CURRENT is set by advance to the current item. If this has nested
patterns, current is set to a pattern, and the pattern stays there in
current until advance is called, either at the end of period or when
+eonp+ is seen.
HAVE-CURRENT is a boolean to tell when CURRENT is valid.
IS-NESTED - set when there are nested patterns. If there are, make all
items of any nested pattern be patterns (no mix of patterns and
non-patterns is allowed; use
(MAKE-CYCLE (LIST item))
to convert a non-pattern to a pattern).
Patterns may be shared, so the state machines may be advanced by more
than one less-deeply nested pattern. Thus, patterns are really DAGs
and not trees. Since patterns are hard enough to understand, the
precise order of evaluation and therefore the behavior of shared
patterns in DAGs may not be well-defined. In this implementation
though, we only call on state machines to advance as needed (we try
not to read out whole periods).
The next() function gets an item or period by calling :next.
The :next method is shared by all pattern sub-classes and behaves
differently with :for vs. no :for parameter. With the :for parameter,
we just get items until the count is reached, but getting items is
a bit tricky, because the normal behavior (without :for) might reach
the end of the "natural" period (+eonp+) before count is
reached. So somehow, we need to override count. We could just set
count the count, but count is going to count items and due to
empty periods, count could go to zero before count does. We could
set count = 1000 * count with the idea that we're probably in an
infinite loop generating empty periods forever if count ever reaches
zero.
But then what about the Heap pattern? If count is greater than the
heap size, what happens when the heap is empty? Or should Heap not
allow :for? There are other "problem" patterns, and not all Vers. 1
patterns allowed :for, so let's make list of patterns that could use
:for:
:for is OK :for is not OK
---------- --------------
cycle heap
line accumulation
random copier
palindrome length
accumulate window
sum
product
eval
markov
It seems that we could use :for for all patterns and just extend the
behavior a bit, e.g. when the heap runs out, replenish it (without
getting another period from a sub-pattern, if any; accumulation could
just start over; copier could cycle as described earlier; length
really should not allow :for, and window could just generate :for
items before reevaluating :skip and :pattern-length parameters.
To implement this, the subclass patterns need :advance to do the right
next thing even if we are beyond the "natural" period. :advance should
go to the next sub-pattern or item without returning +eop+ or getting
the next item from any sub-pattern.
state transitions are based on count and something like this:
count
nil -> actions: :start-period, don't return, set count
N -> N-1, actions: :advance if not have-current, return next item
0 -> -1, actions: return +eop+
-1 -> nil, actions: return +eonp+
def :next()
if length-pattern: // the :for parameter value
if null(count): // initial state before every period
var forcount = next(length-pattern) // must be a number
// compute forcount first and pass to start-period in case there
// is no "natural" period length. If there is a "natural" period,
// the forcount parameter is ignored (the usual case)
self.:start-period(forcount)
have-current = false
// :start-period() sets count, but we replace it with :for parameter
count = forcount
if count == 0:
count = -1
return +eop+
if count == -1:
count = nil
return +eonp+
while true
// otherwise, here is where we return N items
if not have-current
self.:advance()
if not is-nested
// now current is updated
have-current = false
count = count - 1
return current
// nested, so get item from sub-pattern
rslt = current.:next
if rslt == +eonp+
// time to advance because sub-pattern has played out
have-current = false
elif rslt == +eop+
nil // skip ends of periods, we're merging them
// we got a real item to return
else
count = count - 1
return rslt
// here, we have no length-pattern, so use "natural" periods
// count is null, and we use count
while true
if null(count):
have-current = false
self.:start-period()
if is-nested:
if count == 0:
if merge-flag: // we filtered out +eop+ so return one here
count == -1
return +eop+
else
count = nil
return +eonp+
if count == -1
count = nil
return +eonp+
else
if count = 0:
count = -1
return +eop+
if count == -1:
count = nil
return +eonp+
// count is a number > 0
if not have-current:
self.:advance
have-current = true
if not is-nested
have-current = false
count = count - 1
return current
// nested, so get sub-pattern's next item or +eonp+ or +eop+
rslt = current.:next
if rslt == +eonp+
have-current = false // force advance next time, don't
// return +eonp+ until count == 0
else if rslt == +eop+ and merge-flag:
nil // iterate, skipping this +eop+ to merge periods
else
return rslt // either +eop+ or a real item
If the input is a list of patterns, then the pattern selects patterns
from the list, and the internal state advances as each selected
pattern completes a period. In this case, there is no way to control
the number of elements drawn from each selected pattern -- the number
is always the length of the period returned by the selected
pattern. If :for is specified, this controls the length of the period
delivered to the next less deeply nested pattern, but the delivered
period may be a mix of elements from the more deeply nested patterns.
|#
(setf SCORE-EPSILON 0.000001)
(setf pattern-class
(send class :new '(current have-current is-nested name count merge-flag
merge-pattern length-pattern trace)))
;; sub-classes should all call (send-super :isnew length-pattern name trace)
;;
(send pattern-class :answer :isnew '(mp lp nm tr)
'((setf merge-pattern mp length-pattern lp name nm trace tr)
(xm-traceif "pattern-class :isnew nm" nm "name" name)))
(defun patternp (x)
(and (objectp x) (send x :isa pattern-class)))
(setf +eop+ '+eop+)
(setf +eonp+ '+eonp+) ;; end of nested period, this indicates you
;; should advance yourself and call back to get the next element
(defun check-for-list (lis name)
(if (not (listp lis))
(error (format nil "~A, requires a list of elements" name))))
(defun check-for-list-or-pattern (lis name)
(if (not (or (listp lis) (patternp lis)))
(error (format nil "~A, requires a list of elements or a pattern" name))))
(defun list-has-pattern (lis)
(dolist (e lis)
(if (patternp e) (return t))))
(defun is-homogeneous (lis)
(let (type)
(dolist (elem lis t)
(cond ((null type)
(setf type (if (patternp elem) 'pattern 'atom)))
((and (eq type 'pattern)
(not (patternp elem)))
(return nil))
((and (eq type 'atom)
(patternp elem))
(return nil))))))
(defun make-homogeneous (lis traceflag)
(cond ((is-homogeneous lis) lis)
(t
(mapcar #'(lambda (item)
(if (patternp item) item
(make-cycle (list item)
;; help debugging by naming the new pattern
;; probably, the name could be item, but
;; here we coerce item to a string to avoid
;; surprises in code that assumes string names.
:name (format nil "~A" item) :trace traceflag)))
lis))))
;; used for both "advanced to" and ":next returns" messages
;;
(send pattern-class :answer :write-trace '(verb value)
'((format t "pattern ~A ~A ~A~%"
(if name name "<no-name>")
verb
(if (patternp value)
(if (send value :name)
(send value :name)
"<a-pattern>")
value))))
;; :next returns the next value, including +eop+ and +eonp+ markers
;;
(send pattern-class :answer :next '()
'((xm-traceif ":next of" name "is-nested" is-nested "length-pattern" length-pattern)
(incf xm-next-nesting)
(let ((rslt
(cond (length-pattern (send self :next-for))
(t (send self :next-natural)))))
(if trace (send self :write-trace ":next returns" rslt))
(xm-traceif-return ":next" self rslt))))
;; :next-for returns the next value, including +eop+ and +eonp+ markers
;; this code handles the cases where :for is specified, so the length
;; of each period is explicitly given, non intrinsic to the pattern
;;
(send pattern-class :answer :next-for '()
'((block pattern:next-for-block ;; so we can return from inside while loop
(cond ((null count)
(let ((forcount (next length-pattern)))
;; in the case of window-class, there is no "natural" length
;; so for that case, we pass in forcount
(send self :start-period forcount) ;; :start-period sets count,
(setf count forcount) ;; but it is replaced here by a number
(setf have-current nil))))
;; note that merge-flag is ignored if length-pattern
(cond ((zerop count)
(setf count -1)
(return-from pattern:next-for-block +eop+))
((eql count -1)
(setf count nil)
(return-from pattern:next-for-block +eonp+)))
(while t ;; after rejecting special cases, here is where we return N items
(cond ((not have-current)
(send self :advance)
(setf have-current t)
(if trace (send self :write-trace "advanced to" current))))
(cond ((not is-nested) ;; now current is updated
(setf have-current nil)
(decf count)
(return-from pattern:next-for-block current)))
;; is-nested, so get item from sub-pattern
(let ((rslt (send current :next)))
(cond ((eq rslt +eonp+)
;; time to advance because sub-pattern has played out
(setf have-current nil))
((eq rslt +eop+)) ;; skip ends of periods; we merge them
(t
(decf count)
(return-from pattern:next-for-block rslt))))))))
;; :next-natural returns the next value, including +eop+ and +eonp+ markers
;; this code handles the cases where :for is not specified, so the length
;; of each period is implicitly determined from the pattern
;;
(send pattern-class :answer :next-natural '()
'((block pattern:next-natural-block ;; so we can return from inside while loop
(xm-traceif ":next-natural current" current)
(while t
(cond ((null count)
(setf have-current nil)
;; :merge parameter is not used by every pattern, but it does not
;; hurt to evaluate it here
(setf merge-flag (if merge-pattern (next merge-pattern)))
(send self :start-period nil))) ;; sets count
(xm-traceif "count" count "is-nested" is-nested)
(cond (is-nested
(cond ((zerop count)
(cond (merge-flag ;; we filtered out +eop+; return one here
(setf count -1)
(return-from pattern:next-natural-block +eop+))
(t
(setf count nil)
(return-from pattern:next-natural-block +eonp+))))
((eql count -1)
(setf count nil)
(return-from pattern:next-natural-block +eonp+))))
(t
(cond ((zerop count)
(setf count -1)
(return-from pattern:next-natural-block +eop+))
((eql count -1)
(setf count nil)
(return-from pattern:next-natural-block +eonp+)))))
(cond ((not have-current)
(send self :advance)
(setf have-current t)
(if trace (send self :write-trace "advanced to" current))
(xm-traceif ":advance current" current)))
(cond ((not is-nested)
(setf have-current nil)
(decf count)
(return-from pattern:next-natural-block current)))
;; nested, so get sub-pattern's next item or +eonp+ or +eop+
(let ((rslt (send current :next)))
(xm-traceif "in :next-natural got from sub-pattern " rslt)
(cond ((eq rslt +eonp+)
(setf have-current nil) ;; force advance next time, don't
;; return +eonp+ until count == 0
(decf count))
((and (eq rslt +eop+) merge-flag)) ;; iterate, skip +eop+
(t
(return-from pattern:next-natural-block rslt))))))))
(send pattern-class :answer :is-nested '() '(is-nested))
(send pattern-class :answer :name '() '(name))
(send pattern-class :answer :set-current '(c)
'((setf current c)
(let ((value
(if (patternp current)
(send current :name)
current)))
(xm-traceif ":set-current" name "value" value)
)))
;; get-pattern-name - used for debugging, handles non-patterns safely
;;
(defun get-pattern-name (pattern)
(cond ((patternp pattern) (send pattern :name))
(t pattern)))
;; more debugging support
(setf xm-next-nesting -1)
(setf *xm-trace* nil)
;; use xm-traceif for verbose printing. It only prints if *xm-trace*
;;
(defun xm-traceif (&rest items)
(if *xm-trace* (apply #'xm-trace items)))
;; use xm-traceif-return for verbose printing of return values.
;; It only prints if *xm-trace*. Includes decrement of xm-next-nesting.
;;
(defun xm-traceif-return (method pattern val)
(xm-traceif method (get-pattern-name pattern) "returning" val)
(decf xm-next-nesting)
val)
;; use xm-trace for normal tracing enabled by the trace flag in patterns
;;
(defun xm-trace (&rest items)
(princ "|")
(dotimes (i xm-next-nesting) (princ " |"))
(dolist (item items) (princ item) (princ " "))
(terpri))
;; next -- get the next element in a pattern
;;
;; any non-pattern value is simply returned
;;
(defun next (pattern &optional period-flag)
(incf xm-next-nesting)
(xm-traceif "next" (get-pattern-name pattern) period-flag)
(cond ((and period-flag (patternp pattern))
(let (rslt elem)
(incf xm-next-nesting)
(xm-traceif "next sending :next to" (get-pattern-name pattern))
(while (not (eq (setf elem (send pattern :next)) +eop+))
(xm-traceif "next got" elem "from" (get-pattern-name pattern))
(if (not (eq elem +eonp+))
(push elem rslt))
(if (null elem) (error "got null elem"))) ;;;;;;;; DEBUG ;;;;;;;;;;;
(decf xm-next-nesting)
(xm-traceif-return "next" pattern (reverse rslt))))
(period-flag
(xm-traceif "next with period-flag" (get-pattern-name pattern))
(error (format nil "~A, next expected a pattern"
(get-pattern-name pattern))))
((patternp pattern)
(xm-traceif "next with pattern" (get-pattern-name pattern) pattern)
(let (rslt)
(dotimes (i 10000 (error
(format nil
"~A, just retrieved 10000 empty periods -- is there a bug?"
(get-pattern-name pattern))))
(if (not (member (setf rslt (send pattern :next))
'(+eop+ +eonp+)))
(return (xm-traceif-return "next" pattern rslt))))))
(t ;; pattern not a pattern, so just return it:
(xm-traceif "next not pattern" pattern)
(xm-traceif-return "next" pattern pattern))))
;; ---- LENGTH Class ----
(setf length-class
(send class :new '(pattern length-pattern) '() pattern-class))
(send length-class :answer :isnew '(p l nm tr)
'((send-super :isnew nil l nm tr) ;; note: no merge pattern is applicable
(setf pattern p)))
;; note that count is used as a flag as well as a counter.
;; If count is nil, then the pattern-length has not been
;; determined. Count is nil intitially and again at the
;; end of each period. Otherwise, count is an integer
;; used to count down the number of items remaining in
;; the period.
(send length-class :answer :start-period '(forcount)
'((setf count (next length-pattern))))
(send length-class :answer :advance '()
'((send self :set-current (next pattern))))
(defun make-length (pattern length-pattern &key (name "length") trace)
(send length-class :new pattern length-pattern name trace))
;; ---- CYCLE Class ---------
(setf cycle-class (send class :new
'(lis cursor lis-pattern)
'() pattern-class))
(send cycle-class :answer :isnew '(l mp for nm tr)
'((send-super :isnew mp for nm tr)
(cond ((patternp l)
(setf lis-pattern l))
((listp l)
(send self :set-list l tr))
(t
(error (format nil "~A, expected list" nm) l)))))
(send cycle-class :answer :set-list '(l tr)
'((setf lis l)
(check-for-list lis "cycle-class :set-list")
(setf is-nested (list-has-pattern lis))
(setf lis (make-homogeneous lis tr))))
(send cycle-class :answer :start-period '(forcount)
'((xm-traceif "cycle-class :start-period" "lis-pattern"
(get-pattern-name lis-pattern) "lis" lis "count" count
"length-pattern" (get-pattern-name length-pattern))
(cond (lis-pattern
(send self :set-list (next lis-pattern t) trace)))
;; notice that list gets reset at the start of the period
(setf cursor lis)
(if (null count)
(setf count (length lis)))))
(send cycle-class :answer :advance '()
'((cond ((and (null cursor) lis)
(setf cursor lis))
((null cursor)
(error (format nil "~A, :advance - no items" name))))
(send self :set-current (car cursor))
(pop cursor)))
(defun make-cycle (lis &key merge for (name "cycle") trace)
(check-for-list-or-pattern lis "make-cycle")
(send cycle-class :new lis merge for name trace))
;; ---- LINE class ----
(setf line-class (send class :new '(lis cursor lis-pattern)
'() pattern-class))
(send line-class :answer :isnew '(l mp for nm tr)
'((send-super :isnew mp for nm tr)
(cond ((patternp l)
(setf lis-pattern l))
((listp l)
(send self :set-list l tr))
(t
(error (format nil "~A, expected list" nm) l)))))
(send line-class :answer :set-list '(l tr)
'((setf lis l)
(check-for-list lis "line-class :set-list")
(setf is-nested (list-has-pattern lis))
(setf lis (make-homogeneous l tr))
(setf cursor lis)))
(send line-class :answer :start-period '(forcount)
'((cond (lis-pattern
(send self :set-list (next lis-pattern t) trace)
(setf cursor lis)))
(if (null count)
(setf count (length lis)))))
(send line-class :answer :advance '()
'((cond ((null cursor)
(error (format nil "~A, :advance - no items" name))))
(send self :set-current (car cursor))
(if (cdr cursor) (pop cursor))))
(defun make-line (lis &key merge for (name "line") trace)
(check-for-list-or-pattern lis "make-line")
(send line-class :new lis merge for name trace))
;; ---- RANDOM class -----
(setf random-class (send class :new
'(lis lis-pattern len previous repeats mincnt maxcnt)
'() pattern-class))
;; the structure is (value weight weight-pattern max max-pattern min min-pattern)
(setfn rand-item-value car)
(defun set-rand-item-value (item value) (setf (car item) value))
(setfn rand-item-weight cadr)
(defun set-rand-item-weight (item weight) (setf (car (cdr item)) weight))
(setfn rand-item-weight-pattern caddr)
(setfn rand-item-max cadddr)
(defun set-rand-item-max (item max) (setf (car (cdddr item)) max))
(defun rand-item-max-pattern(item) (car (cddddr item)))
(defun rand-item-min (lis) (cadr (cddddr lis)))
(defun set-rand-item-min (item min) (setf (car (cdr (cddddr item))) min))
(defun rand-item-min-pattern(item) (car (cddr (cddddr item))))
(defun select-random (len lis previous repeats mincnt maxcnt)
(let (sum items r)
(cond ((zerop len)
(break "random-class has no list to choose from")
nil)
(t
(setf sum 0)
(dolist (item lis)
(setf sum (+ sum (rand-item-weight item))))
(setf items lis)
(setf r (rrandom))
(setf sum (* sum r))
(loop
(setf sum (- sum (rand-item-weight (car items))))
(if (<= sum 0) (return (car items)))
(setf items (cdr items)))))))
(defun random-convert-spec (item)
;; convert (value :weight wp :min min :max max) to (value nil wp max min)
(let (value (wp 1) minpat maxpat lis)
(setf value (car item))
(setf lis (cdr item))
(while lis
(cond ((eq (car lis) :weight)
(setf wp (cadr lis)))
((eq (car lis) :min)
(setf minpat (cadr lis)))
((eq (car lis) :max)
(setf maxpat (cadr lis)))
(t
(error "(make-random) item syntax error" item)))
(setf lis (cddr lis)))
(list value nil wp nil maxpat nil minpat)))
(defun random-atom-to-list (a)
(if (atom a)
(list a nil 1 nil nil nil nil)
(random-convert-spec a)))
(send random-class :answer :isnew '(l mp for nm tr)
;; there are two things we have to normalize:
;; (1) make all items lists
;; (2) if any item is a pattern, make all items patterns
'((xm-traceif "random :isnew list" l "merge" mp "for" for "name" nm "trace" tr)
(send-super :isnew mp for nm tr)
(cond ((patternp l)
(setf lis-pattern l))
((listp l)
(send self :set-list l))
(t
(error (format nil "~A, expected list") l)))))
(send random-class :answer :set-list '(l)
'((check-for-list l "random-class :set-list")
(setf lis (mapcar #'random-atom-to-list l))
; (display "random set-list" lis)
(dolist (item lis)
(if (patternp (rand-item-value item))
(setf is-nested t)))
(if is-nested
(mapcar #'(lambda (item)
(if (not (patternp (rand-item-value item)))
(set-rand-item-value item
(make-cycle (list (rand-item-value item))))))
lis))
(xm-traceif "random is-new" name lis)
(setf repeats 0)
(setf len (length lis))))
(send random-class :answer :start-period '(forcount)
'((xm-traceif "random-class :start-period" name "count" count "len" len
"lis" lis "lis-pattern" (get-pattern-name lis-pattern))
(cond (lis-pattern
(send self :set-list (next lis-pattern t))))
(if (null count)
(setf count len))
(dolist (item lis)
(set-rand-item-weight item (next (rand-item-weight-pattern item)))
(set-rand-item-max item (next (rand-item-max-pattern item)))
(set-rand-item-min item (next (rand-item-min-pattern item))))
; (display "random start-period" lis-pattern lis)
))
(send random-class :answer :advance '()
'((let (selection (iterations 0))
(xm-traceif "random-class :advance" name "mincnt" mincnt
"repeats" repeats)
(cond ((and mincnt (< repeats mincnt))
(setf selection previous))
(t
(setf selection
(select-random len lis previous repeats mincnt maxcnt))))
(loop ; make sure selection is ok, otherwise try again
(cond ((and (eq selection previous)
maxcnt
(>= repeats maxcnt)) ; hit maximum limit, try again
(setf selection
(select-random len lis previous repeats mincnt maxcnt))
(incf iterations)
(cond ((> iterations 10000)
(error
(format nil
"~A, unable to pick next item after 10000 tries"
name)
lis))))
(t (return)))) ; break from loop, we found a selection
; otherwise, we are ok
; notice that we could have selected based on an older maxcnt and
; maxcnt may now be smaller. This is allowed. Perhaps another
; rule would be better, e.g. update maxcnt and check against it
; with each selection.
(if (not (eq selection previous))
(setf repeats 1)
(incf repeats))
(setf mincnt (rand-item-min selection))
(setf maxcnt (rand-item-max selection))
(setf previous selection)
(xm-traceif "new selection" name "repeats" repeats "mincnt" mincnt
"maxcnt" maxcnt "selection" selection)
(send self :set-current (rand-item-value selection)))))
(defun make-random (lis &key merge for (name "random") trace)
(check-for-list-or-pattern lis "make-random")
(send random-class :new lis merge for name trace))
;; ---- PALINDROME class -----
#| Palindrome includes elide, which is either t, nil, :first, or :last.
The pattern length is the "natural" length of the pattern, which goes
forward and backward through the list. Thus, if the list is of length N,
the palindrome length depends on elide as follows:
elide length
nil 2N
t 2N - 2
:first 2N - 1
:last 2N - 1
If elide is a pattern, and if length is not specified, then length should
be computed based on elide.
|#
(setf palindrome-class (send class :new
'(lis revlis lis-pattern
direction elide-pattern
elide cursor)
'() pattern-class))
(send palindrome-class :answer :set-list '(l tr)
'((setf lis l)
(check-for-list lis "palindrome-class :start-period")
(setf is-nested (list-has-pattern lis))
(setf lis (make-homogeneous l tr))
(send self :set-cursor)))
(send palindrome-class :answer :set-cursor '()
'((setf revlis (reverse lis)
direction t
cursor lis)))
(send palindrome-class :answer :isnew '(l e mp for nm tr)
'((send-super :isnew mp for nm tr)
(cond ((patternp l)
(setf lis-pattern l))
((listp l)
(send self :set-list l tr))
(t
(error (format nil "~A, expected list" nm) l)))
(setf elide-pattern e)))
(send palindrome-class :answer :start-period '(forcount)
'((cond (lis-pattern
(send self :set-list (next lis-pattern t) trace)))
;; like cycle, list is reset at the start of the period
(send self :set-cursor)
(setf elide (next elide-pattern))
(if (and elide (null lis))
(error (format nil "~A, cannot elide if list is empty" name)))
(if (null count)
(setf count (- (* 2 (length lis))
(if (member elide '(:first :last))
1
(if elide 2 0)))))
(if (<= count 0)
(error (format nil "palindrome ~A period is <= 0"
(get-pattern-name self))))))
(send palindrome-class :answer :next-item '()
'((send self :set-current (car cursor))
(pop cursor)
(cond ((and cursor (not (cdr cursor))
(or (and direction (member elide '(:last t)))
(and (not direction) (member elide '(:first t)))))
(pop cursor)))))
(send palindrome-class :answer :advance '()
'(
(cond (cursor
(send self :next-item))
(direction ;; we're going forward
(setf direction nil) ;; now going backward
(setf cursor revlis)
(xm-traceif "palindrome at end" (get-pattern-name self)
"current" (get-pattern-name (car cursor)))
(send self :next-item))
(t ;; direction is reverse
(setf direction t)
(setf cursor lis)
(send self :next-item)))))
(defun make-palindrome (lis &key elide merge for (name "palindrome") trace)
(check-for-list-or-pattern lis "make-palindrome")
(send palindrome-class :new lis elide merge for name trace))
;; ================= HEAP CLASS ======================
;; to handle the :max keyword, which tells the object to avoid
;; repeating the last element of the previous period:
;;
;; maxcnt = 1 means "avoid the repetition"
;; check-repeat signals we are at the beginning of the period and must check
;; prev holds the previous value (initially nil)
;; after each item is generated, check-repeat is cleared. It is
;; recalculated when a new period is started.
(setf heap-class (send class :new '(lis used maxcnt maxcnt-pattern prev
check-repeat lis-pattern len)
'() pattern-class))
(send heap-class :answer :isnew '(l mp for mx nm tr)
'((send-super :isnew mp for nm tr)
(cond ((patternp l)
(setf lis-pattern l))
((listp l)
; make a copy of l to avoid side effects
(send self :set-list (append l nil) tr))
(t
(error (format nil "~A, expected list" nm) l)))
(cond ((patternp mx)
(setf maxcnt-pattern mx))
((not (numberp mx))
(error (format nil "~A, expected number" nm) mx))
(t
(setf maxcnt mx)))))
(send heap-class :answer :set-list '(l tr)
'((setf lis l)
(check-for-list lis "heap-class :set-list")
(setf is-nested (list-has-pattern lis))
(setf lis (make-homogeneous lis tr))
(setf len (length lis))))
(send heap-class :answer :start-period '(forcount)
'((xm-traceif "heap-class :start-period" name "lis-pattern"
(get-pattern-name lis-pattern) "count" count "lis" lis)
(cond (lis-pattern
(send self :set-list (next lis-pattern t) trace)))
(cond (maxcnt-pattern
(setf maxcnt (next maxcnt-pattern))))
; start of period -- may need to avoid repeating previous item
(if (= maxcnt 1) (setf check-repeat t))
(if (null count)
(setf count len))))
(defun delete-first (elem lis)
(cond ((null lis) nil)
((eq elem (car lis))
(cdr lis))
(t
(cons (car lis) (delete-first elem (cdr lis))))))
;; NO-DISTINCT-ELEM -- check if any element of list is not val
;;
(defun no-distinct-elem (lis val)
(not
(dolist (elem lis)
(if (not (equal elem val))
;; there is a distinct element, return t from dolist
(return t)))))
;; if no distinct element, dolist returns nil, but this is negated
;; by the NOT so the function will return t
(send heap-class :answer :advance '()
'((cond ((null lis)
(setf lis used)
(setf used nil)))
(let (n elem)
(cond ((and check-repeat (no-distinct-elem lis prev))
(error (format nil "~A, cannot avoid repetition, but :max is 1"
name))))
(loop
(setf n (random (length lis)))
(setf elem (nth n lis))
(if (or (not check-repeat) (not (equal prev elem)))
(return))) ;; loop until suitable element is chosen
(setf lis (delete-first elem lis))
(push elem used)
(setf check-repeat nil)
(setf prev elem)