This repository has been archived by the owner on Apr 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathdomain_language_test.py
737 lines (667 loc) · 29.1 KB
/
domain_language_test.py
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
from typing import Callable, List
import pytest
from .. import SemparseTestCase
from allennlp_semparse.common import ExecutionError, ParsingError
from allennlp_semparse import DomainLanguage, predicate, predicate_with_side_args
class Arithmetic(DomainLanguage):
def __init__(
self, allow_function_currying: bool = False, allow_function_composition: bool = False
):
super().__init__(
start_types={int},
allowed_constants={
# We unfortunately have to explicitly enumerate all allowed constants in the
# grammar. Because we'll be inducing a grammar for this language for use with a
# semantic parser, we need the grammar to be finite, which means we can't allow
# arbitrary constants (you can't parameterize an infinite categorical
# distribution). So our Arithmetic language will have to only operate on simple
# numbers.
"1": 1,
"2": 2,
"3": 3,
"4": 4,
"5": 5,
"6": 6,
"7": 7,
"8": 8,
"9": 9,
"10": 10,
"20": 20,
"-5": -5,
"-2": -2,
},
allow_function_currying=allow_function_currying,
allow_function_composition=allow_function_composition,
)
@predicate
def add(self, num1: int, num2: int) -> int:
return num1 + num2
@predicate
def sum(self, numbers: List[int]) -> int:
return sum(numbers)
# Unfortunately, to make lists, we need to have some function with a fixed number of list
# elements that we can predict. No variable number of arguments - that gives us an infinite
# number of production rules in our grammar.
@predicate
def list1(self, num1: int) -> List[int]:
return [num1]
@predicate
def list2(self, num1: int, num2: int) -> List[int]:
return [num1, num2]
@predicate
def list3(self, num1: int, num2: int, num3: int) -> List[int]:
return [num1, num2, num3]
@predicate
def list4(self, num1: int, num2: int, num3: int, num4: int) -> List[int]:
return [num1, num2, num3, num4]
@predicate
def subtract(self, num1: int, num2: int) -> int:
return num1 - num2
@predicate
def power(self, num1: int, num2: int) -> int:
return num1**num2
@predicate
def multiply(self, num1: int, num2: int) -> int:
return num1 * num2
@predicate
def divide(self, num1: int, num2: int) -> int:
return num1 // num2
@predicate
def halve(self, num1: int) -> int:
return num1 // 2
@predicate
def three(self) -> int:
return 3
@predicate
def three_less(self, function: Callable[[int, int], int]) -> Callable[[int, int], int]:
"""
Wraps a function into a new function that always returns three less than what the original
function would. Totally senseless function that's just here to test higher-order
functions.
"""
def new_function(num1: int, num2: int) -> int:
return function(num1, num2) - 3
return new_function
@predicate
def append(self, list_: List[int], num: int) -> List[int]:
return list_ + [num]
def not_a_predicate(self) -> int:
return 5
def check_productions_match(actual_rules: List[str], expected_right_sides: List[str]):
actual_right_sides = [rule.split(" -> ")[1] for rule in actual_rules]
assert set(actual_right_sides) == set(expected_right_sides)
class TestDomainLanguage(SemparseTestCase):
def setup_method(self):
super().setup_method()
self.language = Arithmetic()
self.curried_language = Arithmetic(
allow_function_currying=True, allow_function_composition=True
)
def test_constant_logical_form(self):
assert self.language.execute("5") == 5
assert self.language.execute("2") == 2
assert self.language.execute("20") == 20
assert self.language.execute("3") == 3
with pytest.raises(ExecutionError, match="Unrecognized constant"):
self.language.execute('"add"')
def test_error_message_with_wrong_arguments(self):
with pytest.raises(ExecutionError):
self.language.execute("(add)")
with pytest.raises(ExecutionError):
self.language.execute("(add 2)")
def test_not_all_functions_are_predicates(self):
# This should not execute to 5, but instead be treated as a constant.
with pytest.raises(ExecutionError, match="Unrecognized constant"):
self.language.execute("not_a_predicate")
def test_basic_logical_form(self):
assert self.language.execute("three") == 3
assert self.language.execute("(add 2 3)") == 5
assert self.language.execute("(subtract 2 3)") == -1
assert self.language.execute("(halve 20)") == 10
def test_list_types(self):
assert self.language.execute("(sum (list1 2))") == 2
assert self.language.execute("(sum (list2 2 3))") == 5
assert self.language.execute("(sum (list4 2 10 -2 -5))") == 5
assert self.language.execute("(sum (list4 2 three (halve 4) (add -5 -2)))") == 0
def test_nested_logical_form(self):
assert self.language.execute("(add 2 (subtract 4 2))") == 4
assert self.language.execute("(halve (multiply (divide 9 3) (power 2 3)))") == 12
def test_higher_order_logical_form(self):
assert self.language.execute("((three_less add) 2 (subtract 4 2))") == 1
def test_execute_action_sequence(self):
# Repeats tests from above, but using `execute_action_sequence` instead of `execute`.
logical_form = "(add 2 (subtract 4 2))"
action_sequence = self.language.logical_form_to_action_sequence(logical_form)
assert self.language.execute_action_sequence(action_sequence) == 4
logical_form = "(halve (multiply (divide 9 3) (power 2 3)))"
action_sequence = self.language.logical_form_to_action_sequence(logical_form)
assert self.language.execute_action_sequence(action_sequence) == 12
logical_form = "((three_less add) 2 (subtract 4 2))"
action_sequence = self.language.logical_form_to_action_sequence(logical_form)
assert self.language.execute_action_sequence(action_sequence) == 1
logical_form = "((three_less add) three (subtract 4 2))"
action_sequence = self.language.logical_form_to_action_sequence(logical_form)
assert self.language.execute_action_sequence(action_sequence) == 2
def test_execute_function_composition(self):
assert self.curried_language.execute("((* halve halve) 8)") == 2
assert self.curried_language.execute("((* sum list1) 8)") == 8
assert self.curried_language.execute("(multiply 4 ((* sum list1) 6))") == 24
assert self.curried_language.execute("(halve ((* halve halve) 8))") == 1
assert self.curried_language.execute("((* (* halve halve) (three_less multiply)) 2 4)") == 1
def test_execute_function_currying(self):
assert self.curried_language.execute("((multiply 3) 6)") == 18
assert self.curried_language.execute("(sum ((list2 1) 7))") == 8
assert self.curried_language.execute("((append 3) (list1 2))") == [2, 3]
assert self.curried_language.execute("((append (list1 4)) 6)") == [4, 6]
assert self.curried_language.execute("((list3 1 2) 3)") == [1, 2, 3]
def test_execute_action_sequence_function_composition(self):
# Repeats tests from above, but using `execute_action_sequence` instead of `execute`.
logical_form = "((* halve halve) 8)"
action_sequence = self.curried_language.logical_form_to_action_sequence(logical_form)
assert self.curried_language.execute_action_sequence(action_sequence) == 2
logical_form = "((* sum list1) 8)"
action_sequence = self.curried_language.logical_form_to_action_sequence(logical_form)
assert self.curried_language.execute_action_sequence(action_sequence) == 8
logical_form = "(multiply 4 ((* sum list1) 6))"
action_sequence = self.curried_language.logical_form_to_action_sequence(logical_form)
assert self.curried_language.execute_action_sequence(action_sequence) == 24
logical_form = "(halve ((* halve halve) 8))"
action_sequence = self.curried_language.logical_form_to_action_sequence(logical_form)
assert self.curried_language.execute_action_sequence(action_sequence) == 1
def test_execute_action_sequence_function_currying(self):
# Repeats tests from above, but using `execute_action_sequence` instead of `execute`.
logical_form = "((multiply 3) 6)"
action_sequence = self.curried_language.logical_form_to_action_sequence(logical_form)
assert self.curried_language.execute_action_sequence(action_sequence) == 18
logical_form = "(sum ((list3 1 2) 7))"
action_sequence = self.curried_language.logical_form_to_action_sequence(logical_form)
assert self.curried_language.execute_action_sequence(action_sequence) == 10
def test_currying_composed_functions(self):
# Testing all of our operations (conversion and execution) for currying composed functions.
logical_form = "(((* sum list3) 1 2) 7)"
action_sequence = [
"@start@ -> int",
"int -> [<int:int>, int]",
"<int:int> -> [<int,int,int:int>, int, int]",
"<int,int,int:int> -> [*, <List[int]:int>, <int,int,int:List[int]>]",
"<List[int]:int> -> sum",
"<int,int,int:List[int]> -> list3",
"int -> 1",
"int -> 2",
"int -> 7",
]
generated_logical_form = self.curried_language.action_sequence_to_logical_form(
action_sequence
)
assert generated_logical_form == logical_form
generated_action_sequence = self.curried_language.logical_form_to_action_sequence(
logical_form
)
assert generated_action_sequence == action_sequence
assert self.curried_language.execute(logical_form) == 10
assert self.curried_language.execute_action_sequence(action_sequence) == 10
def test_get_nonterminal_productions(self):
valid_actions = self.language.get_nonterminal_productions()
assert set(valid_actions.keys()) == {
"@start@",
"int",
"List[int]",
"<int:int>",
"<int,int:int>",
"<List[int]:int>",
"<List[int],int:List[int]>",
"<int:List[int]>",
"<int,int:List[int]>",
"<int,int,int:List[int]>",
"<int,int,int,int:List[int]>",
"<<int,int:int>:<int,int:int>>",
}
check_productions_match(valid_actions["@start@"], ["int"])
check_productions_match(
valid_actions["int"],
[
"[<int,int:int>, int, int]",
"[<int:int>, int]",
"[<List[int]:int>, List[int]]",
"three",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"20",
"-5",
"-2",
],
)
check_productions_match(
valid_actions["List[int]"],
[
"[<int:List[int]>, int]",
"[<int,int:List[int]>, int, int]",
"[<int,int,int:List[int]>, int, int, int]",
"[<int,int,int,int:List[int]>, int, int, int, int]",
"[<List[int],int:List[int]>, List[int], int]",
],
)
check_productions_match(valid_actions["<int:int>"], ["halve"])
check_productions_match(
valid_actions["<int,int:int>"],
[
"[<<int,int:int>:<int,int:int>>, <int,int:int>]",
"add",
"subtract",
"multiply",
"divide",
"power",
],
)
check_productions_match(valid_actions["<List[int],int:List[int]>"], ["append"])
check_productions_match(valid_actions["<List[int]:int>"], ["sum"])
check_productions_match(valid_actions["<int:List[int]>"], ["list1"])
check_productions_match(valid_actions["<int,int:List[int]>"], ["list2"])
check_productions_match(valid_actions["<int,int,int:List[int]>"], ["list3"])
check_productions_match(valid_actions["<int,int,int,int:List[int]>"], ["list4"])
check_productions_match(valid_actions["<<int,int:int>:<int,int:int>>"], ["three_less"])
def test_get_nonterminal_productions_curried_language_and_function_composition(self):
valid_actions = self.curried_language.get_nonterminal_productions()
assert set(valid_actions.keys()) == {
"@start@",
"int",
"List[int]",
"<int:int>",
"<int,int:int>",
"<List[int]:int>",
"<int:List[int]>",
"<int,int:List[int]>",
"<int,int,int:List[int]>",
"<int,int,int,int:List[int]>",
"<List[int],int:List[int]>",
"<<int,int:int>:<int,int:int>>",
# Types induced by allowing function composition
"<List[int]:List[int]>", # also induced from currying
"<int,int,int,int:int>",
"<int,int,int:int>",
"<List[int],int:int>",
}
check_productions_match(valid_actions["@start@"], ["int"])
check_productions_match(
valid_actions["int"],
[
"[<int,int:int>, int, int]",
"[<int:int>, int]",
"[<List[int]:int>, List[int]]",
"three",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"20",
"-5",
"-2",
],
)
check_productions_match(
valid_actions["List[int]"],
[
"[<int:List[int]>, int]",
"[<int,int:List[int]>, int, int]",
"[<int,int,int:List[int]>, int, int, int]",
"[<int,int,int,int:List[int]>, int, int, int, int]",
"[<List[int],int:List[int]>, List[int], int]",
],
)
check_productions_match(
valid_actions["<int:int>"],
[
"halve",
# Production due to function composition
"[*, <int:int>, <int:int>]",
"[*, <List[int]:int>, <int:List[int]>]",
# Production due to function currying
"[<int,int:int>, int]",
],
)
check_productions_match(
valid_actions["<int,int:int>"],
[
"[<<int,int:int>:<int,int:int>>, <int,int:int>]",
"add",
"subtract",
"multiply",
"divide",
"power",
# Production due to function composition
"[*, <int:int>, <int,int:int>]",
"[*, <List[int]:int>, <int,int:List[int]>]",
],
)
check_productions_match(
valid_actions["<List[int]:int>"],
[
"sum",
# Production due to function composition
"[*, <int:int>, <List[int]:int>]",
"[*, <List[int]:int>, <List[int]:List[int]>]",
],
)
check_productions_match(
valid_actions["<int:List[int]>"],
[
"list1",
# Production due to function composition
"[*, <int:List[int]>, <int:int>]",
"[*, <List[int]:List[int]>, <int:List[int]>]",
# Production due to function currying
"[<List[int],int:List[int]>, List[int]]",
"[<int,int:List[int]>, int]",
"[<int,int,int:List[int]>, int, int]",
"[<int,int,int,int:List[int]>, int, int, int]",
],
)
check_productions_match(
valid_actions["<List[int],int:List[int]>"],
["append", "[*, <List[int]:List[int]>, <List[int],int:List[int]>]"],
)
check_productions_match(
valid_actions["<int,int:List[int]>"],
[
"list2",
"[*, <int:List[int]>, <int,int:int>]",
"[*, <List[int]:List[int]>, <int,int:List[int]>]",
],
)
check_productions_match(
valid_actions["<int,int,int:List[int]>"],
["list3", "[*, <List[int]:List[int]>, <int,int,int:List[int]>]"],
)
check_productions_match(
valid_actions["<int,int,int,int:List[int]>"],
["list4", "[*, <List[int]:List[int]>, <int,int,int,int:List[int]>]"],
)
check_productions_match(
valid_actions["<<int,int:int>:<int,int:int>>"],
["three_less", "[*, <<int,int:int>:<int,int:int>>, <<int,int:int>:<int,int:int>>]"],
)
# Production due to function composition
check_productions_match(
valid_actions["<List[int]:List[int]>"],
[
"[*, <int:List[int]>, <List[int]:int>]",
"[*, <List[int]:List[int]>, <List[int]:List[int]>]",
"[<List[int],int:List[int]>, int]",
],
)
check_productions_match(
valid_actions["<int,int,int,int:int>"],
[
"[*, <List[int]:int>, <int,int,int,int:List[int]>]",
],
)
check_productions_match(
valid_actions["<int,int,int:int>"],
[
"[*, <List[int]:int>, <int,int,int:List[int]>]",
],
)
check_productions_match(
valid_actions["<List[int],int:int>"],
[
"[*, <List[int]:int>, <List[int],int:List[int]>]",
],
)
def test_logical_form_to_action_sequence(self):
action_sequence = self.language.logical_form_to_action_sequence("(add 2 3)")
assert action_sequence == [
"@start@ -> int",
"int -> [<int,int:int>, int, int]",
"<int,int:int> -> add",
"int -> 2",
"int -> 3",
]
action_sequence = self.language.logical_form_to_action_sequence(
"(halve (subtract 8 three))"
)
assert action_sequence == [
"@start@ -> int",
"int -> [<int:int>, int]",
"<int:int> -> halve",
"int -> [<int,int:int>, int, int]",
"<int,int:int> -> subtract",
"int -> 8",
"int -> three",
]
logical_form = "(halve (multiply (divide 9 three) (power 2 3)))"
action_sequence = self.language.logical_form_to_action_sequence(logical_form)
assert action_sequence == [
"@start@ -> int",
"int -> [<int:int>, int]",
"<int:int> -> halve",
"int -> [<int,int:int>, int, int]",
"<int,int:int> -> multiply",
"int -> [<int,int:int>, int, int]",
"<int,int:int> -> divide",
"int -> 9",
"int -> three",
"int -> [<int,int:int>, int, int]",
"<int,int:int> -> power",
"int -> 2",
"int -> 3",
]
def test_logical_form_to_action_sequence_with_higher_order_functions(self):
action_sequence = self.language.logical_form_to_action_sequence("((three_less add) 2 3)")
assert action_sequence == [
"@start@ -> int",
"int -> [<int,int:int>, int, int]",
"<int,int:int> -> [<<int,int:int>:<int,int:int>>, <int,int:int>]",
"<<int,int:int>:<int,int:int>> -> three_less",
"<int,int:int> -> add",
"int -> 2",
"int -> 3",
]
def test_logical_form_to_action_sequence_with_function_composition(self):
action_sequence = self.curried_language.logical_form_to_action_sequence(
"((* halve halve) 8)"
)
assert action_sequence == [
"@start@ -> int",
"int -> [<int:int>, int]",
"<int:int> -> [*, <int:int>, <int:int>]",
"<int:int> -> halve",
"<int:int> -> halve",
"int -> 8",
]
action_sequence = self.curried_language.logical_form_to_action_sequence("((* sum list1) 8)")
assert action_sequence == [
"@start@ -> int",
"int -> [<int:int>, int]",
"<int:int> -> [*, <List[int]:int>, <int:List[int]>]",
"<List[int]:int> -> sum",
"<int:List[int]> -> list1",
"int -> 8",
]
# Trying a mix of regular composition and function-composition
action_sequence = self.curried_language.logical_form_to_action_sequence(
"(halve ((* halve halve) 8))"
)
assert action_sequence == [
"@start@ -> int",
"int -> [<int:int>, int]",
"<int:int> -> halve",
"int -> [<int:int>, int]",
"<int:int> -> [*, <int:int>, <int:int>]",
"<int:int> -> halve",
"<int:int> -> halve",
"int -> 8",
]
# Idea is to execute multiply(4, sum(list3(2, 4))(6)) where list3 is curried and then
# composed with sum
action_sequence = self.curried_language.logical_form_to_action_sequence(
"(multiply 4 ((* sum (list3 2 4)) 6))"
)
assert action_sequence == [
"@start@ -> int",
"int -> [<int,int:int>, int, int]",
"<int,int:int> -> multiply",
"int -> 4",
"int -> [<int:int>, int]",
"<int:int> -> [*, <List[int]:int>, <int:List[int]>]",
"<List[int]:int> -> sum",
"<int:List[int]> -> [<int,int,int:List[int]>, int, int]",
"<int,int,int:List[int]> -> list3",
"int -> 2",
"int -> 4",
"int -> 6",
]
action_sequence = self.curried_language.logical_form_to_action_sequence(
"((* (* halve halve) (three_less multiply)) 2 4)"
)
assert action_sequence == [
"@start@ -> int",
"int -> [<int,int:int>, int, int]",
"<int,int:int> -> [*, <int:int>, <int,int:int>]",
"<int:int> -> [*, <int:int>, <int:int>]",
"<int:int> -> halve",
"<int:int> -> halve",
"<int,int:int> -> [<<int,int:int>:<int,int:int>>, <int,int:int>]",
"<<int,int:int>:<int,int:int>> -> three_less",
"<int,int:int> -> multiply",
"int -> 2",
"int -> 4",
]
def test_logical_form_to_action_sequence_with_function_currying(self):
action_sequence = self.curried_language.logical_form_to_action_sequence("((multiply 3) 6)")
assert action_sequence == [
"@start@ -> int",
"int -> [<int:int>, int]",
"<int:int> -> [<int,int:int>, int]",
"<int,int:int> -> multiply",
"int -> 3",
"int -> 6",
]
action_sequence = self.curried_language.logical_form_to_action_sequence(
"(sum ((list3 1 2) 7))"
)
assert action_sequence == [
"@start@ -> int",
"int -> [<List[int]:int>, List[int]]",
"<List[int]:int> -> sum",
"List[int] -> [<int:List[int]>, int]",
"<int:List[int]> -> [<int,int,int:List[int]>, int, int]",
"<int,int,int:List[int]> -> list3",
"int -> 1",
"int -> 2",
"int -> 7",
]
def test_action_sequence_to_logical_form(self):
logical_form = "(add 2 3)"
action_sequence = self.language.logical_form_to_action_sequence(logical_form)
recovered_logical_form = self.language.action_sequence_to_logical_form(action_sequence)
assert recovered_logical_form == logical_form
logical_form = "(halve (multiply (divide 9 three) (power 2 3)))"
action_sequence = self.language.logical_form_to_action_sequence(logical_form)
recovered_logical_form = self.language.action_sequence_to_logical_form(action_sequence)
assert recovered_logical_form == logical_form
logical_form = "((three_less add) 2 3)"
action_sequence = self.language.logical_form_to_action_sequence(logical_form)
recovered_logical_form = self.language.action_sequence_to_logical_form(action_sequence)
assert recovered_logical_form == logical_form
def test_action_sequence_to_logical_form_with_function_composition(self):
logical_form = "((* halve halve) 8)"
action_sequence = self.curried_language.logical_form_to_action_sequence(logical_form)
recovered_logical_form = self.curried_language.action_sequence_to_logical_form(
action_sequence
)
assert recovered_logical_form == logical_form
logical_form = "((* sum list1) 8)"
action_sequence = self.curried_language.logical_form_to_action_sequence(logical_form)
recovered_logical_form = self.curried_language.action_sequence_to_logical_form(
action_sequence
)
assert recovered_logical_form == logical_form
logical_form = "(multiply 4 ((* sum list1) 6))"
action_sequence = self.curried_language.logical_form_to_action_sequence(logical_form)
recovered_logical_form = self.curried_language.action_sequence_to_logical_form(
action_sequence
)
assert recovered_logical_form == logical_form
logical_form = "(halve ((* halve halve) 8))"
action_sequence = self.curried_language.logical_form_to_action_sequence(logical_form)
recovered_logical_form = self.curried_language.action_sequence_to_logical_form(
action_sequence
)
assert recovered_logical_form == logical_form
def test_action_sequence_to_logical_form_with_function_currying(self):
logical_form = "((multiply 3) 6)"
action_sequence = self.curried_language.logical_form_to_action_sequence(logical_form)
recovered_logical_form = self.curried_language.action_sequence_to_logical_form(
action_sequence
)
assert recovered_logical_form == logical_form
logical_form = "(sum ((list3 1 2) 7))"
action_sequence = self.curried_language.logical_form_to_action_sequence(logical_form)
recovered_logical_form = self.curried_language.action_sequence_to_logical_form(
action_sequence
)
assert recovered_logical_form == logical_form
def test_logical_form_parsing_fails_on_bad_inputs(self):
# We don't catch all type inconsistencies in the code, but we _do_ catch some. If we add
# more that we catch, this is a good place to test for them.
with pytest.raises(ParsingError, match="Wrong number of arguments"):
self.language.logical_form_to_action_sequence("(halve 2 3)")
with pytest.raises(ParsingError, match="Wrong number of arguments"):
self.language.logical_form_to_action_sequence("(add 3)")
with pytest.raises(ParsingError, match="unallowed start type"):
self.language.logical_form_to_action_sequence("add")
with pytest.raises(ParsingError, match="Zero-arg function or constant"):
self.language.logical_form_to_action_sequence("(sum (3 2))")
with pytest.raises(ParsingError, match="did not have expected type"):
self.language.logical_form_to_action_sequence("(sum (add 2 3))")
def test_execution_with_side_arguments(self):
class SideArgumentLanguage(DomainLanguage):
def __init__(self) -> None:
super().__init__(start_types={int}, allowed_constants={"1": 1, "2": 2, "3": 3})
@predicate_with_side_args(["num2"])
def add(self, num1: int, num2: int) -> int:
return num1 + num2
@predicate_with_side_args(["num"])
def current_number(self, num: int) -> int:
return num
language = SideArgumentLanguage()
# (add 1)
action_sequence = [
"@start@ -> int",
"int -> [<int:int>, int]",
"<int:int> -> add",
"int -> 1",
]
# For each action in the action sequence, we pass state. We only actually _use_ the state
# when the action we've predicted at that step needs the state. In this case, the third
# action will get {'num2': 3} passed to the `add()` function.
state = [{"num2": 1}, {"num2": 2}, {"num2": 3}, {"num2": 4}]
assert language.execute_action_sequence(action_sequence, state) == 4
# (add current_number)
action_sequence = [
"@start@ -> int",
"int -> [<int:int>, int]",
"<int:int> -> add",
"int -> current_number",
]
state = [
{"num2": 1, "num": 5},
{"num2": 2, "num": 6},
{"num2": 3, "num": 7},
{"num2": 4, "num": 8},
]
assert language.execute_action_sequence(action_sequence, state) == 11