forked from qobi/R6RS-AD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wwd.ss
2145 lines (2032 loc) · 73.6 KB
/
wwd.ss
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
#!r6rs
(library
(wwd)
(export *empty* grid-xy? grid-zy? below? below next? next previous? previous
grid-points empty? grid-point-contents=? create-grid max-x max-y max-z
at grid-binding lift-grid write-grid implies iff
assert-lincoln-log-grammar-constraints!
left-edge? right-edge? empty?-constraint left-edge?-constraint
right-edge?-constraint valid-thing? thing-xy? thing-zy?
thing-width thing-height thing-left-of? thing-below? thing-inside?
from-to-by things wall?-constraint window?-constraint door?-constraint
solid?-constraint
parse-type pretty-type parse-expression pretty-expression pretty-value
pretty-environment type free-variables equal-type? equal-value?
create-typed-meaning evaluate call *s* *n* *np* *d* *vp* *v* *v-np*
*lexicon* a-typed-apply a-meaning-constraint create-word-variable
create-type-variable make-upper-triangular-matrix
upper-triangular-matrix-ref constraints all-domain-variables
all-sentences)
(import (rnrs)
(QobiScheme)
(nondeterministic-scheme)
(rename (nondeterministic-constraints)
(binding nondeterministic-constraints:binding))
(nondeterministic-lifting))
;; x, y, and z are nonnegative integers
;; x and z are horizontal axes
;; y is vertical axis increasing upward
;; looking down along y increasing x is horizontal rightward axis
;; and increasing z is vertical upward axis
;; y=0 is the layer on the ground
;; even y are oriented along x
;; odd y are oriented along z
(define-record-type grid-point (fields x y z))
;; notches: 1, 2, or 3, 0<=notch<notches an integer
;; each grid point is occupied by either a grid-point-contents or #f indicating
;; empty
(define-record-type grid-point-contents (fields notches notch))
;; left, bottom, right, top, depth are nonnegative integers
(define-record-type thing (fields left bottom right top depth))
(define-record-type typed-meaning (fields type meaning))
(define-record-type arrow-type (fields argument result))
(define-record-type leftward-arrow-type (fields result argument))
(define-record-type rightward-arrow-type (fields argument result))
(define-record-type variable-access-expression (fields variable type))
(define-record-type lambda-expression
(fields variable expression variables types type))
(define-record-type llambda-expression
(fields variable expression variables types type))
(define-record-type rlambda-expression
(fields variable expression variables types type))
(define-record-type application (fields callee argument type))
(define-record-type and-expression (fields expressions))
(define-record-type or-expression (fields expressions))
(define-record-type not-expression (fields expression))
(define-record-type some-expression (fields expression))
(define-record-type every-expression (fields expression))
(define-record-type at-least-expression (fields k expression))
(define-record-type at-most-expression (fields k expression))
(define-record-type exactly-expression (fields k expression))
(define-record-type wall?-expression (fields expression))
(define-record-type window?-expression (fields expression))
(define-record-type door?-expression (fields expression))
(define-record-type solid?-expression (fields expression))
(define-record-type width=?-expression (fields expression k))
(define-record-type height=?-expression (fields expression k))
(define-record-type left-of?-expression (fields expression1 expression2))
(define-record-type below?-expression (fields expression1 expression2))
(define-record-type perpendicular?-expression (fields expression1 expression2))
(define-record-type inside?-expression (fields expression1 expression2))
(define-record-type binding (fields variable value))
(define-record-type closure (fields environment expression))
(define *empty* #f)
;; Grid points
(define (grid-xy? grid-point) (even? (grid-point-y grid-point)))
(define (grid-zy? grid-point) (odd? (grid-point-y grid-point)))
(define (below? grid-point) (>= (grid-point-y grid-point) 1))
(define (below grid-point)
(make-grid-point (grid-point-x grid-point)
(- (grid-point-y grid-point) 1)
(grid-point-z grid-point)))
(define (next? grid grid-point)
(cond ((grid-xy? grid-point) (< (grid-point-x grid-point) (- (max-x grid) 1)))
((grid-zy? grid-point) (< (grid-point-z grid-point) (- (max-z grid) 1)))
(else (fuck-up))))
(define (next grid-point)
(cond ((grid-xy? grid-point)
(make-grid-point (+ (grid-point-x grid-point) 1)
(grid-point-y grid-point)
(grid-point-z grid-point)))
((grid-zy? grid-point)
(make-grid-point (grid-point-x grid-point)
(grid-point-y grid-point)
(+ (grid-point-z grid-point) 1)))
(else (fuck-up))))
(define (previous? grid-point)
(cond ((grid-xy? grid-point) (positive? (grid-point-x grid-point)))
((grid-zy? grid-point) (positive? (grid-point-z grid-point)))
(else (fuck-up))))
(define (previous grid-point)
(cond ((grid-xy? grid-point)
(make-grid-point (- (grid-point-x grid-point) 1)
(grid-point-y grid-point)
(grid-point-z grid-point)))
((grid-zy? grid-point)
(make-grid-point (grid-point-x grid-point)
(grid-point-y grid-point)
(- (grid-point-z grid-point) 1)))
(else (fuck-up))))
(define (grid-points grid)
(map-reduce-n
append
'()
(lambda (x)
(map-reduce-n
append
'()
(lambda (y) (map-n (lambda (z) (make-grid-point x y z)) (max-z grid)))
(max-y grid)))
(max-x grid)))
;; Grid point contents
(define (empty? grid-point-contents) (eq? grid-point-contents *empty*))
(define (grid-point-contents=? grid-point-contents1 grid-point-contents2)
(or (and (empty? grid-point-contents1) (empty? grid-point-contents2))
(and (not (empty? grid-point-contents1))
(not (empty? grid-point-contents2))
(= (grid-point-contents-notches grid-point-contents1)
(grid-point-contents-notches grid-point-contents2))
(= (grid-point-contents-notch grid-point-contents1)
(grid-point-contents-notch grid-point-contents2)))))
;; The grid
(define (create-grid x y z)
(map-n-vector
(lambda (x)
(map-n-vector (lambda (y)
(map-n-vector (lambda (z)
(new-domain-variable
(list *empty*
(make-grid-point-contents 1 0)
(make-grid-point-contents 2 0)
(make-grid-point-contents 2 1)
(make-grid-point-contents 3 0)
(make-grid-point-contents 3 1)
(make-grid-point-contents 3 2))))
z))
y))
x))
(define (max-x grid) (vector-length grid))
(define (max-y grid) (vector-length (vector-ref grid 0)))
(define (max-z grid) (vector-length (vector-ref (vector-ref grid 0) 0)))
(define (at grid grid-point)
(vector-ref (vector-ref (vector-ref grid (grid-point-x grid-point))
(grid-point-y grid-point))
(grid-point-z grid-point)))
(define (grid-binding grid)
(map-n-vector
(lambda (x)
(map-n-vector
(lambda (y)
(map-n-vector
(lambda (z)
(nondeterministic-constraints:binding
(at grid (make-grid-point x y z))))
(max-z grid)))
(max-y grid)))
(max-x grid)))
(define (lift-grid grid)
(map-n-vector
(lambda (x)
(map-n-vector
(lambda (y)
(map-n-vector
(lambda (z)
(new-domain-variable (list (at grid (make-grid-point x y z)))))
(max-z grid)))
(max-y grid)))
(max-x grid)))
(define (write-grid grid pathname)
(call-with-output-file pathname
(lambda (port)
(for-each-n
(lambda (x)
(for-each-n
(lambda (y)
(for-each-n
(lambda (z)
(let ((grid-point-contents
(nondeterministic-constraints:binding
(at grid (make-grid-point x y z)))))
(unless (empty? grid-point-contents)
(write x port)
(display " " port)
(write y port)
(display " " port)
(write z port)
(display " " port)
(write (grid-point-contents-notches grid-point-contents) port)
(display " " port)
(write (grid-point-contents-notch grid-point-contents) port)
(newline port))))
(max-z grid)))
(max-y grid)))
(max-x grid)))))
;; The Grammar of Lincoln Logs
(define (implies p q) (or (not p) q))
(define (iff p q) (eq? p q))
(define (assert-lincoln-log-grammar-constraints! grid)
(for-each
(lambda (grid-point)
;; 2-notch logs occupy two adjacent grid points
;; 2, 3
(if (next? grid grid-point)
(assert-nondeterministic-constraint!
(lambda (Zp Znp)
(iff (grid-point-contents=? Zp (make-grid-point-contents 2 0))
(grid-point-contents=? Znp (make-grid-point-contents 2 1))))
(at grid grid-point)
(at grid (next grid-point)))
(assert-nondeterministic-constraint!
(lambda (Zp)
(not (grid-point-contents=? Zp (make-grid-point-contents 2 0))))
(at grid grid-point)))
(unless (previous? grid-point)
(assert-nondeterministic-constraint!
(lambda (Zp)
(not (grid-point-contents=? Zp (make-grid-point-contents 2 1))))
(at grid grid-point)))
;; 3-notch logs occupy three adjacent grid points
;; 4, 6
(if (next? grid grid-point)
(assert-nondeterministic-constraint!
(lambda (Zp Znp)
(iff (grid-point-contents=? Zp (make-grid-point-contents 3 0))
(grid-point-contents=? Znp (make-grid-point-contents 3 1))))
(at grid grid-point)
(at grid (next grid-point)))
(assert-nondeterministic-constraint!
(lambda (Zp)
(not (grid-point-contents=? Zp (make-grid-point-contents 3 0))))
(at grid grid-point)))
(unless (previous? grid-point)
(assert-nondeterministic-constraint!
(lambda (Zp)
(not (grid-point-contents=? Zp (make-grid-point-contents 3 1))))
(at grid grid-point)))
;; 5, 8
(if (and (next? grid grid-point) (next? grid (next grid-point)))
(assert-nondeterministic-constraint!
(lambda (Zp znnp)
(iff (grid-point-contents=? Zp (make-grid-point-contents 3 0))
(grid-point-contents=? znnp (make-grid-point-contents 3 2))))
(at grid grid-point)
(at grid (next (next grid-point))))
(assert-nondeterministic-constraint!
(lambda (Zp)
(not (grid-point-contents=? Zp (make-grid-point-contents 3 0))))
(at grid grid-point)))
(unless (and (previous? grid-point) (previous? (previous grid-point)))
(assert-nondeterministic-constraint!
(lambda (Zp)
(not (grid-point-contents=? Zp (make-grid-point-contents 3 2))))
(at grid grid-point)))
;; 7, 9
(if (next? grid grid-point)
(assert-nondeterministic-constraint!
(lambda (Zp Znp)
(iff (grid-point-contents=? Zp (make-grid-point-contents 3 1))
(grid-point-contents=? Znp (make-grid-point-contents 3 2))))
(at grid grid-point)
(at grid (next grid-point)))
(assert-nondeterministic-constraint!
(lambda (Zp)
(not (grid-point-contents=? Zp (make-grid-point-contents 3 1))))
(at grid grid-point)))
;; 1- and 2-notch logs must be supported at all notches
;; 10
(when (below? grid-point)
(assert-nondeterministic-constraint!
(lambda (Zp Zbp)
(implies (or (grid-point-contents=? Zp (make-grid-point-contents 1 0))
(grid-point-contents=? Zp (make-grid-point-contents 2 0))
(grid-point-contents=? Zp (make-grid-point-contents 2 1)))
(not (empty? Zbp))))
(at grid grid-point)
(at grid (below grid-point))))
;; 3-notch logs must be supported at at least 2 out of 3 notches
;; 11
(when (and (below? grid-point)
(next? grid grid-point)
(below? (next grid-point))
(next? grid (next grid-point))
(next? grid (next grid-point))
(below? (next (next grid-point))))
(assert-nondeterministic-constraint!
(lambda (Zp Zbp Zbnp Zbnnp)
(implies (grid-point-contents=? Zp (make-grid-point-contents 3 0))
(or (and (not (empty? Zbp)) (not (empty? Zbnp)))
(and (not (empty? Zbp)) (not (empty? Zbnnp)))
(and (not (empty? Zbnp)) (not (empty? Zbnnp))))))
(at grid grid-point)
(at grid (below grid-point))
(at grid (below (next grid-point)))
(at grid (below (next (next grid-point))))))
;; 12
(when (and (below? grid-point)
(next? grid grid-point)
(below? (next grid-point))
(previous? grid-point)
(below? (previous grid-point)))
(assert-nondeterministic-constraint!
(lambda (Zp Zbp Zbnp Zbpp)
(implies (grid-point-contents=? Zp (make-grid-point-contents 3 1))
(or (and (not (empty? Zbp)) (not (empty? Zbnp)))
(and (not (empty? Zbp)) (not (empty? Zbpp)))
(and (not (empty? Zbnp)) (not (empty? Zbpp))))))
(at grid grid-point)
(at grid (below grid-point))
(at grid (below (next grid-point)))
(at grid (below (previous grid-point)))))
;; 13
(when (and (below? grid-point)
(previous? grid-point)
(below? (previous grid-point))
(previous? (previous grid-point))
(previous? (previous grid-point))
(below? (previous (previous grid-point))))
(assert-nondeterministic-constraint!
(lambda (Zp Zbp Zbpp Zbppp)
(implies (grid-point-contents=? Zp (make-grid-point-contents 3 2))
(or (and (not (empty? Zbp)) (not (empty? Zbpp)))
(and (not (empty? Zbp)) (not (empty? Zbppp)))
(and (not (empty? Zbpp)) (not (empty? Zbppp))))))
(at grid grid-point)
(at grid (below grid-point))
(at grid (below (previous grid-point)))
(at grid (below (previous (previous grid-point)))))))
(grid-points grid)))
;; Linguistic Constraints
(define (left-edge? grid-point-contents)
(and (not (empty? grid-point-contents))
(zero? (grid-point-contents-notch grid-point-contents))))
(define (right-edge? grid-point-contents)
(and (not (empty? grid-point-contents))
(= (grid-point-contents-notch grid-point-contents)
(- (grid-point-contents-notches grid-point-contents) 1))))
(define (empty?-constraint grid-point-contents-variable)
(let ((boolean-variable (create-boolean-variable)))
(assert-nondeterministic-constraint!
(lambda (boolean grid-point-contents)
(eq? boolean (empty? grid-point-contents)))
boolean-variable
grid-point-contents-variable)
boolean-variable))
(define (left-edge?-constraint grid-point-contents-variable)
(let ((boolean-variable (create-boolean-variable)))
(assert-nondeterministic-constraint!
(lambda (boolean grid-point-contents)
(eq? boolean (left-edge? grid-point-contents)))
boolean-variable
grid-point-contents-variable)
boolean-variable))
(define (right-edge?-constraint grid-point-contents-variable)
(let ((boolean-variable (create-boolean-variable)))
(assert-nondeterministic-constraint!
(lambda (boolean grid-point-contents)
(eq? boolean (right-edge? grid-point-contents)))
boolean-variable
grid-point-contents-variable)
boolean-variable))
(define (valid-thing? thing)
(and
;; Since this is < instead of <=, a tower of 1-notch logs doesn't count as
;; a wall.
(< (thing-left thing) (thing-right thing))
;; If this would be < instead of <= then couldn't have 1-log-high walls,
;; windows, and doors.
(<= (thing-bottom thing) (thing-top thing))
(or (and (even? (thing-bottom thing)) (even? (thing-top thing)))
(and (odd? (thing-bottom thing)) (odd? (thing-top thing))))))
(define (thing-xy? thing) (even? (thing-bottom thing)))
(define (thing-zy? thing) (odd? (thing-bottom thing)))
;; Because there is no +1, a wall made of 2-notch logs has width 1. And a wall
;; of width 1 can contain a window or door of width 1.
(define (thing-width thing) (- (thing-right thing) (thing-left thing)))
;; The +1 is so that 1-log-high walls, windows, and doors have a height of 1.
(define (thing-height thing)
(+ (/ (- (thing-top thing) (thing-bottom thing)) 2) 1))
(define (thing-left-of? thing1 thing2)
;; This doesn't constrain the vertical relation.
;; It doesn't allow horizontal overlap.
;; It does allow adjacency.
(and (eq? (thing-xy? thing1) (thing-xy? thing2))
(= (thing-depth thing1) (thing-depth thing2))
(<= (thing-right thing1) (thing-left thing2))))
(define (thing-below? thing1 thing2)
;; This doesn't constrain the horizontal relation.
;; It doesn't allow vertical overlap.
;; It doesn't allow adjacency.
(and (eq? (thing-xy? thing1) (thing-xy? thing2))
(= (thing-depth thing1) (thing-depth thing2))
(< (thing-top thing1) (thing-bottom thing2))))
(define (thing-perpendicular? thing1 thing2)
(not (eq? (thing-xy? thing1) (thing-xy? thing2))))
(define (thing-inside? thing1 thing2)
(and (>= (thing-right thing1) (thing-right thing2))
(<= (thing-left thing1) (thing-left thing2))
(>= (thing-bottom thing1) (thing-bottom thing2))
(<= (thing-top thing1) (thing-top thing2))
(= (thing-depth thing1) (thing-depth thing2))))
(define (from-to-by i j k)
(if ((if (negative? k) < >) i j) '() (cons i (from-to-by (+ i k) j k))))
(define (things grid)
(append
(map-reduce-n
append
'()
(lambda (bottom)
(map-reduce-n
append
'()
(lambda (top)
(map-reduce-n
append
'()
(lambda (left)
(map-reduce-n
append
'()
(lambda (right)
(map-reduce-n
append
'()
(lambda (depth)
(let ((thing (make-thing left bottom right top depth)))
(if (and (thing-xy? thing) (valid-thing? thing))
(list thing)
'())))
(max-z grid)))
(max-x grid)))
(max-x grid)))
(max-y grid)))
(max-y grid))
(map-reduce-n
append
'()
(lambda (bottom)
(map-reduce-n
append
'()
(lambda (top)
(map-reduce-n
append
'()
(lambda (left)
(map-reduce-n
append
'()
(lambda (right)
(map-reduce-n
append
'()
(lambda (depth)
(let ((thing (make-thing left bottom right top depth)))
(if (and (thing-zy? thing) (valid-thing? thing))
(list thing)
'())))
(max-x grid)))
(max-z grid)))
(max-z grid)))
(max-y grid)))
(max-y grid))))
(define (wall?-constraint grid thing)
;; A wall has
;; - its bottom along the ground
;; there is no constraint as to the bottom being populated
;; - its left and right edges fully populated with outward log ends
;; they must be fully populated and they must be outward log ends
;; - all grid points along its top edge populated
;; - no grid point along its top edge (except possibly the rightmost)
;; populated with a right edge
;; - no grid point along its top edge (except possibly the leftmost)
;; populated with a left edge
;; - no grid points on the row above its top edge populated
;; A wall need not be fully populated; it can have holes
(or-constraint
(if
;; - its bottom along the ground
;; there is no constraint as to the bottom being populated
(zero? (thing-bottom thing))
(and-constraint
;; - its left and right edges fully populated with outward log ends
;; they must be fully populated and they must be outward log ends
(every-constraint
(lambda (y)
(and-constraint
(left-edge?-constraint
(at grid
(make-grid-point (thing-left thing) y (thing-depth thing))))
(right-edge?-constraint
(at grid
(make-grid-point (thing-right thing) y (thing-depth thing))))))
(from-to-by (thing-bottom thing) (thing-top thing) 2))
;; - all grid points along its top edge populated
(every-constraint
(lambda (x)
(not-constraint
(empty?-constraint
(at grid
(make-grid-point x (thing-top thing) (thing-depth thing))))))
(from-to-by (thing-left thing) (thing-right thing) 1))
;; - no grid point along its top edge (except possibly the rightmost)
;; populated with a right edge
(every-constraint
(lambda (x)
(not-constraint
(right-edge?-constraint
(at grid
(make-grid-point x (thing-top thing) (thing-depth thing))))))
(from-to-by (thing-left thing) (- (thing-right thing) 1) 1))
;; - no grid point along its top edge (except possibly the leftmost)
;; populated with a left edge
(every-constraint
(lambda (x)
(not-constraint
(left-edge?-constraint
(at grid
(make-grid-point x (thing-top thing) (thing-depth thing))))))
(from-to-by (+ (thing-left thing) 1) (thing-right thing) 1))
;; - no grid points on the row above its top edge populated
(if (< (+ (thing-top thing) 2) (max-y grid))
(every-constraint
(lambda (x)
(empty?-constraint
(at grid
(make-grid-point
x (+ (thing-top thing) 2) (thing-depth thing)))))
(from-to-by (thing-left thing) (thing-right thing) 1))
(true-domain-variable)))
(false-domain-variable))
(if
;; - its bottom along the ground
;; there is no constraint as to the bottom being populated
(= (thing-bottom thing) 1)
(and-constraint
;; - its left and right edges fully populated with outward log ends
;; they must be fully populated and they must be outward log ends
(every-constraint
(lambda (y)
(and-constraint
(left-edge?-constraint
(at grid
(make-grid-point (thing-depth thing) y (thing-left thing))))
(right-edge?-constraint
(at grid
(make-grid-point (thing-depth thing) y (thing-right thing))))))
(from-to-by (thing-bottom thing) (thing-top thing) 2))
;; - all grid points along its top edge populated
(every-constraint
(lambda (z)
(not-constraint
(empty?-constraint
(at grid
(make-grid-point (thing-depth thing) (thing-top thing) z)))))
(from-to-by (thing-left thing) (thing-right thing) 1))
;; - no grid point along its top edge (except possibly the rightmost)
;; populated with a right edge
(every-constraint
(lambda (z)
(not-constraint
(right-edge?-constraint
(at grid
(make-grid-point (thing-depth thing) (thing-top thing) z)))))
(from-to-by (thing-left thing) (- (thing-right thing) 1) 1))
;; - no grid point along its top edge (except possibly the leftmost)
;; populated with a left edge
(every-constraint
(lambda (z)
(not-constraint
(left-edge?-constraint
(at grid
(make-grid-point (thing-depth thing) (thing-top thing) z)))))
(from-to-by (+ (thing-left thing) 1) (thing-right thing) 1))
;; - no grid points on the row above its top edge populated
(if (< (+ (thing-top thing) 2) (max-y grid))
(every-constraint
(lambda (z)
(empty?-constraint
(at grid
(make-grid-point
(thing-depth thing) (+ (thing-top thing) 2) z))))
(from-to-by (thing-left thing) (thing-right thing) 1))
(true-domain-variable)))
(false-domain-variable))))
(define (window?-constraint grid thing)
;; A window has
;; - its left and right edges fully populated with inward log ends
;; they must be fully populated and they must be inward log ends
;; - no grid points between left+1 and right-1 and top and bottom populated
;; - all grid points on the row above its top edge populated
;; - no grid point on the row above its top edge (except possibly the
;; rightmost) populated with a right edge
;; - no grid point on the row above its top edge (except possibly the
;; leftmost) populated with a left edge
;; - all grid points on the row below its bottom edge populated
;; - no grid point on the row below its bottom edge (except possibly the
;; rightmost) populated with a right edge
;; - no grid point on the row below its bottom edge (except possibly the
;; leftmost) populated with a left edge
(or-constraint
(if (and (thing-xy? thing)
(>= (- (thing-bottom thing) 2) 0)
(< (+ (thing-top thing) 2) (max-y grid)))
(and-constraint
;; - its left and right edges fully populated with inward log ends
;; they must be fully populated and they must be inward log ends
(every-constraint
(lambda (y)
(and-constraint
(right-edge?-constraint
(at grid
(make-grid-point (thing-left thing) y (thing-depth thing))))
(left-edge?-constraint
(at grid
(make-grid-point (thing-right thing) y (thing-depth thing))))))
(from-to-by (thing-bottom thing) (thing-top thing) 2))
;; - no grid points between left+1 and right-1 and top and bottom
;; populated
(every-constraint
(lambda (x)
(every-constraint
(lambda (y)
(empty?-constraint
(at grid (make-grid-point x y (thing-depth thing)))))
(from-to-by (thing-bottom thing) (thing-top thing) 2)))
(from-to-by (+ (thing-left thing) 1) (- (thing-right thing) 1) 1))
;; - all grid points on the row above its top edge populated
(every-constraint
(lambda (x)
(not-constraint
(empty?-constraint
(at grid
(make-grid-point
x (+ (thing-top thing) 2) (thing-depth thing))))))
(from-to-by (thing-left thing) (thing-right thing) 1))
;; - no grid point on the row above its top edge (except possibly the
;; rightmost) populated with a right edge
(every-constraint
(lambda (x)
(not-constraint
(right-edge?-constraint
(at grid
(make-grid-point
x (+ (thing-top thing) 2) (thing-depth thing))))))
(from-to-by (thing-left thing) (- (thing-right thing) 1) 1))
;; - no grid point on the row above its top edge (except possibly the
;; leftmost) populated with a left edge
(every-constraint
(lambda (x)
(not-constraint
(left-edge?-constraint
(at grid
(make-grid-point
x (+ (thing-top thing) 2) (thing-depth thing))))))
(from-to-by (+ (thing-left thing) 1) (thing-right thing) 1))
;; - all grid points on the row below its bottom edge populated
(every-constraint
(lambda (x)
(not-constraint
(empty?-constraint
(at grid
(make-grid-point
x (- (thing-bottom thing) 2) (thing-depth thing))))))
(from-to-by (thing-left thing) (thing-right thing) 1))
;; - no grid point on the row below its bottom edge (except possibly the
;; rightmost) populated with a right edge
(every-constraint
(lambda (x)
(not-constraint
(right-edge?-constraint
(at grid
(make-grid-point
x (- (thing-bottom thing) 2) (thing-depth thing))))))
(from-to-by (thing-left thing) (- (thing-right thing) 1) 1))
;; - no grid point on the row below its bottom edge (except possibly the
;; leftmost) populated with a left edge
(every-constraint
(lambda (x)
(not-constraint
(left-edge?-constraint
(at grid
(make-grid-point
x (- (thing-bottom thing) 2) (thing-depth thing))))))
(from-to-by (+ (thing-left thing) 1) (thing-right thing) 1)))
(false-domain-variable))
(if (and (thing-zy? thing)
(>= (- (thing-bottom thing) 2) 0)
(< (+ (thing-top thing) 2) (max-y grid)))
(and-constraint
;; - its left and right edges fully populated with inward log ends
;; they must be fully populated and they must be inward log ends
(every-constraint
(lambda (y)
(and-constraint
(right-edge?-constraint
(at grid
(make-grid-point (thing-depth thing) y (thing-left thing))))
(left-edge?-constraint
(at grid
(make-grid-point (thing-depth thing) y (thing-right thing))))))
(from-to-by (thing-bottom thing) (thing-top thing) 2))
;; - no grid points between left+1 and right-1 and top and bottom
;; populated
(every-constraint
(lambda (z)
(every-constraint
(lambda (y)
(empty?-constraint
(at grid (make-grid-point (thing-depth thing) y z))))
(from-to-by (thing-bottom thing) (thing-top thing) 2)))
(from-to-by (+ (thing-left thing) 1) (- (thing-right thing) 1) 1))
;; - all grid points on the row above its top edge populated
(every-constraint
(lambda (z)
(not-constraint
(empty?-constraint
(at grid
(make-grid-point
(thing-depth thing) (+ (thing-top thing) 2) z)))))
(from-to-by (thing-left thing) (thing-right thing) 1))
;; - no grid point on the row above its top edge (except possibly the
;; rightmost) populated with a right edge
(every-constraint
(lambda (z)
(not-constraint
(right-edge?-constraint
(at grid
(make-grid-point
(thing-depth thing) (+ (thing-top thing) 2) z)))))
(from-to-by (thing-left thing) (- (thing-right thing) 1) 1))
;; - no grid point on the row above its top edge (except possibly the
;; leftmost) populated with a left edge
(every-constraint
(lambda (z)
(not-constraint
(left-edge?-constraint
(at grid
(make-grid-point
(thing-depth thing) (+ (thing-top thing) 2) z)))))
(from-to-by (+ (thing-left thing) 1) (thing-right thing) 1))
;; - all grid points on the row below its bottom edge populated
(every-constraint
(lambda (z)
(not-constraint
(empty?-constraint
(at grid
(make-grid-point
(thing-depth thing) (- (thing-bottom thing) 2) z)))))
(from-to-by (thing-left thing) (thing-right thing) 1))
;; - no grid point on the row below its bottom edge (except possibly the
;; rightmost) populated with a right edge
(every-constraint
(lambda (z)
(not-constraint
(right-edge?-constraint
(at grid
(make-grid-point
(thing-depth thing) (- (thing-bottom thing) 2) z)))))
(from-to-by (thing-left thing) (- (thing-right thing) 1) 1))
;; - no grid point on the row below its bottom edge (except possibly the
;; leftmost) populated with a left edge
(every-constraint
(lambda (z)
(not-constraint
(left-edge?-constraint
(at grid
(make-grid-point
(thing-depth thing) (- (thing-bottom thing) 2) z)))))
(from-to-by (+ (thing-left thing) 1) (thing-right thing) 1)))
(false-domain-variable))))
(define (door?-constraint grid thing)
;; A door has
;; - its bottom along the ground
;; - its left and right edges fully populated with inward log ends
;; they must be fully populated and they must be inward log ends
;; - no grid points between left+1 and right-1 and top and bottom populated
;; - all grid points on the row above its top edge populated
;; - no grid point on the row above its top edge (except possibly the
;; rightmost) populated with a right edge
;; - no grid point on the row above its top edge (except possibly the
;; leftmost) populated with a left edge
(or-constraint
(if (and
;; - its bottom along the ground
(zero? (thing-bottom thing))
(< (+ (thing-top thing) 2) (max-y grid)))
(and-constraint
;; - its left and right edges fully populated with inward log ends
;; they must be fully populated and they must be inward log ends
(every-constraint
(lambda (y)
(and-constraint
(right-edge?-constraint
(at grid
(make-grid-point (thing-left thing) y (thing-depth thing))))
(left-edge?-constraint
(at grid
(make-grid-point (thing-right thing) y (thing-depth thing))))))
(from-to-by (thing-bottom thing) (thing-top thing) 2))
;; - no grid points between left+1 and right-1 and top and bottom
;; populated
(every-constraint
(lambda (x)
(every-constraint
(lambda (y)
(empty?-constraint
(at grid (make-grid-point x y (thing-depth thing)))))
(from-to-by (thing-bottom thing) (thing-top thing) 2)))
(from-to-by (+ (thing-left thing) 1) (- (thing-right thing) 1) 1))
;; - all grid points on the row above its top edge populated
(every-constraint
(lambda (x)
(not-constraint
(empty?-constraint
(at grid
(make-grid-point
x (+ (thing-top thing) 2) (thing-depth thing))))))
(from-to-by (thing-left thing) (thing-right thing) 1))
;; - no grid point on the row above its top edge (except possibly the
;; rightmost) populated with a right edge
(every-constraint
(lambda (x)
(not-constraint
(right-edge?-constraint
(at grid
(make-grid-point
x (+ (thing-top thing) 2) (thing-depth thing))))))
(from-to-by (thing-left thing) (- (thing-right thing) 1) 1))
;; - no grid point on the row above its top edge (except possibly the
;; leftmost) populated with a left edge
(every-constraint
(lambda (x)
(not-constraint
(left-edge?-constraint
(at grid
(make-grid-point
x (+ (thing-top thing) 2) (thing-depth thing))))))
(from-to-by (+ (thing-left thing) 1) (thing-right thing) 1)))
(false-domain-variable))
(if (and
;; - its bottom along the ground
(= (thing-bottom thing) 1)
(< (+ (thing-top thing) 2) (max-y grid)))
(and-constraint
;; - its left and right edges fully populated with inward log ends
;; they must be fully populated and they must be inward log ends
(every-constraint
(lambda (y)
(and-constraint
(right-edge?-constraint
(at grid
(make-grid-point (thing-depth thing) y (thing-left thing))))
(left-edge?-constraint
(at grid
(make-grid-point (thing-depth thing) y (thing-right thing))))))
(from-to-by (thing-bottom thing) (thing-top thing) 2))
;; - no grid points between left+1 and right-1 and top and bottom
;; populated
(every-constraint
(lambda (z)
(every-constraint
(lambda (y)
(empty?-constraint
(at grid (make-grid-point (thing-depth thing) y z))))
(from-to-by (thing-bottom thing) (thing-top thing) 2)))
(from-to-by (+ (thing-left thing) 1) (- (thing-right thing) 1) 1))
;; - all grid points on the row above its top edge populated
(every-constraint
(lambda (z)
(not-constraint
(empty?-constraint
(at grid
(make-grid-point
(thing-depth thing) (+ (thing-top thing) 2) z)))))
(from-to-by (thing-left thing) (thing-right thing) 1))
;; - no grid point on the row above its top edge (except possibly the
;; rightmost) populated with a right edge
(every-constraint
(lambda (z)
(not-constraint
(right-edge?-constraint
(at grid
(make-grid-point
(thing-depth thing) (+ (thing-top thing) 2) z)))))
(from-to-by (thing-left thing) (- (thing-right thing) 1) 1))
;; - no grid point on the row above its top edge (except possibly the
;; leftmost) populated with a left edge
(every-constraint
(lambda (z)
(not-constraint
(left-edge?-constraint
(at grid
(make-grid-point
(thing-depth thing) (+ (thing-top thing) 2) z)))))
(from-to-by (+ (thing-left thing) 1) (thing-right thing) 1)))
(false-domain-variable))))
(define (solid?-constraint grid thing)
;; A solid thing has
;; - every grid point between left and right and top and bottom populated
;; - no grid point between left and right-1 and top and bottom populated
;; with a right-edge
;; - no grid point between left+1 and right and top and bottom populated
;; with a left-edge
(or-constraint
(if (thing-xy? thing)
(and-constraint
;; - every grid point between left and right and top and bottom
;; populated
(every-constraint
(lambda (x)