forked from michaelballantyne/faster-minikanren
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mk.scm
1158 lines (1003 loc) · 30.9 KB
/
mk.scm
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
; Scope object.
; Used to determine whether a branch has occured between variable
; creation and unification to allow the set-var-val! optimization
; in subst-add. Both variables and substitutions will contain a
; scope. When a substitution flows through a conde it is assigned
; a new scope.
; Creates a new scope that is not scope-eq? to any other scope
(define new-scope
(lambda ()
(list 'scope)))
; Scope used when variable bindings should always be made in the
; substitution, as in disequality solving and reification. We
; don't want to set-var-val! a variable when checking if a
; disequality constraint holds!
(define nonlocal-scope
(list 'non-local-scope))
(define scope-eq? eq?)
; Logic variable object.
; Contains:
; val - value for variable assigned by unification using
; set-var-val! optimization. unbound if not yet set or
; stored in substitution.
; scope - scope that the variable was created in.
; idx - unique numeric index for the variable. Used by the
; trie substitution representation.
; Variable objects are compared by object identity.
; The unique val for variables that have not yet been bound
; to a value or are bound in the substitution
(define unbound (list 'unbound))
(define var
(let ((counter -1))
(lambda (scope)
(set! counter (+ 1 counter))
(vector unbound scope counter))))
; Vectors are not allowed as terms, so terms that are vectors
; are variables.
(define var?
(lambda (x)
(vector? x)))
(define var-eq? eq?)
(define var-val
(lambda (x)
(vector-ref x 0)))
(define set-var-val!
(lambda (x v)
(vector-set! x 0 v)))
(define var-scope
(lambda (x)
(vector-ref x 1)))
(define var-idx
(lambda (x)
(vector-ref x 2)))
; Substitution object.
; Contains:
; map - mapping of variables to values
; scope - scope at current program point, for set-var-val!
; optimization. Updated at conde. Included in the substitution
; because it is required to fully define the substitution
; and how it is to be extended.
;
; Implementation of the substitution map depends on the Scheme used,
; as we need a map. See mk.rkt and mk-vicare.scm.
(define subst
(lambda (mapping scope)
(cons mapping scope)))
(define subst-map car)
(define subst-scope cdr)
(define subst-length
(lambda (S)
(subst-map-length (subst-map S))))
(define subst-with-scope
(lambda (S new-scope)
(subst (subst-map S) new-scope)))
(define empty-subst (subst empty-subst-map (new-scope)))
(define subst-add
(lambda (S x v)
; set-var-val! optimization: set the value directly on the
; variable object if we haven't branched since its creation
; (the scope of the variable and the substitution are the same).
; Otherwise extend the substitution mapping.
(if (scope-eq? (var-scope x) (subst-scope S))
(begin
(set-var-val! x v)
S)
(subst (subst-map-add (subst-map S) x v) (subst-scope S)))))
(define subst-lookup
(lambda (u S)
; set-var-val! optimization.
; Tried checking the scope here to avoid a subst-map-lookup
; if it was definitely unbound, but that was slower.
(if (not (eq? (var-val u) unbound))
(var-val u)
(subst-map-lookup u (subst-map S)))))
; Association object.
; Describes an association mapping the lhs to the rhs. Returned by
; unification to describe the associations that were added to the
; substitution (whose representation is opaque) and used to represent
; disequality constraints.
(define lhs car)
(define rhs cdr)
; Constraint record object.
;
; Describes the constraints attached to a single variable.
;
; Contains:
; T - type constraint. 'symbolo 'numbero or #f to indicate
; no constraint
; D - list of disequality constraints. Each disequality is a list of
; associations. The constraint is violated if all associated
; variables are equal in the substitution simultaneously. D
; could contain duplicate constraints (created by distinct =/=
; calls). A given disequality constraint is only attached to
; one of the variables involved, as all components of the
; constraint must be violated to cause failure.
; A - list of absento constraints. Each constraint is a ground atom.
; The list contains no duplicates.
(define empty-c `(#f () ()))
(define c-T
(lambda (c)
(car c)))
(define c-D
(lambda (c)
(cadr c)))
(define c-A
(lambda (c)
(caddr c)))
(define c-with-T
(lambda (c T)
(list T (c-D c) (c-A c))))
(define c-with-D
(lambda (c D)
(list (c-T c) D (c-A c))))
(define c-with-A
(lambda (c A)
(list (c-T c) (c-D c) A)))
; Constraint store object.
; Mapping of representative variable to constraint record. Constraints
; are always on the representative element and must be moved / merged
; when that element changes.
; Implementation depends on the Scheme used, as we need a map. See
; mk.rkt and mk-vicare.scm.
; State object.
; The state is the value that is monadically passed through the search
; Contains:
; S - the substitution
; C - the constraint store
(define state
(lambda (S C)
(cons S C)))
(define state-S (lambda (st) (car st)))
(define state-C (lambda (st) (cdr st)))
(define empty-state (state empty-subst empty-C))
(define state-with-scope
(lambda (st new-scope)
(state (subst-with-scope (state-S st) new-scope) (state-C st))))
; Unification
(define walk
(lambda (u S)
(if (var? u)
(let ((val (subst-lookup u S)))
(if (eq? val unbound)
u
(walk val S)))
u)))
(define occurs-check
(lambda (x v S)
(let ((v (walk v S)))
(cond
((var? v) (var-eq? v x))
((pair? v)
(or
(occurs-check x (car v) S)
(occurs-check x (cdr v) S)))
(else #f)))))
(define ext-s-check
(lambda (x v S)
(cond
((occurs-check x v S) (values #f #f))
(else (values (subst-add S x v) `((,x . ,v)))))))
; Returns as values the extended substitution and a list of
; associations added during the unification, or (values #f #f) if the
; unification failed.
;
; Right now appends the list of added values from sub-unifications.
; Alternatively could be threaded monadically, which could be faster
; or slower.
(define unify
(lambda (u v s)
(let ((u (walk u s))
(v (walk v s)))
(cond
((eq? u v) (values s '()))
((var? u) (ext-s-check u v s))
((var? v) (ext-s-check v u s))
((and (pair? u) (pair? v))
(let-values (((s added-car) (unify (car u) (car v) s)))
(if s
(let-values (((s added-cdr) (unify (cdr u) (cdr v) s)))
(values s (append added-car added-cdr)))
(values #f #f))))
((equal? u v) (values s '()))
(else (values #f #f))))))
(define unify*
(lambda (S+ S)
(unify (map lhs S+) (map rhs S+) S)))
; Search
; SearchStream: #f | Procedure | State | (Pair State (-> SearchStream))
; SearchStream constructor types. Names inspired by the plus monad?
; -> SearchStream
(define mzero (lambda () #f))
; c: State
; -> SearchStream
(define unit (lambda (c) c))
; c: State
; f: (-> SearchStream)
; -> SearchStream
;
; f is a thunk to avoid unnecessary computation in the case that c is
; the last answer needed to satisfy the query.
(define choice (lambda (c f) (cons c f)))
; e: SearchStream
; -> (-> SearchStream)
(define-syntax inc
(syntax-rules ()
((_ e) (lambda () e))))
; Goal: (State -> SearchStream)
; e: SearchStream
; -> Goal
(define-syntax lambdag@
(syntax-rules ()
((_ (st) e) (lambda (st) e))))
; Match on search streams. The state type must not be a pair with a
; procedure in its cdr.
;
; (() e0) failure
; ((f) e1) inc for interleaving. separate from success or failure
; to ensure it goes all the way to the top of the tree.
; ((c) e2) single result. Used rather than (choice c (inc (mzero)))
; to avoid returning to search a part of the tree that
; will inevitably fail.
; ((c f) e3) multiple results.
(define-syntax case-inf
(syntax-rules ()
((_ e (() e0) ((f^) e1) ((c^) e2) ((c f) e3))
(let ((c-inf e))
(cond
((not c-inf) e0)
((procedure? c-inf) (let ((f^ c-inf)) e1))
((not (and (pair? c-inf)
(procedure? (cdr c-inf))))
(let ((c^ c-inf)) e2))
(else (let ((c (car c-inf)) (f (cdr c-inf)))
e3)))))))
; c-inf: SearchStream
; f: (-> SearchStream)
; -> SearchStream
;
; f is a thunk to avoid unnecesarry computation in the case that the
; first answer produced by c-inf is enough to satisfy the query.
(define mplus
(lambda (c-inf f)
(case-inf c-inf
(() (f))
((f^) (inc (mplus (f) f^)))
((c) (choice c f))
((c f^) (choice c (inc (mplus (f) f^)))))))
; c-inf: SearchStream
; g: Goal
; -> SearchStream
(define bind
(lambda (c-inf g)
(case-inf c-inf
(() (mzero))
((f) (inc (bind (f) g)))
((c) (g c))
((c f) (mplus (g c) (inc (bind (f) g)))))))
; Int, SearchStream -> (ListOf SearchResult)
(define take
(lambda (n f)
(cond
((and n (zero? n)) '())
(else
(case-inf (f)
(() '())
((f) (take n f))
((c) (cons c '()))
((c f) (cons c
(take (and n (- n 1)) f))))))))
; -> SearchStream
(define-syntax bind*
(syntax-rules ()
((_ e) e)
((_ e g0 g ...) (bind* (bind e g0) g ...))))
; -> SearchStream
(define-syntax mplus*
(syntax-rules ()
((_ e) e)
((_ e0 e ...) (mplus e0
(inc (mplus* e ...))))))
; -> Goal
(define-syntax fresh
(syntax-rules ()
((_ (x ...) g0 g ...)
(lambdag@ (st)
; this inc triggers interleaving
(inc
(let ((scope (subst-scope (state-S st))))
(let ((x (var scope)) ...)
(bind* (g0 st) g ...))))))))
; -> Goal
(define-syntax conde
(syntax-rules ()
((_ (g0 g ...) (g1 g^ ...) ...)
(lambdag@ (st)
; this inc triggers interleaving
(inc
(let ((st (state-with-scope st (new-scope))))
(mplus*
(bind* (g0 st) g ...)
(bind* (g1 st) g^ ...) ...)))))))
(define-syntax run
(syntax-rules ()
((_ n (q) g0 g ...)
(take n
(inc
((fresh (q) g0 g ...
(lambdag@ (st)
(let ((st (state-with-scope st nonlocal-scope)))
(let ((z ((reify q) st)))
(choice z (lambda () (lambda () #f)))))))
empty-state))))
((_ n (q0 q1 q ...) g0 g ...)
(run n (x)
(fresh (q0 q1 q ...)
g0 g ...
(== `(,q0 ,q1 ,q ...) x))))))
(define-syntax run*
(syntax-rules ()
((_ (q0 q ...) g0 g ...) (run #f (q0 q ...) g0 g ...))))
; Constraints
; C refers to the constraint store map
; c refers to an individual constraint record
; Constraint: State -> #f | State
;
; (note that a Constraint is a Goal but a Goal is not a Constraint.
; Constraint implementations currently use this more restrained type.
; See `and-foldl` and `update-constraints`.)
; Requirements for type constraints:
; 1. Must be positive, not negative. not-pairo wouldn't work.
; 2. Each type must have infinitely many possible values to avoid
; incorrectness in combination with disequality constraints,
; like: (fresh (x) (booleano x) (=/= x #t) (=/= x #f))
(define type-constraint
(lambda (type-pred type-id)
(lambda (u)
(lambdag@ (st)
(let ((term (walk u (state-S st))))
(cond
((type-pred term) st)
((var? term)
(let* ((c (lookup-c term st))
(T (c-T c)))
(cond
((eq? T type-id) st)
((not T) (set-c term (c-with-T c type-id) st))
(else #f))))
(else #f)))))))
(define symbolo (type-constraint symbol? 'symbolo))
(define numbero (type-constraint number? 'numbero))
(define (add-to-D st v d)
(let* ((c (lookup-c v st))
(c^ (c-with-D c (cons d (c-D c)))))
(set-c v c^ st)))
(define =/=*
(lambda (S+)
(lambdag@ (st)
(let-values (((S added) (unify* S+ (subst-with-scope
(state-S st)
nonlocal-scope))))
(cond
((not S) st)
((null? added) #f)
(else
; Choose one of the disequality elements (el) to attach
; the constraint to. Only need to choose one because
; all must fail to cause the constraint to fail.
(let ((el (car added)))
(let ((st (add-to-D st (car el) added)))
(if (var? (cdr el))
(add-to-D st (cdr el) added)
st)))))))))
(define =/=
(lambda (u v)
(=/=* `((,u . ,v)))))
(define absento
(lambda (ground-atom term)
(unless (or (symbol? ground-atom)
(number? ground-atom)
(boolean? ground-atom)
(null? ground-atom))
(error 'absento "first argument to absento must be a ground atom"))
(lambdag@ (st)
(let ((term (walk term (state-S st))))
(cond
((pair? term)
(let ((st^ ((absento ground-atom (car term)) st)))
(and st^ ((absento ground-atom (cdr term)) st^))))
((eqv? term ground-atom) #f)
((var? term)
(let* ((c (lookup-c term st))
(A (c-A c)))
(if (memv ground-atom A)
st
(let ((c^ (c-with-A c (cons ground-atom A))))
(set-c term c^ st)))))
(else st))))))
; Fold lst with proc and initial value init. If proc ever returns #f,
; return with #f immediately. Used for applying a series of
; constraints to a state, failing if any operation fails.
(define (and-foldl proc init lst)
(if (null? lst)
init
(let ([res (proc (car lst) init)])
(and res (and-foldl proc res (cdr lst))))))
(define ==
(lambda (u v)
(lambdag@ (st)
(let-values (((S added) (unify u v (state-S st))))
(if S
(and-foldl update-constraints (state S (state-C st)) added)
#f)))))
; Not fully optimized. Could do absento update with fewer
; hash-refs / hash-sets.
(define update-constraints
(lambda (a st)
(let ([old-c (lookup-c (lhs a) st)])
(if (eq? old-c empty-c)
st
(let ((st (remove-c (lhs a) st)))
(and-foldl (lambda (op st) (op st)) st
(append
(if (eq? (c-T old-c) 'symbolo)
(list (symbolo (rhs a)))
'())
(if (eq? (c-T old-c) 'numbero)
(list (numbero (rhs a)))
'())
(map (lambda (atom) (absento atom (rhs a))) (c-A old-c))
(map (lambda (d) (=/=* d)) (c-D old-c)))))))))
; Reification
(define walk*
(lambda (v S)
(let ((v (walk v S)))
(cond
((var? v) v)
((pair? v)
(cons (walk* (car v) S) (walk* (cdr v) S)))
(else v)))))
(define vars
(lambda (term acc)
(cond
((var? term) (cons term acc))
((pair? term)
(vars (cdr term) (vars (car term) acc)))
(else acc))))
(define-syntax project
(syntax-rules ()
((_ (x ...) g g* ...)
(lambdag@ (st)
(let ((x (walk* x (state-S st))) ...)
((fresh () g g* ...) st))))))
; Create a constraint store of the old representation from a state
; object, so that we can use the old reifier. Only accumulates
; constraints related to the variable being reified which makes things
; a bit faster.
(define c-from-st
(lambda (st x)
(let ((vs (vars (walk* x (state-S st)) '())))
(foldl
(lambda (v c-store)
(let ((c (lookup-c v st)))
(let ((S (state-S st))
(D (c->D c-store))
(Y (c->Y c-store))
(N (c->N c-store))
(T (c->T c-store))
(T^ (c-T c))
(D^ (c-D c))
(A^ (c-A c)))
`(,S
,(append D^ D)
,(if (eq? T^ 'symbolo)
(cons v Y)
Y)
,(if (eq? T^ 'numbero)
(cons v N)
N)
,(append
(map (lambda (atom) (cons atom v)) A^)
T)))))
`(,(state-S st) () () () ())
(remove-duplicates vs)))))
(define reify
(lambda (x)
(lambda (st)
(let ((c (c-from-st st x)))
(let ((c (cycle c)))
(let* ((S (c->S c))
(D (walk* (c->D c) S))
(Y (walk* (c->Y c) S))
(N (walk* (c->N c) S))
(T (walk* (c->T c) S)))
(let ((v (walk* x S)))
(let ((R (reify-S v (subst empty-subst-map
nonlocal-scope))))
(reify+ v R
(let ((D (remp
(lambda (d)
(let ((dw (walk* d S)))
(anyvar? dw R)))
(rem-xx-from-d c))))
(rem-subsumed D))
(remp
(lambda (y) (var? (walk y R)))
Y)
(remp
(lambda (n) (var? (walk n R)))
N)
(remp (lambda (t)
(anyvar? t R)) T))))))))))
; Bits from the old constraint implementation, still used for
; reification.
; In this part of the code, c refers to the
; old constraint store with components:
; S - substitution
; D - disequality constraints
; Y - symbolo
; N - numbero
; T - absento
(define c->S (lambda (c) (car c)))
(define c->D (lambda (c) (cadr c)))
(define c->Y (lambda (c) (caddr c)))
(define c->N (lambda (c) (cadddr c)))
(define c->T (lambda (c) (cadddr (cdr c))))
; Syntax for reification goal objects using the old constraint store
(define-syntax lambdar@
(syntax-rules (:)
((_ (c) e) (lambda (c) e))
((_ (c : S D Y N T) e)
(lambda (c)
(let ((S (c->S c))
(D (c->D c))
(Y (c->Y c))
(N (c->N c))
(T (c->T c)))
e)))))
(define tagged?
(lambda (S Y y^)
(exists (lambda (y) (eqv? (walk y S) y^)) Y)))
(define untyped-var?
(lambda (S Y N t^)
(let ((in-type? (lambda (y) (var-eq? (walk y S) t^))))
(and (var? t^)
(not (exists in-type? Y))
(not (exists in-type? N))))))
(define reify-S
(lambda (v S)
(let ((v (walk v S)))
(cond
((var? v)
(let ((n (subst-length S)))
(let ((name (reify-name n)))
(subst-add S v name))))
((pair? v)
(let ((S (reify-S (car v) S)))
(reify-S (cdr v) S)))
(else S)))))
(define reify-name
(lambda (n)
(string->symbol
(string-append "_" "." (number->string n)))))
(define drop-dot
(lambda (X)
(map (lambda (t)
(let ((a (lhs t))
(d (rhs t)))
`(,a ,d)))
X)))
(define sorter
(lambda (ls)
(list-sort lex<=? ls)))
(define lex<=?
(lambda (x y)
(string<=? (datum->string x) (datum->string y))))
(define datum->string
(lambda (x)
(call-with-string-output-port
(lambda (p) (display x p)))))
(define anyvar?
(lambda (u r)
(cond
((pair? u)
(or (anyvar? (car u) r)
(anyvar? (cdr u) r)))
(else (var? (walk u r))))))
(define member*
(lambda (u v)
(cond
((equal? u v) #t)
((pair? v)
(or (member* u (car v)) (member* u (cdr v))))
(else #f))))
(define drop-N-b/c-const
(lambdar@ (c : S D Y N T)
(let ((const? (lambda (n)
(not (var? (walk n S))))))
(cond
((find const? N) =>
(lambda (n) `(,S ,D ,Y ,(remq1 n N) ,T)))
(else c)))))
(define drop-Y-b/c-const
(lambdar@ (c : S D Y N T)
(let ((const? (lambda (y)
(not (var? (walk y S))))))
(cond
((find const? Y) =>
(lambda (y) `(,S ,D ,(remq1 y Y) ,N ,T)))
(else c)))))
(define remq1
(lambda (elem ls)
(cond
((null? ls) '())
((eq? (car ls) elem) (cdr ls))
(else (cons (car ls) (remq1 elem (cdr ls)))))))
(define same-var?
(lambda (v)
(lambda (v^)
(and (var? v) (var? v^) (var-eq? v v^)))))
(define find-dup
(lambda (f S)
(lambda (set)
(let loop ((set^ set))
(cond
((null? set^) #f)
(else
(let ((elem (car set^)))
(let ((elem^ (walk elem S)))
(cond
((find (lambda (elem^^)
((f elem^) (walk elem^^ S)))
(cdr set^))
elem)
(else (loop (cdr set^))))))))))))
(define drop-N-b/c-dup-var
(lambdar@ (c : S D Y N T)
(cond
(((find-dup same-var? S) N) =>
(lambda (n) `(,S ,D ,Y ,(remq1 n N) ,T)))
(else c))))
(define drop-Y-b/c-dup-var
(lambdar@ (c : S D Y N T)
(cond
(((find-dup same-var? S) Y) =>
(lambda (y)
`(,S ,D ,(remq1 y Y) ,N ,T)))
(else c))))
(define var-type-mismatch?
(lambda (S Y N t1^ t2^)
(cond
((num? S N t1^) (not (num? S N t2^)))
((sym? S Y t1^) (not (sym? S Y t2^)))
(else #f))))
(define term-ununifiable?
(lambda (S Y N t1 t2)
(let ((t1^ (walk t1 S))
(t2^ (walk t2 S)))
(cond
((or (untyped-var? S Y N t1^) (untyped-var? S Y N t2^)) #f)
((var? t1^) (var-type-mismatch? S Y N t1^ t2^))
((var? t2^) (var-type-mismatch? S Y N t2^ t1^))
((and (pair? t1^) (pair? t2^))
(or (term-ununifiable? S Y N (car t1^) (car t2^))
(term-ununifiable? S Y N (cdr t1^) (cdr t2^))))
(else (not (eqv? t1^ t2^)))))))
(define T-term-ununifiable?
(lambda (S Y N)
(lambda (t1)
(let ((t1^ (walk t1 S)))
(letrec
((t2-check
(lambda (t2)
(let ((t2^ (walk t2 S)))
(if (pair? t2^)
(and
(term-ununifiable? S Y N t1^ t2^)
(t2-check (car t2^))
(t2-check (cdr t2^)))
(term-ununifiable? S Y N t1^ t2^))))))
t2-check)))))
(define num?
(lambda (S N n)
(let ((n (walk n S)))
(cond
((var? n) (tagged? S N n))
(else (number? n))))))
(define sym?
(lambda (S Y y)
(let ((y (walk y S)))
(cond
((var? y) (tagged? S Y y))
(else (symbol? y))))))
(define drop-T-b/c-Y-and-N
(lambdar@ (c : S D Y N T)
(let ((drop-t? (T-term-ununifiable? S Y N)))
(cond
((find (lambda (t) ((drop-t? (lhs t)) (rhs t))) T) =>
(lambda (t) `(,S ,D ,Y ,N ,(remq1 t T))))
(else c)))))
(define move-T-to-D-b/c-t2-atom
(lambdar@ (c : S D Y N T)
(cond
((exists (lambda (t)
(let ((t2^ (walk (rhs t) S)))
(cond
((and (not (untyped-var? S Y N t2^))
(not (pair? t2^)))
(let ((T (remq1 t T)))
`(,S ((,t) . ,D) ,Y ,N ,T)))
(else #f))))
T))
(else c))))
(define terms-pairwise=?
(lambda (pr-a^ pr-d^ t-a^ t-d^ S)
(or
(and (term=? pr-a^ t-a^ S)
(term=? pr-d^ t-a^ S))
(and (term=? pr-a^ t-d^ S)
(term=? pr-d^ t-a^ S)))))
(define T-superfluous-pr?
(lambda (S Y N T)
(lambda (pr)
(let ((pr-a^ (walk (lhs pr) S))
(pr-d^ (walk (rhs pr) S)))
(cond
((exists
(lambda (t)
(let ((t-a^ (walk (lhs t) S))
(t-d^ (walk (rhs t) S)))
(terms-pairwise=? pr-a^ pr-d^ t-a^ t-d^ S)))
T)
(for-all
(lambda (t)
(let ((t-a^ (walk (lhs t) S))
(t-d^ (walk (rhs t) S)))
(or
(not (terms-pairwise=? pr-a^ pr-d^ t-a^ t-d^ S))
(untyped-var? S Y N t-d^)
(pair? t-d^))))
T))
(else #f))))))
(define drop-from-D-b/c-T
(lambdar@ (c : S D Y N T)
(cond
((find
(lambda (d)
(exists
(T-superfluous-pr? S Y N T)
d))
D) =>
(lambda (d) `(,S ,(remq1 d D) ,Y ,N ,T)))
(else c))))
(define drop-t-b/c-t2-occurs-t1
(lambdar@ (c : S D Y N T)
(cond
((find (lambda (t)
(let ((t-a^ (walk (lhs t) S))
(t-d^ (walk (rhs t) S)))
(mem-check t-d^ t-a^ S)))
T) =>
(lambda (t)
`(,S ,D ,Y ,N ,(remq1 t T))))
(else c))))
(define split-t-move-to-d-b/c-pair
(lambdar@ (c : S D Y N T)
(cond
((exists
(lambda (t)
(let ((t2^ (walk (rhs t) S)))
(cond
((pair? t2^) (let ((ta `(,(lhs t) . ,(car t2^)))
(td `(,(lhs t) . ,(cdr t2^))))
(let ((T `(,ta ,td . ,(remq1 t T))))
`(,S ((,t) . ,D) ,Y ,N ,T))))
(else #f))))
T))
(else c))))
(define find-d-conflict
(lambda (S Y N)
(lambda (D)
(find
(lambda (d)
(exists (lambda (pr)
(term-ununifiable? S Y N (lhs pr) (rhs pr)))
d))
D))))
(define drop-D-b/c-Y-or-N
(lambdar@ (c : S D Y N T)
(cond
(((find-d-conflict S Y N) D) =>
(lambda (d) `(,S ,(remq1 d D) ,Y ,N ,T)))
(else c))))
(define cycle
(lambdar@ (c)
(let loop ((c^ c)
(fns^ (LOF))
(n (length (LOF))))
(cond
((zero? n) c^)
((null? fns^) (loop c^ (LOF) n))
(else
(let ((c^^ ((car fns^) c^)))
(cond
((not (eq? c^^ c^))
(loop c^^ (cdr fns^) (length (LOF))))
(else (loop c^ (cdr fns^) (sub1 n))))))))))
(define mem-check
(lambda (u t S)
(let ((t (walk t S)))
(cond
((pair? t)
(or (term=? u t S)
(mem-check u (car t) S)
(mem-check u (cdr t) S)))
(else (term=? u t S))))))
(define term=?
(lambda (u t S)
(let-values (((S added) (unify u t (subst-with-scope
S
nonlocal-scope))))
(and S (null? added)))))
(define ground-non-<type>?
(lambda (pred)
(lambda (u S)
(let ((u (walk u S)))
(cond
((var? u) #f)
(else (not (pred u))))))))
(define ground-non-symbol?
(ground-non-<type>? symbol?))
(define ground-non-number?
(ground-non-<type>? number?))
(define succeed (== #f #f))
(define fail (== #f #t))
(define ==fail-check
(lambda (S0 D Y N T)
(let ([S0 (subst-with-scope S0 nonlocal-scope)])
(cond
((atomic-fail-check S0 Y ground-non-symbol?) #t)
((atomic-fail-check S0 N ground-non-number?) #t)
((symbolo-numbero-fail-check S0 Y N) #t)
((=/=-fail-check S0 D) #t)
((absento-fail-check S0 T) #t)
(else #f)))))
(define atomic-fail-check
(lambda (S A pred)