-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterp-enumerate.rkt
More file actions
1725 lines (1403 loc) · 62.2 KB
/
interp-enumerate.rkt
File metadata and controls
1725 lines (1403 loc) · 62.2 KB
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
#lang rosette
(error-print-width 1000)
(current-bitwidth #f)
(require math/base)
(require math/statistics)
(require racket/async-channel)
(require rosette/solver/smt/z3)
(require srfi/19)
(require data/heap)
(require json)
(require "../rosette/rosette/solver/smt/server.rkt")
(require "dates.rkt")
(require "local-engine.rkt")
(require "threads.rkt")
(require "utils.rkt")
(define timestep 0)
(define (function-order l r)
(or
(< (caar l) (caar r))
(and (= (caar l) (caar r))
(< (cdar l) (cdar r)))))
(define function-queue (make-heap function-order))
(define vals (make-hash))
(define (sign b)
(cond
[(positive? b) 1]
[(zero? b) 0]
[(negative? b) -1]
))
(define/match (var? e)
[((expression op child ...)) #f]
[((? constant?)) #t]
[(_) #f])
(define (id pos)
(string->symbol (format "~S" pos)))
(define (clear-vals!)
(hash-clear! vals))
(define (val pos type)
(let ((name (id pos)))
(if (hash-has-key? vals (cons name type))
(hash-ref vals (cons name type))
(let ((v (constant name type)))
(hash-set! vals (cons name type) v)
v))))
(define (merge . args)
(apply append (filter cons? args)))
(define logging-processor%
(class object%
(super-new)
(define/public (invalid v)
#f)
(define/public (calculate-cost v)
(let ((l (map (lambda (q) (hash-ref func_to_costs q 1)) v)))
(* (apply max l) (length l))))
(define/public (constant v)
'())
(define/public (basic-unary f v)
(cons f v))
(define/public (basic-binary f l r)
(cons f (merge l r)))
(define/public (is-null-v? mb v pos)
(cons 'is-null v))
(define/public (is-null? pos)
(list do-is-null?))
(define/public (in pos type-f)
(if (eq? type-f number?)
(list do-in-int)
(list do-in-str)))
(define/public (in-v pos v type-f)
(if (eq? type-f number?)
(list do-in-int)
(list do-in-str)))
(define/public (symbolic pos type)
(list
(if (equal? type integer?)
do-intv
do-strv)))
(define/public (basic-math pos l r)
(cons do-basic-math (merge l r)))
(define/public (basic-num-functions pos v)
(merge (list do-basic-num-functions) v))
(define/public (index-of pos l r)
(cons do-index-of (merge l r)))
(define/public (compare-to pos l r)
(cons do-compare-to (merge l r)))
(define/public (compare-to-str pos l r)
(cons do-compare-to-str (merge l r)))
(define/public (logic-op pos l r)
(cons do-logic-op (merge l r)))
(define/public (logic-op-not pos v)
(merge (list do-logic-op-not) v))
(define/public (if-then-else-str case l r)
(cons do-if-then-str (merge case l r)))
(define/public (if-then-else-int case l r)
(cons do-if-then-int (merge case l r)))
(define/public (if-then-else-any case l r)
(cons do-if-then-any (merge case l r)))
(define/public (strlength pos str)
(merge (list do-length) str))
(define/public (substr str l r)
(cons do-substring (merge str l r)))
(define/public (concat pos left right)
(cons do-concat (merge left right)))
(define/public (get-digits pos str)
(merge (list do-get-digits) str))
(define/public (trim pos str)
(merge (list do-trim) str))
(define/public (date-diff pos left right)
(merge (list do-date-diff) left right))
(define/public (date-compare pos left right)
(merge (list do-date-compare) left right))
(define/public (date-interval pos left right)
(merge (list do-date-interval) left right))
(define/public (date-extract pos v)
(merge (list do-date-extract) v))
(define/public (date-to-epoch pos v)
(merge (list do-date-to-epoch) v))
(define/public (date-from-epoch pos v)
(merge (list do-date-from-epoch) v))
(define/public (like pos lhs rhs)
(merge (list do-like) lhs rhs))
(define/public (set-seconds-to l r)
(merge (list 'set-seconds-to) l r))
(define/public (set-minutes-to l r)
(merge (list 'set-minutes-to) l r))
(define/public (set-hours-to l r)
(merge (list 'set-hours-to) l r))
(define/public (set-days-to l r)
(merge (list 'set-days-to) l r))
(define/public (set-months-to l r)
(merge (list 'set-months-to) l r))
(define/public (set-years-to l r)
(merge (list 'set-years-to) l r))
(define/public (set-to-first-day-of-month v)
(merge (list 'set-to-first-day-of-month) v))
(define/public (set-to-last-day-of-month v)
(merge (list 'set-to-last-day-of-month) v))
(define/public (set-to-first-month v)
(merge (list 'set-to-first-month) v))
(define/public (set-to-last-month v)
(merge (list 'set-to-last-month) v))
(define/public (set-to-next-day v)
(merge (list 'set-to-next-day) v))
(define/public (set-to-previous-day v)
(merge (list 'set-to-previous-day) v))
(define/public (set-to-next-month v)
(merge (list 'set-to-next-month) v))
(define/public (set-to-previous-month v)
(merge (list 'set-to-previous-month) v))
(define/public (is-in-current-month v)
(merge (list 'is-in-current-month) v))
(define/public (is-in-last-x-months l r)
(merge (list 'is-in-last-x-months) l r))
(define/public (general-compare date-op number-op left right)
(merge (list 'general-compare) number-op left right))
(define/public (aggregate-op pos type v op is-average)
(merge (list 'agg) op v))
(define/public (aggregate pos type v)
(let* ((op
(if (eq? type 'string)
'group-concat
(let ((v1 (val (cons 1 pos) boolean?))
(v2 (val (cons 2 pos) boolean?)))
(cond ((and v1 v2) 'sum)
((and v1 (not v2)) 'max)
(#t 'min))))))
(merge (list op) v)))))
(define doc-processor%
(class object%
(super-new)
(define/public (invalid v)
#f)
(define/public (calculate-cost v)
0)
(define/public (constant v)
v)
(define/public (basic-unary f v)
(list f v))
(define/public (basic-binary f l r)
(list (if (equal? f equal?) '== (object-name f)) l r))
(define/public (is-null-v? mb v pos)
(list (if mb 'is-null 'is-not-null) v))
(define/public (is-null? pos)
(let ((mb (val (cons 'is-null pos) boolean?))
(mi (val (cons 'argn pos) integer?)))
(list (if mb 'is-null 'is-not-null) mi)))
(define/public (in pos type-f)
(let ((m (val (cons 'argn pos) integer?)))
(list 'in m)))
(define/public (in-v pos v type-f)
(list 'in v))
(define/public (symbolic pos type)
(val pos type))
(define/public (basic-math pos l r)
(let ((m1 (val (cons 'm1 pos) boolean?))
(m2 (val (cons 'm2 pos) boolean?))
(m3 (val (cons 'm3 pos) boolean?)))
(list
(cond
[(and m1 (not m2) m3) /]
[(and m1 (not m2) (not m3)) *]
[(and (not m1) m2 m3) -]
[(and m1 m2 m3) +])
l r)))
(define/public (basic-num-functions pos v)
(let ((m1 (val (cons 'm1 pos) boolean?))
(m2 (val (cons 'm2 pos) boolean?))
(m3 (val (cons 'm3 pos) boolean?)))
(list
(if m1 (if m2 (if m3 'abs 'truncate) (if m3 'sign 'ceiling)) (if m2 (if m3 'floor 'lifted-round) (if m3 'quotient 'remainder))) v)))
(define/public (index-of pos l r)
(list 'index-of l r))
(define/public (compare-to pos l r)
(let ((islt (val (cons 'islt pos) boolean?))
(iseq (val (cons 'iseq pos) boolean?)))
(list (if iseq (if islt '<= '>=) (if islt '< '>)) l r)))
(define/public (compare-to-str pos l r)
(list '== l r))
(define/public (logic-op pos l r)
(let ((isand (val (cons 'isand pos) boolean?)))
(list (if isand 'and 'or) l r)))
(define/public (logic-op-not pos v)
(list 'not v))
(define/public (if-then-else-str case l r)
(list 'if case l r))
(define/public (if-then-else-int case l r)
(list 'if case l r))
(define/public (if-then-else-any case l r)
(list 'if case l r))
(define/public (strlength pos str)
(list 'length str))
(define/public (substr str l r)
(list 'substring str l r))
(define/public (concat pos left right)
(list 'concat left right))
(define/public (date-diff pos left right)
(list 'date-subtract left right))
(define/public (date-compare pos left right)
(let ((di1 (val (cons 'di1 pos) boolean?))
(di2 (val (cons 'di2 pos) boolean?))
(di3 (val (cons 'di3 pos) boolean?)))
(list
(if di1
(if di2
(if di3 'date-le 'date-ge)
(if di3 'date-gt 'date-lt))
(when di2 'date-equal))
left right)))
(define/public (date-interval pos left right)
(let ((di1 (val (cons 'di1 pos) boolean?))
(di2 (val (cons 'di2 pos) boolean?))
(di3 (val (cons 'di3 pos) boolean?))
(di4 (val (cons 'di4 pos) boolean?)))
(list
(if di1
(if di2
(if di3
(if di4 'add-seconds 'add-minutes)
(if di4 'add-hours 'add-days))
(if di3
(if di4 'add-months 'add-years)
(if di4 'subtract-seconds 'subtract-minutes)))
(if di2
(if di3 subtract-hours subtract-days)
(if di3 subtract-months subtract-years)))
left right)))
(define/public (date-extract pos v)
(let ((de1 (val (cons 'de1 pos) boolean?))
(de2 (val (cons 'de2 pos) boolean?))
(de3 (val (cons 'de3 pos) boolean?)))
(list
(if de1
(if de2
(if de3 'extract-seconds 'extract-minutes)
(if de3 'extract-hours 'extract-days))
(if de2
(if de3 'extract-months 'extract-years)
(if de3 'extract-day-of-year 'extract-day-of-week))) v)))
(define/public (date-to-epoch pos v)
(list 'date-to-epoch v))
(define/public (set-seconds-to l r)
(list 'set-seconds-to l r))
(define/public (set-minutes-to l r)
(list 'set-minutes-to l r))
(define/public (set-hours-to l r)
(list 'set-hours-to l r))
(define/public (set-days-to l r)
(list 'set-days-to l r))
(define/public (set-months-to l r)
(list 'set-months-to l r))
(define/public (set-years-to l r)
(list 'set-years-to l r))
(define/public (set-to-first-day-of-month v)
(list 'set-to-first-day-of-month v))
(define/public (set-to-last-day-of-month v)
(list 'set-to-last-day-of-month v))
(define/public (set-to-first-month v)
(list 'set-to-first-month v))
(define/public (set-to-last-month v)
(list 'set-to-last-month v))
(define/public (set-to-next-day v)
(list 'set-to-next-day v))
(define/public (general-compare date-op number-op left right)
(if (equal? number-op equal?)
(list '= left right) (list number-op left right)))
(define/public (set-to-previous-day v)
(list 'set-to-previous-day v))
(define/public (set-to-next-month v)
(list 'set-to-next-month v))
(define/public (set-to-previous-month v)
(list 'set-to-previous-month v))
(define/public (is-in-current-month v)
(list 'is-in-current-month v))
(define/public (is-in-last-x-months l r)
(list 'is-in-last-x-months l r))
(define/public (date-from-epoch pos v)
(list 'date-from-epoch v))
(define/public (get-digits pos str)
(list 'get-digits str))
(define/public (trim pos str)
(list 'trim str))
(define/public (aggregate-op pos type v op is-average)
(list 'agg op v))
(define/public (like pos lhs rhs)
(let ((m1 (val (cons 'li1 pos) boolean?))
(m2 (val (cons 'li2 pos) boolean?)))
(list 'like lhs
(if m1
(if m2 (string-append rhs "%") (string-append "%" rhs))
(if m2 (string-append "%" rhs "%") rhs)))))
(define/public (aggregate pos type v)
(let ((op
(if (eq? type 'string)
'group-concat
(let ((v1 (val (cons 1 pos) boolean?))
(v2 (val (cons 2 pos) boolean?)))
(cond ((and v1 v2) 'min)
((and v1 (not v2)) 'max)
((and (not v1) (not v2)) 'avg)
(#t 'sum))))))
(list 'agg op v)))))
(define (basic-math-op r pos + - * / )
(let ((m1 (val (cons 'm1 pos) boolean?))
(m2 (val (cons 'm2 pos) boolean?))
(m3 (val (cons 'm3 pos) boolean?)))
(cond [(and m1 m2 m3) +]
[(and m1 (not m2) m3) (if (not (= r 0)) / 'invalid)]
[(and m1 (not m2) (not m3)) *]
[(and (not m1) m2 m3) -]
[#t 'invalid])))
(define (lifted-round v)
(truncate (+ v .5)))
(define percent-regex (regexp (string-append "[" (~a (integer->char 33)) "-" (~a (integer->char 126)) "]*")))
(define underscore-regex (regexp (string-append "[" (~a (integer->char 33)) "-" (~a (integer->char 126)) "]")))
(define (create-like-list indexes pat)
(let ((result (for/list ([x indexes]
[y (in-range (length indexes))])
(let ((z (if (equal? (string-ref pat x) #\%)
percent-regex
underscore-regex)))
(cond
[(and (> x 0) (= y 0)) (list (regexp-quote (substring pat 0 x)) z)]
[(and (> x 0) (> y 0)) (if (> (- x (list-ref indexes (- y 1))) 1)
(list (regexp-quote (substring pat (+ (list-ref indexes (- y 1)) 1) x)) z)
(list z))]
[#t (list z)])))))
(if (< (last indexes) (- (string-length pat) 1))
(flatten (append result (list (regexp-quote (substring pat (+ (last indexes) 1) (string-length pat))))))
(flatten result))))
(define (create-like-constraints lhs like-list)
(regexp-match-exact? (apply regexp-concat like-list) lhs))
(define (convert-string-to-date str)
(with-handlers ([exn:fail?
(lambda (e) #f)])
(let ((d (string->date str "~m-~d-~Y")))
(vector (date-second d) (date-minute d) (date-hour d) (date-day d) (date-month d) (date-year d)))))
(define (dateable? v)
(or (vector? v)
(and
(string? v)
(not (symbolic? v))(convert-string-to-date v))))
(define-syntax date-case-op
(syntax-rules ()
((_ (arg ...) (body ...) (otherwise ...))
(if (and (dateable? arg) ...)
(let ((arg (if (vector? arg) arg (convert-string-to-date arg))) ...)
body ...)
(if (and (not (eq? arg 'invalid)) ...) (begin otherwise ...) 'invalid)))))
(define-syntax date-op
(syntax-rules ()
((_ (arg ...) body ...)
(date-case-op (arg ...) (body ...) ('invalid)))))
(define (like-constant lhs rhs)
(if (and (string? lhs) (string? rhs))
(let* ((indexes
(filter (lambda (x) (not (equal? x -1)))
(for/list ([i rhs]
[k (in-range (string-length rhs))])
(if (or (equal? i #\%) (equal? i #\_)) k -1))))
(ll (create-like-list indexes rhs))
(lc (create-like-constraints lhs ll)))
; (println "like list")
; (println ll)
; (println "constraint")
; (println lc)
lc)
'invalid))
(define expr-processor%
(class object%
(init inputs)
(super-new)
(define input-vals inputs)
(define/public (invalid v)
(eq? v 'invalid))
(define/public (calculate-cost v)
0)
(define/public (constant v)
(let ((r
(if (not (vector? v))
v
(let ((s (extract-seconds v))
(m (extract-minutes v))
(h (extract-hours v))
(dy (extract-days v))
(mn (extract-months v))
(yr (extract-years v)))
(if (and (>= s 0) (< s 60)
(>= m 0) (< m 60)
(>= h 0) (< h 24)
(> dy 0) (<= dy 31)
(> mn 0) (<= mn 12)
(> yr 0))
v
'invalid)))))
;; (println "value")
;; (println r)
r))
(define/public (basic-unary f v)
(f v))
(define/public (basic-binary f l r)
;; (println (type-of l))
;; (println (type-of r))
(if (or (eq? l 'invalid) (eq? r 'invalid))
'invalid
(f l r)))
(define/public (set-seconds-to l r)
(create-date r (get-field l "minutes") (get-field l "hours") (get-field l "days") (get-field l "months") (get-field l "years")))
(define/public (set-minutes-to l r)
(create-date (get-field l "seconds") r (get-field l "hours") (get-field l "days") (get-field l "months") (get-field l "years")))
(define/public (set-hours-to l r)
(create-date (get-field l "seconds") (get-field l "minutes") r (get-field l "days") (get-field l "months") (get-field l "years")))
(define/public (set-days-to l r)
(create-date (get-field l "seconds") (get-field l "minutes") (get-field l "hours") r (get-field l "months") (get-field l "years")))
(define/public (set-months-to l r)
(create-date (get-field l "seconds") (get-field l "minutes") (get-field l "hours") (get-field l "days") r (get-field l "years")))
(define/public (set-years-to l r)
(create-date (get-field l "seconds") (get-field l "minutes") (get-field l "hours") (get-field l "days") (get-field l "months") r))
(define/public (set-to-first-day-of-month v)
(create-date (get-field v "seconds") (get-field v "minutes") (get-field v "hours") 1 (get-field v "months") (get-field v "years")))
(define/public (set-to-last-day-of-month v)
(create-date (get-field v "seconds") (get-field v "minutes") (get-field v "hours") (num-days-in-month (get-field v "months")) (get-field v "years")))
(define/public (set-to-first-month v)
(create-date (get-field v "seconds") (get-field v "minutes") (get-field v "hours") (get-field v "days") 1 (get-field v "years")))
(define/public (set-to-last-month v)
(create-date (get-field v "seconds") (get-field v "minutes") (get-field v "hours") (get-field v "days") 12 (get-field v "years")))
(define/public (set-to-next-day v)
(add-days v 1))
(define/public (set-to-previous-day v)
(subtract-days v 1))
(define/public (set-to-next-month v)
(add-months v 1))
(define/public (set-to-previous-month v)
(subtract-months v 1))
(define/public (is-in-current-month v)
(equal? (extract-months (extract-date-from-epoch (current-seconds))) (extract-months v)))
(define/public (is-in-last-x-months l r)
(>= l (subtract-months (extract-date-from-epoch (current-seconds)) r)))
(define/public (is-null-v? mb v pos)
(send this basic-unary (lambda (v) (if mb (equal? v '()) (not (equal? v '())))) v))
(define/public (is-null? pos)
(let ((mb (val (cons 'is-null pos) boolean?))
(mi (val (cons 'argn pos) integer?)))
(if (and (>= mi 1) (< (- mi 1) (length input-vals)))
(let ((v (list-ref input-vals (- mi 1))))
(send this is-null-v? mb v pos))
'invalid)))
(define/public (in pos type-f)
(let ((m (val (cons 'argn pos) integer?)))
(if (and (>= m 1) (< (- m 1) (length input-vals)))
(send this in-v pos m type-f)
'invalid)))
(define/public (in-v pos v type-f)
(let ((val (list-ref input-vals (- v 1))))
(if (type-f val)
(send this constant val)
'invalid)))
(define/public (symbolic pos type)
(val pos type))
(define/public (basic-math pos l r)
(if (and (number? l) (number? r))
(let ((op (basic-math-op r pos + - * /)))
(if (eq? op 'invalid)
'invalid
(send this basic-binary op l r)))
'invalid))
(define/public (basic-num-functions pos v)
(if (number? v)
(let ((m1 (val (cons 'm1 pos) boolean?))
(m2 (val (cons 'm2 pos) boolean?))
(m3 (val (cons 'm3 pos) boolean?)))
(send this basic-unary (if m1 (if m2 (if m3 abs truncate) (if m3 sign ceiling)) (if m2 (if m3 floor lifted-round) (if m3 quotient remainder))) v))
'invalid))
(define/public (index-of pos l r)
(if (and (string? l) (string? r) (<= (string-length r) (string-length l)))
(string-index-of l r)
'invalid))
(define/public (compare-to pos l r)
(if (and (number? l) (number? r))
(let ((islt (val (cons 'islt pos) boolean?))
(iseq (val (cons 'iseq pos) boolean?)))
(send this basic-binary (if iseq (if islt <= >=) (if islt < >)) l r))
'invalid))
(define/public (compare-to-str pos l r)
(send this basic-binary equal? l r))
(define/public (logic-op-not pos v)
(if (boolean? v)
(send this basic-unary not v)
'invalid))
(define/public (logic-op pos l r)
(if (and (boolean? l) (boolean? r))
(let ((isand (val (cons 'isand pos) boolean?)))
(send this basic-binary (lambda (l r) (if isand (and l r) (or l r))) l r))
'invalid))
(define/public (if-then-else-str case l r)
(if (and (boolean? case) (string? l) (string? r))
(if case l r)
'invalid))
(define/public (if-then-else-int case l r)
(if (and (boolean? case) (number? l) (number? r))
(if case l r)
'invalid))
(define/public (if-then-else-any case l r)
(if (boolean? case)
(if case l r)
'invalid))
(define/public (strlength pos str)
(if (string? str)
(send this basic-unary string-length str)
'invalid))
(define/public (substr str l r)
;(println "types")
;(println str)
;(println (type-of str))
;(println (type-of l))
;(println (type-of r))
(let ((v (if (and (string? str) (integer? l) (integer? r) (>= l 0) (<= l r) (<= r (string-length str)))
(substring str l r)
'invalid)))
;(println "substr")
;(println v)
v))
(define/public (concat pos left right)
(let (( v (if (and (string? left) (string? right))
(send this basic-binary string-append left right)
'invalid)))
;(println "concat")
;(println v)
v))
(define/public (date-diff pos left right)
(date-op (left right) (date-subtract left right)))
(define/public (date-compare pos left right)
(let ((di1 (val (cons 'di1 pos) boolean?))
(di2 (val (cons 'di2 pos) boolean?))
(di3 (val (cons 'di3 pos) boolean?)))
(if (and (not di1) (not di2) (not di3))
'invalid
(date-op (left right) ((if di1
(if di2
(if di3
date-le
date-ge)
(if di3
date-gt date-lt))
date-equal) left right)))))
(define/public (date-interval pos left right)
(date-op (left)
(if (integer? right)
(let ((d (copy-date left))
(di1 (val (cons 'di1 pos) boolean?))
(di2 (val (cons 'di2 pos) boolean?))
(di3 (val (cons 'di3 pos) boolean?))
(di4 (val (cons 'di4 pos) boolean?)))
(if (and (not di1) (not di2)(not di3))
'invalid
(send this basic-binary
(if di1
(if di2
(if di3
(if di4 add-seconds add-minutes)
(if di4 add-hours add-days))
(if di3
(if di4 add-months add-years)
(if di4 subtract-seconds subtract-minutes)))
(if di2
(if di3 subtract-hours subtract-days)
(if di3 subtract-months subtract-years)))
d right))
d)
'invalid)))
(define/public (date-extract-bin pos v f)
(if (vector? v)
(f v)
'invalid))
(define/public (date-extract pos v)
(date-op (v)
(let ((de1 (val (cons 'de1 pos) boolean?))
(de2 (val (cons 'de2 pos) boolean?))
(de3 (val (cons 'de3 pos) boolean?)))
(date-extract-bin pos v
(if de1
(if de2
(if de3 extract-seconds extract-minutes)
(if de3 extract-hours extract-days))
(if de2
(if de3 extract-months extract-years)
(if de3 extract-day-of-year extract-day-of-week)))))))
(define/public (general-compare date-op number-op left right)
(date-case-op (left right) ((date-op left right)) ((number-op left right))))
(define/public (date-to-epoch pos v)
(date-op (v)
(send this basic-unary extract-epoch v)))
(define/public (date-from-epoch pos v)
(if (integer? v)
(send this basic-unary extract-date-from-epoch v)
'invalid))
(define/public (like pos lhs rhs)
; (println "like")
; (println "rhs")
; (println rhs)
; (println "lhs")
; (println lhs)
(if (and (string? lhs) (string? rhs))
(let ((m1 (val (cons 'li1 pos) boolean?))
(m2 (val (cons 'li2 pos) boolean?)))
; (when (and (not m1) (not m2)) (begin (println rhs) (println "about to call like-constant")))
(for/all ([r rhs #:exhaustive])
(if (and (not m1) (not m2))
; (println r)
(if (not (symbolic? r))
(like-constant lhs r)
'invalid)
(let ((expr
(if m1
(if m2
(string-suffix? lhs rhs)
(string-prefix? lhs rhs))
(string-contains? lhs rhs))))
; (println "expr")
; (println expr)
; (println "expr")
expr))))
'invalid))
(define/public (trim pos str)
(let ((s1 (val (cons 'b str) string?))
(s2 (val (cons 'd str) string?))
(s3 (val (cons 'a str) string?)))
(if (and (equal? str (string-append s1 s2 s3))
(regexp-match-exact? #rx"[ ]*" s1)
(regexp-match-exact? #rx"[ ]*" s3)
(regexp-match-exact? #rx"[A-Za-z0-9_:.,?']+[A-Za-z0-9_:.,?' ]*[A-Za-z0-9_:.,?']+" s2))
s2
'invalid)))
(define/public (get-digits pos str)
(let ((s1 (val (cons 'b str) string?))
(s2 (val (cons 'd str) string?))
(s3 (val (cons 'a str) string?)))
(if (and (equal? str (string-append s1 s2 s3))
(regexp-match-exact? #rx"[A-Za-z]*" s1)
(regexp-match-exact? #rx"[A-Za-z]*" s3)
(regexp-match-exact? #rx"[0-9]+" s2))
s2
'invalid)))))
(define compound-processor%
(class object%
(init children [extras '()] [ordering-function '()])
(super-new)
(init-field [extra-functions extras]
[processors children]
[ordering ordering-function])
(define/public (invalid vs)
(for/fold ([result #f])
([p processors]
[v vs])
(or result (send p invalid v))))
(define/public (calculate-cost vs)
(for/fold ([result 0])
([p processors]
[v vs])
(apply max (list (send p calculate-cost v) result))))
(define/public (extra-f)
extra-functions)
(define/public (get-ordering-function)
ordering)
(define/public (constant v)
(for/list ([p processors])
(send p constant v)))
(define/public (basic-unary f value)
(for/list ([p processors] [v value])
(send p basic-unary f v)))
(define/public (basic-binary f left right)
(for/list ([p processors] [l left] [r right])
(send p basic-binary f l r)))
(define/public (is-null-v? mb vs pos)
(for/list ([p processors] [v vs])
(send p is-null-v? mb v pos)))
(define/public (is-null? pos)
(for/list ([p processors])
(send p is-null? pos)))
(define/public (in pos type-f)
(for/list ([p processors])
(send p in pos type-f)))
(define/public (in-v pos v type-f)
(for/list ([p processors])
(send p in-v pos v type-f)))
(define/public (symbolic pos type)
(for/list ([p processors])
(send p symbolic pos type)))
(define/public (basic-num-functions pos v)
(for/list ([p processors] [vs v])
(send p basic-num-functions pos vs)))
(define/public (basic-math pos left right)
(for/list ([p processors] [l left] [r right])
(send p basic-math pos l r)))
(define/public (index-of pos left right)
(for/list ([p processors] [l left] [r right])
(send p index-of pos l r)))
(define/public (date-compare pos left right)
(for/list ([p processors] [l left] [r right])
(send p date-compare pos l r)))
(define/public (compare-to pos left right)
(for/list ([p processors] [l left] [r right])
(send p compare-to pos l r)))
(define/public (compare-to-str pos left right)
(for/list ([p processors] [l left] [r right])
(send p compare-to-str pos l r)))
(define/public (logic-op-not pos v)
(for/list ([p processors] [vs v])
(send p logic-op-not pos vs)))
(define/public (if-then-else-str cases left right)
(for/list ([p processors] [case cases] [l left] [r right])
(send p if-then-else-str case l r)))
(define/public (if-then-else-int cases left right)
(for/list ([p processors] [case cases] [l left] [r right])
(send p if-then-else-int case l r)))
(define/public (if-then-else-any cases left right)
(for/list ([p processors] [case cases] [l left] [r right])
(send p if-then-else-any case l r)))
(define/public (logic-op pos left right)
(for/list ([p processors] [l left] [r right])
(send p logic-op pos l r)))
(define/public (strlength pos strs)
(for/list ([p processors] [str strs])
(send p strlength pos str)))
(define/public (substr strs left right)
(for/list ([p processors] [str strs] [l left] [r right])
(send p substr str l r)))
(define/public (concat pos left right)
(for/list ([p processors] [l left] [r right])
(send p concat pos l r)))
(define/public (get-digits pos strs)
(for/list ([p processors] [s strs])
(send p get-digits pos s)))
(define/public (trim pos strs)
(for/list ([p processors] [s strs])
(send p trim pos s)))
(define/public (date-diff pos left right)
(for/list ([p processors] [l left] [r right])
(send p date-diff pos l r)))
(define/public (date-interval pos left right)
(for/list ([p processors] [l left] [r right])
(send p date-interval pos l r)))
(define/public (like pos left right)