-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathunit_tests_compilation.py
130 lines (118 loc) · 4.54 KB
/
unit_tests_compilation.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
import logging
import unittest
import random
from math import sqrt
from scipy.stats import chisquare
from type_system import Type, PolymorphicType, PrimitiveType, Arrow, List, UnknownType, INT, BOOL, STRING
from program import Program, Function, Variable, BasicPrimitive, New
from program_as_list import evaluation_from_compressed, reconstruct_from_compressed
from dsl import DSL
from DSL.deepcoder import semantics,primitive_types
class TestSum(unittest.TestCase):
def test_construction_CFG_toy(self):
"""
Checks the construction of a CFG from a toy DSL
"""
t0 = PolymorphicType("t0")
t1 = PolymorphicType("t1")
semantics = {
"RANGE": (),
"HEAD": (),
"SUCC": (),
"MAP": (),
}
primitive_types = {
"HEAD": Arrow(List(INT), INT),
"RANGE": Arrow(INT, List(INT)),
"SUCC": Arrow(INT, INT),
"MAP": Arrow(Arrow(t0, t1), Arrow(List(t0), List(t1))),
}
toy_DSL = DSL(semantics, primitive_types)
type_request = Arrow(List(INT), List(INT))
toy_CFG = toy_DSL.DSL_to_CFG(type_request)
self.assertTrue(len(toy_CFG.rules) == 14)
self.assertTrue(len(toy_CFG.rules[toy_CFG.start]) == 3)
def test_construction_CFG_deepcoder(self):
"""
Checks the construction of a PCFG from the DeepCoder DSL
"""
deepcoder = DSL(semantics, primitive_types)
type_request = Arrow(List(INT), List(INT))
deepcoder_CFG = deepcoder.DSL_to_CFG(type_request)
# checks that all non-terminals are productive
for S in deepcoder_CFG.rules:
self.assertTrue(len(deepcoder_CFG.rules[S]) > 0)
for P in deepcoder_CFG.rules[S]:
args_P = deepcoder_CFG.rules[S][P]
for arg in args_P:
self.assertTrue(arg in deepcoder_CFG.rules)
def test_construction_PCFG_toy(self):
"""
Checks the construction of a PCFG from a DSL
"""
t0 = PolymorphicType("t0")
t1 = PolymorphicType("t1")
semantics = {
"RANGE": (),
"HEAD": (),
"TAIL": (),
"SUCC": (),
"PRED": (),
"MAP": (),
}
primitive_types = {
"HEAD": Arrow(List(INT), INT),
"TAIL": Arrow(List(INT), INT),
"RANGE": Arrow(INT, List(INT)),
"SUCC": Arrow(INT, INT),
"PRED": Arrow(INT, INT),
"MAP": Arrow(Arrow(t0, t1), Arrow(List(t0), List(t1))),
}
toy_DSL = DSL(semantics, primitive_types)
type_request = Arrow(List(INT), List(INT))
toy_CFG = toy_DSL.DSL_to_CFG(type_request)
toy_PCFG = toy_CFG.CFG_to_Uniform_PCFG()
toy_PCFG.compute_max_probability()
max_program = Function(
BasicPrimitive("MAP"),
[
BasicPrimitive("HEAD"),
Function(BasicPrimitive("MAP"), [BasicPrimitive("RANGE"), Variable(0)]),
],
)
self.assertTrue(
toy_PCFG.max_probability[toy_PCFG.start].typeless_eq(max_program)
)
for S in toy_PCFG.rules:
max_program = toy_PCFG.max_probability[S]
self.assertTrue(
max_program.probability[(toy_PCFG.__hash__(), S)]
== toy_PCFG.probability_program(S, max_program)
)
def test_construction_PCFG_deepcoder(self):
"""
Checks the construction of a PCFG from a DSL
"""
deepcoder = DSL(semantics, primitive_types)
type_request = Arrow(List(INT), List(INT))
deepcoder_CFG = deepcoder.DSL_to_CFG(type_request)
deepcoder_PCFG = deepcoder_CFG.CFG_to_Random_PCFG()
deepcoder_PCFG.compute_max_probability()
for S in deepcoder_PCFG.rules:
max_program = deepcoder_PCFG.max_probability[S]
self.assertTrue(
deepcoder_PCFG.max_probability[S].probability[
(deepcoder_PCFG.__hash__(), S)
]
== deepcoder_PCFG.probability_program(S, max_program)
)
for P in deepcoder_PCFG.rules[S]:
max_program = deepcoder_PCFG.max_probability[(S, P)]
self.assertTrue(
deepcoder_PCFG.max_probability[(S, P)].probability[
(deepcoder_PCFG.__hash__(), S)
]
== deepcoder_PCFG.probability_program(S, max_program)
)
if __name__ == "__main__":
unittest.main(verbosity=2)