-
Notifications
You must be signed in to change notification settings - Fork 6
/
games7.ss
1788 lines (1674 loc) · 63.4 KB
/
games7.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
(games7)
(export make-state parse-type pretty-type parse-expression pretty-expression
pretty-value pretty-environment
type free-variables equal-type? equal-value?
*s* *n* *np* *d*
*pp/of/np* *pp/for/np* *pp/in/np* *pp/from/np* *pp/to/np*
*p/in-np* *p/for-np* *p/of-np* *p/from-np* *p/to-np*
*n-pp/of/np* *n-pp/for/np* *n-pp/in/np*
*a* *a-pp/for/np* *a-pp/for/np-pp/of/np*
*vp* *pp/by/vp* *p-vp* *pp/when/s* *p-s* *v* *v-pp/by/vp*
*v-pp/when/s* *v-np* *v-np-pp/from/np-pp/to/np* *coord-vp* *coord-s*
*lexicon* board-ref cache-ref vector-replace matrix-replace
board-replace cache-replace turn evaluate
call a-type a-lambda-expression-of-type a-value-of-type
a-phrase-of-type a-phrase-of-value a-bottom-up-parse parse-length
a-top-down-parse things a-typed-apply possibly-true?
*tic-tac-toe* *hexapawn*)
(import (rnrs) (QobiScheme) (nondeterministic-scheme))
(define-record-type typed-meaning (fields type meaning))
(define-record-type stack-entry (fields type parse))
(define-record-type result (fields parse words))
(define-record-type state (fields board cache-for-x cache-for-o player))
(define-record-type player (fields player))
(define-record-type piece (fields player))
(define-record-type board-square (fields row column))
(define-record-type row (fields row))
(define-record-type column (fields column))
(define-record-type diagonal (fields slope))
(define-record-type cache-square (fields player i))
(define-record-type cache (fields player))
(define-record-type move (fields))
(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 application (fields callee argument type))
(define-record-type and-expression (fields expressions))
(define-record-type or-expression (fields expressions))
(define-record-type a-expression (fields expression1 expression2))
(define-record-type some-expression (fields expression1 expression2))
(define-record-type every-expression (fields expression1 expression2))
(define-record-type that-expression (fields expression1 expression2))
(define-record-type no-expression (fields expression1 expression2))
(define-record-type the-expression (fields expression1 expression2))
(define-record-type player?-expression (fields expression))
(define-record-type row?-expression (fields expression))
(define-record-type column?-expression (fields expression))
(define-record-type diagonal?-expression (fields expression))
(define-record-type move?-expression (fields expression))
(define-record-type opponent?-expression (fields expression1 expression2))
(define-record-type cache?-expression (fields expression1 expression2))
(define-record-type square?-expression (fields expression1 expression2))
(define-record-type piece?-expression (fields expression1 expression2))
(define-record-type distant?-expression (fields expression1 expression2))
(define-record-type close?-expression (fields expression1 expression2))
(define-record-type forward-adjacent?-expression
(fields expression1 expression2 expression3))
(define-record-type forward-diagonal?-expression
(fields expression1 expression2 expression3))
(define-record-type empty?-expression (fields expression))
(define-record-type has?-expression (fields expression1 expression2))
(define-record-type binding (fields variable value))
(define-record-type closure (fields environment expression))
(define (parse-type type)
(cond ((eq? type 'boolean) 'boolean)
((eq? type 'thing) 'thing)
((eq? type 'pp/of/np) 'pp/of/np)
((eq? type 'pp/for/np) 'pp/for/np)
((eq? type 'pp/in/np) 'pp/in/np)
((eq? type 'pp/from/np) 'pp/from/np)
((eq? type 'pp/to/np) 'pp/to/np)
((eq? type 'pp/by/vp) 'pp/by/vp)
((eq? type 'pp/when/s) 'pp/when/s)
((and (list? type) (= (length type) 3) (eq? (first type) '<-))
(make-leftward-arrow-type
(parse-type (second type)) (parse-type (third type))))
((and (list? type) (= (length type) 3) (eq? (first type) '->))
(make-rightward-arrow-type
(parse-type (second type)) (parse-type (third type))))
(else (error #f "Bad type"))))
(define (pretty-type type)
(cond ((equal-type? type 'boolean) 'boolean)
((equal-type? type 'thing) 'thing)
((equal-type? type 'pp/of/np) 'pp/of/np)
((equal-type? type 'pp/for/np) 'pp/for/np)
((equal-type? type 'pp/in/np) 'pp/in/np)
((equal-type? type 'pp/from/np) 'pp/from/np)
((equal-type? type 'pp/to/np) 'pp/to/np)
((equal-type? type 'pp/by/vp) 'pp/by/vp)
((equal-type? type 'pp/when/s) 'pp/when/s)
((leftward-arrow-type? type)
`(<- ,(pretty-type (leftward-arrow-type-result type))
,(pretty-type (leftward-arrow-type-argument type))))
((rightward-arrow-type? type)
`(-> ,(pretty-type (rightward-arrow-type-argument type))
,(pretty-type (rightward-arrow-type-result type))))
(else (fuck-up))))
(define (parse-expression expression type)
(let loop ((expression expression) (type type) (variables '()) (types '()))
(cond
((memq expression variables)
(make-variable-access-expression
expression (list-ref types (positionq expression variables))))
((and (list? expression)
(= (length expression) 3)
(eq? (first expression) 'lambda)
(list? (second expression))
(= (length (second expression)) 1)
(symbol? (first (second expression))))
(unless (or (leftward-arrow-type? type) (rightward-arrow-type? type))
(error #f "lambda expression not of function type"))
(let* ((variable (first (second expression)))
(expression (loop (third expression)
(if (leftward-arrow-type? type)
(leftward-arrow-type-result type)
(rightward-arrow-type-result type))
(cons (first (second expression)) variables)
(cons (if (leftward-arrow-type? type)
(leftward-arrow-type-argument type)
(rightward-arrow-type-argument type))
types)))
(free-variables (removeq variable (free-variables expression))))
(make-lambda-expression
variable
expression
free-variables
(map (lambda (variable) (list-ref types (positionq variable variables)))
free-variables)
type)))
((and (list? expression)
(>= (length expression) 1)
(eq? (first expression) 'and))
(unless (equal-type? type 'boolean)
(error #f "and expression not of type boolean"))
(make-and-expression
(map (lambda (expression) (loop expression 'boolean variables types))
(rest expression))))
((and (list? expression)
(>= (length expression) 1)
(eq? (first expression) 'or))
(unless (equal-type? type 'boolean)
(error #f "or expression not of type boolean"))
(make-or-expression
(map (lambda (expression) (loop expression 'boolean variables types))
(rest expression))))
((and (list? expression)
(= (length expression) 3)
(eq? (first expression) 'a))
(unless (equal-type? type 'boolean)
(error #f "a expression not of type boolean"))
(make-a-expression (loop (second expression)
(make-rightward-arrow-type 'thing 'boolean)
variables
types)
(loop (third expression)
(make-rightward-arrow-type 'thing 'boolean)
variables
types)))
((and (list? expression)
(= (length expression) 3)
(eq? (first expression) 'some))
(unless (equal-type? type 'boolean)
(error #f "some expression not of type boolean"))
(make-some-expression (loop (second expression)
(make-rightward-arrow-type 'thing 'boolean)
variables
types)
(loop (third expression)
(make-rightward-arrow-type 'thing 'boolean)
variables
types)))
((and (list? expression)
(= (length expression) 3)
(eq? (first expression) 'every))
(unless (equal-type? type 'boolean)
(error #f "every expression not of type boolean"))
(make-every-expression (loop (second expression)
(make-rightward-arrow-type 'thing 'boolean)
variables
types)
(loop (third expression)
(make-rightward-arrow-type 'thing 'boolean)
variables
types)))
((and (list? expression)
(= (length expression) 3)
(eq? (first expression) 'that))
(unless (equal-type? type 'boolean)
(error #f "that expression not of type boolean"))
(make-that-expression (loop (second expression)
(make-rightward-arrow-type 'thing 'boolean)
variables
types)
(loop (third expression)
(make-rightward-arrow-type 'thing 'boolean)
variables
types)))
((and (list? expression)
(= (length expression) 3)
(eq? (first expression) 'no))
(unless (equal-type? type 'boolean)
(error #f "no expression not of type boolean"))
(make-no-expression (loop (second expression)
(make-rightward-arrow-type 'thing 'boolean)
variables
types)
(loop (third expression)
(make-rightward-arrow-type 'thing 'boolean)
variables
types)))
((and (list? expression)
(= (length expression) 3)
(eq? (first expression) 'the))
(unless (equal-type? type 'boolean)
(error #f "the expression not of type boolean"))
(make-the-expression (loop (second expression)
(make-rightward-arrow-type 'thing 'boolean)
variables
types)
(loop (third expression)
(make-rightward-arrow-type 'thing 'boolean)
variables
types)))
((and (list? expression)
(= (length expression) 2)
(eq? (first expression) 'player?))
(unless (equal-type? type 'boolean)
(error #f "player? expression not of type boolean"))
(make-player?-expression
(loop (second expression) 'thing variables types)))
((and (list? expression)
(= (length expression) 2)
(eq? (first expression) 'row?))
(unless (equal-type? type 'boolean)
(error #f "row? expression not of type boolean"))
(make-row?-expression (loop (second expression) 'thing variables types)))
((and (list? expression)
(= (length expression) 2)
(eq? (first expression) 'column?))
(unless (equal-type? type 'boolean)
(error #f "column? expression not of type boolean"))
(make-column?-expression
(loop (second expression) 'thing variables types)))
((and (list? expression)
(= (length expression) 2)
(eq? (first expression) 'diagonal?))
(unless (equal-type? type 'boolean)
(error #f "diagonal? expression not of type boolean"))
(make-diagonal?-expression
(loop (second expression) 'thing variables types)))
((and (list? expression)
(= (length expression) 2)
(eq? (first expression) 'move?))
(unless (equal-type? type 'boolean)
(error #f "move? expression not of type boolean"))
(make-move?-expression (loop (second expression) 'thing variables types)))
((and (list? expression)
(= (length expression) 3)
(eq? (first expression) 'opponent?))
(unless (equal-type? type 'boolean)
(error #f "opponent? expression not of type boolean"))
(make-opponent?-expression
(loop (second expression) 'thing variables types)
(loop (third expression) 'thing variables types)))
((and (list? expression)
(= (length expression) 3)
(eq? (first expression) 'cache?))
(unless (equal-type? type 'boolean)
(error #f "cache? expression not of type boolean"))
(make-cache?-expression (loop (second expression) 'thing variables types)
(loop (third expression) 'thing variables types)))
((and (list? expression)
(= (length expression) 3)
(eq? (first expression) 'square?))
(unless (equal-type? type 'boolean)
(error #f "square? expression not of type boolean"))
(make-square?-expression (loop (second expression) 'thing variables types)
(loop (third expression) 'thing variables types)))
((and (list? expression)
(= (length expression) 3)
(eq? (first expression) 'piece?))
(unless (equal-type? type 'boolean)
(error #f "piece? expression not of type boolean"))
(make-piece?-expression (loop (second expression) 'thing variables types)
(loop (third expression) 'thing variables types)))
((and (list? expression)
(= (length expression) 3)
(eq? (first expression) 'distant?))
(unless (equal-type? type 'boolean)
(error #f "distant? expression not of type boolean"))
(make-distant?-expression
(loop (second expression) 'thing variables types)
(loop (third expression) 'thing variables types)))
((and (list? expression)
(= (length expression) 3)
(eq? (first expression) 'close?))
(unless (equal-type? type 'boolean)
(error #f "close? expression not of type boolean"))
(make-close?-expression (loop (second expression) 'thing variables types)
(loop (third expression) 'thing variables types)))
((and (list? expression)
(= (length expression) 4)
(eq? (first expression) 'forward-adjacent?))
(unless (equal-type? type 'boolean)
(error #f "forward-adjacent? expression not of type boolean"))
(make-forward-adjacent?-expression
(loop (second expression) 'thing variables types)
(loop (third expression) 'thing variables types)
(loop (fourth expression) 'thing variables types)))
((and (list? expression)
(= (length expression) 4)
(eq? (first expression) 'forward-diagonal?))
(unless (equal-type? type 'boolean)
(error #f "forward-diagonal? expression not of type boolean"))
(make-forward-diagonal?-expression
(loop (second expression) 'thing variables types)
(loop (third expression) 'thing variables types)
(loop (fourth expression) 'thing variables types)))
((and (list? expression)
(= (length expression) 2)
(eq? (first expression) 'empty?))
(unless (equal-type? type 'boolean)
(error #f "empty? expression not of type boolean"))
(make-empty?-expression (loop (second expression) 'thing variables types)))
((and (list? expression)
(= (length expression) 3)
(eq? (first expression) 'has?))
(unless (equal-type? type 'boolean)
(error #f "has? expression not of type boolean"))
(make-has?-expression (loop (second expression) 'thing variables types)
(loop (third expression) 'thing variables types)))
((and (list? expression) (= (length expression) 3))
;; needs work: should do type inference
(let ((callee-type (parse-type (third expression))))
(unless (or (and (leftward-arrow-type? callee-type)
(equal-type? (leftward-arrow-type-result callee-type)
type))
(and (rightward-arrow-type? callee-type)
(equal-type? (rightward-arrow-type-result callee-type)
type)))
(error #f "Type error"))
(make-application (loop (first expression) callee-type variables types)
(loop (second expression)
((if (leftward-arrow-type? callee-type)
leftward-arrow-type-argument
rightward-arrow-type-argument)
callee-type)
variables
types)
type)))
(else (error #f "Bad expression")))))
(define (pretty-expression expression)
(cond
((variable-access-expression? expression)
(variable-access-expression-variable expression))
((lambda-expression? expression)
`(lambda (,(lambda-expression-variable expression))
,(pretty-expression (lambda-expression-expression expression))))
((and-expression? expression)
`(and ,@(map pretty-expression (and-expression-expressions expression))))
((or-expression? expression)
`(or ,@(map pretty-expression (or-expression-expressions expression))))
((a-expression? expression)
`(a ,(pretty-expression (a-expression-expression1 expression))
,(pretty-expression (a-expression-expression2 expression))))
((some-expression? expression)
`(some ,(pretty-expression (some-expression-expression1 expression))
,(pretty-expression (some-expression-expression2 expression))))
((every-expression? expression)
`(every ,(pretty-expression (every-expression-expression1 expression))
,(pretty-expression (every-expression-expression2 expression))))
((that-expression? expression)
`(that ,(pretty-expression (that-expression-expression1 expression))
,(pretty-expression (that-expression-expression2 expression))))
((no-expression? expression)
`(no ,(pretty-expression (no-expression-expression1 expression))
,(pretty-expression (no-expression-expression2 expression))))
((the-expression? expression)
`(the ,(pretty-expression (the-expression-expression1 expression))
,(pretty-expression (the-expression-expression2 expression))))
((player?-expression? expression)
`(player? ,(pretty-expression (player?-expression-expression expression))))
((row?-expression? expression)
`(row? ,(pretty-expression (row?-expression-expression expression))))
((column?-expression? expression)
`(column? ,(pretty-expression (column?-expression-expression expression))))
((diagonal?-expression? expression)
`(diagonal?
,(pretty-expression (diagonal?-expression-expression expression))))
((move?-expression? expression)
`(move? ,(pretty-expression (move?-expression-expression expression))))
((opponent?-expression? expression)
`(opponent?
,(pretty-expression (opponent?-expression-expression1 expression))
,(pretty-expression (opponent?-expression-expression2 expression))))
((cache?-expression? expression)
`(cache? ,(pretty-expression (cache?-expression-expression1 expression))
,(pretty-expression (cache?-expression-expression2 expression))))
((square?-expression? expression)
`(square? ,(pretty-expression (square?-expression-expression1 expression))
,(pretty-expression (square?-expression-expression2 expression))))
((piece?-expression? expression)
`(piece? ,(pretty-expression (piece?-expression-expression1 expression))
,(pretty-expression (piece?-expression-expression2 expression))))
((distant?-expression? expression)
`(distant?
,(pretty-expression (distant?-expression-expression1 expression))
,(pretty-expression (distant?-expression-expression2 expression))))
((close?-expression? expression)
`(close? ,(pretty-expression (close?-expression-expression1 expression))
,(pretty-expression (close?-expression-expression2 expression))))
((forward-adjacent?-expression? expression)
`(forward-adjacent?
,(pretty-expression (forward-adjacent?-expression-expression1 expression))
,(pretty-expression (forward-adjacent?-expression-expression2 expression))
,(pretty-expression
(forward-adjacent?-expression-expression3 expression))))
((forward-diagonal?-expression? expression)
`(forward-diagonal?
,(pretty-expression (forward-diagonal?-expression-expression1 expression))
,(pretty-expression (forward-diagonal?-expression-expression2 expression))
,(pretty-expression
(forward-diagonal?-expression-expression3 expression))))
((empty?-expression? expression)
`(empty? ,(pretty-expression (empty?-expression-expression expression))))
((has?-expression? expression)
`(has? ,(pretty-expression (has?-expression-expression1 expression))
,(pretty-expression (has?-expression-expression2 expression))))
((application? expression)
`(,(pretty-expression (application-callee expression))
,(pretty-expression (application-argument expression))))
(else (fuck-up))))
(define (pretty-value value)
(cond ((boolean? value) value)
((closure? value)
(cons 'closure (pretty-environment (closure-environment value))))
(else value)))
(define (pretty-environment environment)
(map
(lambda (binding)
(list (binding-variable binding) (pretty-value (binding-value binding))))
environment))
(define (type expression)
(cond ((variable-access-expression? expression)
(variable-access-expression-type expression))
((lambda-expression? expression) (lambda-expression-type expression))
((and-expression? expression) 'boolean)
((or-expression? expression) 'boolean)
((a-expression? expression) 'boolean)
((some-expression? expression) 'boolean)
((every-expression? expression) 'boolean)
((that-expression? expression) 'boolean)
((no-expression? expression) 'boolean)
((the-expression? expression) 'boolean)
((player?-expression? expression) 'boolean)
((row?-expression? expression) 'boolean)
((column?-expression? expression) 'boolean)
((diagonal?-expression? expression) 'boolean)
((move?-expression? expression) 'boolean)
((opponent?-expression? expression) 'boolean)
((cache?-expression? expression) 'boolean)
((square?-expression? expression) 'boolean)
((piece?-expression? expression) 'boolean)
((distant?-expression? expression) 'boolean)
((close?-expression? expression) 'boolean)
((forward-adjacent?-expression? expression) 'boolean)
((forward-diagonal?-expression? expression) 'boolean)
((empty?-expression? expression) 'boolean)
((has?-expression? expression) 'boolean)
((application? expression) (application-type expression))
(else (fuck-up))))
(define (free-variables expression)
(cond
((variable-access-expression? expression)
(list (variable-access-expression-variable expression)))
((lambda-expression? expression) (lambda-expression-variables expression))
((and-expression? expression)
(map-reduce
unionq '() free-variables (and-expression-expressions expression)))
((or-expression? expression)
(map-reduce
unionq '() free-variables (or-expression-expressions expression)))
((a-expression? expression)
(unionq (free-variables (a-expression-expression1 expression))
(free-variables (a-expression-expression2 expression))))
((some-expression? expression)
(unionq (free-variables (some-expression-expression1 expression))
(free-variables (some-expression-expression2 expression))))
((every-expression? expression)
(unionq (free-variables (every-expression-expression1 expression))
(free-variables (every-expression-expression2 expression))))
((that-expression? expression)
(unionq (free-variables (that-expression-expression1 expression))
(free-variables (that-expression-expression2 expression))))
((no-expression? expression)
(unionq (free-variables (no-expression-expression1 expression))
(free-variables (no-expression-expression2 expression))))
((the-expression? expression)
(unionq (free-variables (the-expression-expression1 expression))
(free-variables (the-expression-expression2 expression))))
((player?-expression? expression)
(free-variables (player?-expression-expression expression)))
((row?-expression? expression)
(free-variables (row?-expression-expression expression)))
((column?-expression? expression)
(free-variables (column?-expression-expression expression)))
((diagonal?-expression? expression)
(free-variables (diagonal?-expression-expression expression)))
((move?-expression? expression)
(free-variables (move?-expression-expression expression)))
((opponent?-expression? expression)
(unionq (free-variables (opponent?-expression-expression1 expression))
(free-variables (opponent?-expression-expression2 expression))))
((cache?-expression? expression)
(unionq (free-variables (cache?-expression-expression1 expression))
(free-variables (cache?-expression-expression2 expression))))
((square?-expression? expression)
(unionq (free-variables (square?-expression-expression1 expression))
(free-variables (square?-expression-expression2 expression))))
((piece?-expression? expression)
(unionq (free-variables (piece?-expression-expression1 expression))
(free-variables (piece?-expression-expression2 expression))))
((distant?-expression? expression)
(unionq (free-variables (distant?-expression-expression1 expression))
(free-variables (distant?-expression-expression2 expression))))
((close?-expression? expression)
(unionq (free-variables (close?-expression-expression1 expression))
(free-variables (close?-expression-expression2 expression))))
((forward-adjacent?-expression? expression)
(unionq
(free-variables (forward-adjacent?-expression-expression1 expression))
(unionq
(free-variables (forward-adjacent?-expression-expression2 expression))
(free-variables (forward-adjacent?-expression-expression3 expression)))))
((forward-diagonal?-expression? expression)
(unionq
(free-variables (forward-diagonal?-expression-expression1 expression))
(unionq
(free-variables (forward-diagonal?-expression-expression2 expression))
(free-variables (forward-diagonal?-expression-expression3 expression)))))
((empty?-expression? expression)
(free-variables (empty?-expression-expression expression)))
((has?-expression? expression)
(unionq (free-variables (has?-expression-expression1 expression))
(free-variables (has?-expression-expression2 expression))))
((application? expression)
(unionq (free-variables (application-callee expression))
(free-variables (application-argument expression))))
(else (fuck-up))))
(define (equal-type? type1 type2)
;; needs work: pp/{of,for,in,from,to}/np, pp/by/vp, and pp/when/s
(or (eq? type1 type2)
(and (leftward-arrow-type? type1)
(leftward-arrow-type? type2)
(equal-type? (leftward-arrow-type-argument type1)
(leftward-arrow-type-argument type2))
(equal-type? (leftward-arrow-type-result type1)
(leftward-arrow-type-result type2)))
(and (rightward-arrow-type? type1)
(rightward-arrow-type? type2)
(equal-type? (rightward-arrow-type-argument type1)
(rightward-arrow-type-argument type2))
(equal-type? (rightward-arrow-type-result type1)
(rightward-arrow-type-result type2)))))
(define (equal-value? value1 value2)
(or (eq? value1 value2)
;; needs work
(and
(closure? value1)
(closure? value2)
(eq? (closure-expression value1) (closure-expression value2))
(every
(lambda (binding1 binding2)
(unless (eq? (binding-variable binding1) (binding-variable binding2))
(fuck-up))
(equal-value? (binding-value binding1) (binding-value binding2)))
(closure-environment value1)
(closure-environment value2)))))
(define (create-typed-meaning type expression)
(let* ((type (parse-type type))
(expression (parse-expression expression type)))
(unless (and (lambda-expression? expression)
(null? (lambda-expression-variables expression)))
(fuck-up))
(make-typed-meaning type expression)))
(define *s* 'boolean)
(define *n* '(-> thing boolean))
(define *np* `(-> ,*n* boolean))
(define *d* `(-> ,*n* ,*np*))
(define *pp/of/np* 'pp/of/np)
(define *pp/for/np* 'pp/for/np)
(define *pp/in/np* 'pp/in/np)
(define *pp/from/np* 'pp/from/np)
(define *pp/to/np* 'pp/to/np)
(define *p/in-np* `(-> ,*np* ,*pp/in/np*))
(define *p/for-np* `(-> ,*np* ,*pp/for/np*))
(define *p/of-np* `(-> ,*np* ,*pp/of/np*))
(define *p/from-np* `(-> ,*np* ,*pp/from/np*))
(define *p/to-np* `(-> ,*np* ,*pp/to/np*))
(define *n-pp/of/np* `(-> ,*pp/of/np* ,*n*))
(define *n-pp/for/np* `(-> ,*pp/for/np* ,*n*))
(define *n-pp/in/np* `(-> ,*pp/in/np* ,*n*))
(define *a* `(-> ,*n* ,*n*))
(define *a-pp/for/np* `(-> ,*n* (-> ,*pp/for/np* ,*n*)))
(define *a-pp/for/np-pp/of/np*
`(-> ,*n* (-> ,*pp/for/np* (-> ,*pp/of/np* ,*n*))))
(define *vp* `(<- ,*s* ,*np*))
(define *pp/by/vp* 'pp/by/vp)
(define *p-vp* `(-> ,*vp* ,*pp/by/vp*))
(define *pp/when/s* 'pp/when/s)
(define *p-s* `(-> ,*s* ,*pp/when/s*))
(define *v* *vp*)
(define *v-pp/by/vp* `(-> ,*pp/by/vp* ,*vp*))
(define *v-pp/when/s* `(-> ,*pp/when/s* ,*vp*))
(define *v-np* `(-> ,*np* ,*vp*))
(define *v-np-pp/from/np-pp/to/np*
`(-> ,*np* (-> ,*pp/from/np* (-> ,*pp/to/np* ,*vp*))))
(define *coord-vp* `(<- (-> ,*vp* ,*vp*) ,*vp*))
(define *coord-s* `(<- (-> ,*s* ,*s*) ,*s*))
;; needs work: subcategorization of adjectives based on class of noun
;; complement
;; needs work: subcategorization of nouns, adjectives, and verbs based on
;; class of head noun of np complement of pp complement
;; needs work: subcategorization of prepositions and verbs based on class of
;; head noun of np complement
;; needs work: eliminated relative clauses for now since the current rules
;; don't use them
;; needs work: eliminated restricted associativity of coordination and instead
;; have type-restricted coordination; can't coordinate nouns, NPs,
;; determiners, prepositions, PPs, adjectives, or verbs
;; needs work: ambiguity between ((a n) pp) and (a (n pp))
;; needs work: coordination ambiguity due to lack of declarative sentence
;; constraint
(define *lexicon*
(remove-if-not
(lambda (x) x)
(list
;; needs work: should abstract lexical entries
(and #f
(cons 'a
(create-typed-meaning
*d* '(lambda (noun1) (lambda (noun2) (a noun1 noun2))))))
(cons 'some
(create-typed-meaning
*d* '(lambda (noun1) (lambda (noun2) (some noun1 noun2)))))
(cons 'every
(create-typed-meaning
*d* '(lambda (noun1) (lambda (noun2) (every noun1 noun2)))))
(and #f
(cons 'that
(create-typed-meaning
*d* '(lambda (noun1) (lambda (noun2) (that noun1 noun2))))))
(cons 'no
(create-typed-meaning
*d* '(lambda (noun1) (lambda (noun2) (no noun1 noun2)))))
(cons 'the
(create-typed-meaning
*d* '(lambda (noun1) (lambda (noun2) (the noun1 noun2)))))
;;
(cons 'player (create-typed-meaning *n* '(lambda (thing) (player? thing))))
(cons 'opponent
(create-typed-meaning
*n*
'(lambda (thing1)
(some (lambda (thing2) (player? thing2))
(lambda (thing2) (opponent? thing1 thing2))))))
(cons 'opponent
(create-typed-meaning
*n-pp/of/np* ;player
'(lambda (np)
(lambda (thing1)
(np (lambda (thing2) (opponent? thing1 thing2))
(-> (-> thing boolean) boolean))))))
(cons 'row (create-typed-meaning *n* '(lambda (thing) (row? thing))))
(cons 'column (create-typed-meaning *n* '(lambda (thing) (column? thing))))
(cons
'diagonal (create-typed-meaning *n* '(lambda (thing) (diagonal? thing))))
(cons 'cache
(create-typed-meaning
*n*
'(lambda (thing1)
(some (lambda (thing2) (player? thing2))
(lambda (thing2) (cache? thing1 thing2))))))
(cons 'cache
(create-typed-meaning
*n-pp/for/np* ;player
'(lambda (np)
(lambda (thing1)
(np (lambda (thing2) (cache? thing1 thing2))
(-> (-> thing boolean) boolean))))))
(cons 'square
(create-typed-meaning
*n*
'(lambda (thing1)
(some (lambda (thing2)
(or (row? thing2)
(column? thing2)
(diagonal? thing2)
(some (lambda (thing3) (player? thing3))
(lambda (thing3) (cache? thing2 thing3)))))
(lambda (thing2) (square? thing1 thing2))))))
(cons 'square
(create-typed-meaning
*n-pp/of/np* ;line
'(lambda (np)
(lambda (thing1)
(np (lambda (thing2) (square? thing1 thing2))
(-> (-> thing boolean) boolean))))))
(cons 'square
(create-typed-meaning
*n-pp/in/np* ;line
'(lambda (np)
(lambda (thing1)
(np (lambda (thing2) (square? thing1 thing2))
(-> (-> thing boolean) boolean))))))
(cons 'piece
(create-typed-meaning
*n*
'(lambda (thing1)
(some (lambda (thing2) (player? thing2))
(lambda (thing2) (piece? thing1 thing2))))))
(cons 'piece
(create-typed-meaning
*n-pp/of/np* ;player
'(lambda (np)
(lambda (thing1)
(np (lambda (thing2) (piece? thing1 thing2))
(-> (-> thing boolean) boolean))))))
(cons 'move (create-typed-meaning *n* '(lambda (thing) (move? thing))))
;;
;; needs work: In all of the following, need to deal with the lexical
;; ambiguity of scoping of the "and" that implements adjectival
;; modification relative the the quantifiers of the (implicit)
;; complements of the adjecive.
(cons 'distant
(create-typed-meaning
*a* ;row
'(lambda (noun)
(lambda (thing1)
(and (noun thing1 (-> thing boolean))
(some (lambda (thing2) (player? thing2))
(lambda (thing2) (distant? thing1 thing2))))))))
(cons 'distant
(create-typed-meaning
*a-pp/for/np* ;row player
'(lambda (noun)
(lambda (np)
(lambda (thing1)
(and (noun thing1 (-> thing boolean))
(np (lambda (thing2) (distant? thing1 thing2))
(-> (-> thing boolean) boolean))))))))
(cons 'close
(create-typed-meaning
*a* ;row
'(lambda (noun)
(lambda (thing1)
(and (noun thing1 (-> thing boolean))
(some (lambda (thing2) (player? thing2))
(lambda (thing2) (close? thing1 thing2))))))))
(cons 'close
(create-typed-meaning
*a-pp/for/np* ;row player
'(lambda (noun)
(lambda (np)
(lambda (thing1)
(and (noun thing1 (-> thing boolean))
(np (lambda (thing2) (close? thing1 thing2))
(-> (-> thing boolean) boolean))))))))
;; lexicalization
(cons
'forward-adjacent
(create-typed-meaning
*a* ;square
'(lambda (noun)
(lambda (thing1)
(and
(noun thing1 (-> thing boolean))
(some (lambda (thing2) (player? thing2))
(lambda (thing2)
(some
(lambda (thing3)
(or (row? thing3) (column? thing3) (diagonal? thing3)))
(lambda (thing3)
(some (lambda (thing4) (square? thing4 thing3))
(lambda (thing4)
(forward-adjacent? thing1 thing2 thing4))))))))))))
(cons 'forward-adjacent
(create-typed-meaning
*a-pp/for/np-pp/of/np* ;square player square
'(lambda (noun)
(lambda (np1)
(lambda (np2)
(lambda (thing1)
(and (noun thing1 (-> thing boolean))
(np1 (lambda (thing2)
(np2 (lambda (thing3)
(forward-adjacent? thing1 thing2 thing3))
(-> (-> thing boolean) boolean)))
(-> (-> thing boolean) boolean)))))))))
(cons 'forward-adjacent
(create-typed-meaning
*a-pp/for/np-pp/of/np* ;square player square
'(lambda (noun)
(lambda (np1)
(lambda (np2)
(lambda (thing1)
(and (noun thing1 (-> thing boolean))
(np2 (lambda (thing3)
(np1 (lambda (thing2)
(forward-adjacent? thing1 thing2 thing3))
(-> (-> thing boolean) boolean)))
(-> (-> thing boolean) boolean)))))))))
;; lexicalization
(cons
'forward-diagonal
(create-typed-meaning
*a* ;square
'(lambda (noun)
(lambda (thing1)
(and
(noun thing1 (-> thing boolean))
(some (lambda (thing2) (player? thing2))
(lambda (thing2)
(some
(lambda (thing3)
(or (row? thing3) (column? thing3) (diagonal? thing3)))
(lambda (thing3)
(some (lambda (thing4) (square? thing4 thing3))
(lambda (thing4)
(forward-diagonal? thing1 thing2 thing4))))))))))))
(cons 'forward-diagonal
(create-typed-meaning
*a-pp/for/np-pp/of/np* ;square player square
'(lambda (noun)
(lambda (np1)
(lambda (np2)
(lambda (thing1)
(and (noun thing1 (-> thing boolean))
(np1 (lambda (thing2)
(np2 (lambda (thing3)
(forward-diagonal? thing1 thing2 thing3))
(-> (-> thing boolean) boolean)))
(-> (-> thing boolean) boolean)))))))))
(cons 'forward-diagonal
(create-typed-meaning
*a-pp/for/np-pp/of/np* ;square player square
'(lambda (noun)
(lambda (np1)
(lambda (np2)
(lambda (thing1)
(and (noun thing1 (-> thing boolean))
(np2 (lambda (thing3)
(np1 (lambda (thing2)
(forward-diagonal? thing1 thing2 thing3))
(-> (-> thing boolean) boolean)))
(-> (-> thing boolean) boolean)))))))))
(cons 'board
(create-typed-meaning
*a* ;square
'(lambda (noun)
(lambda (thing1)
(and
(noun thing1 (-> thing boolean))
(some (lambda (thing2)
(or (row? thing2) (column? thing2) (diagonal? thing2)))
(lambda (thing2) (square? thing1 thing2))))))))
(cons 'cache
(create-typed-meaning
*a* ;square
'(lambda (noun)
(lambda (thing1)
(and
(noun thing1 (-> thing boolean))
(some (lambda (thing2) (player? thing2))
(lambda (thing2)
(some (lambda (thing3) (cache? thing3 thing2))
(lambda (thing3) (square? thing1 thing3))))))))))
(cons 'cache
(create-typed-meaning
*a-pp/for/np* ;square player
'(lambda (noun)
(lambda (np)
(lambda (thing1)
(and (noun thing1 (-> thing boolean))
(np (lambda (thing2)
(some (lambda (thing3) (cache? thing3 thing2))
(lambda (thing3) (square? thing1 thing3))))
(-> (-> thing boolean) boolean))))))))
(cons 'empty
(create-typed-meaning
*a* ;line|square
'(lambda (noun)
(lambda (thing)
(and (noun thing (-> thing boolean)) (empty? thing))))))
;;
;; line
(cons 'in (create-typed-meaning *p/in-np* '(lambda (np) np)))
;; player
(cons 'for (create-typed-meaning *p/for-np* '(lambda (np) np)))
;; player|square
(cons 'of (create-typed-meaning *p/of-np* '(lambda (np) np)))
;; square
(cons 'from (create-typed-meaning *p/from-np* '(lambda (np) np)))
;; square
(cons 'to (create-typed-meaning *p/to-np* '(lambda (np) np)))
;; needs work: should only allow moving
(cons 'by (create-typed-meaning *p-vp* '(lambda (vp) vp)))
;; needs work: should only allow wins/1, draws/1, and has
(cons 'when (create-typed-meaning *p-s* '(lambda (s) s)))