-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunction_repository.py
302 lines (251 loc) · 11.4 KB
/
function_repository.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import collections
import copy
import itertools
import time
import ground
import topology
import tree
import lis
def contains(expression, variables):
for element in expression:
if isinstance(element, tuple):
if contains(element, variables):
return True
else:
if element in variables:
return True
return False
class FunctionRepository(object):
def __init__(self, fname):
self.primitives = {}
self.arities = collections.defaultdict(set)
self.in_types = collections.defaultdict(set)
self.out_types = collections.defaultdict(set)
self.extended_primitives = None
self.extended_arities = None
self.extended_in_types = None
self.extended_out_types = None
self.target_function_name = None
self.returned_constants = {}
self.fname = fname
if os.path.exists(self.fname):
self.derived = self.load()
else:
# name: (input_types, output_type, code)
self.derived = collections.OrderedDict()
self.interpreter = lis.Interpreter()
self.consider('if', 'car', 'cdr', 'cons', 'succ', 'pred', 0, 'list?',
('quote', ()))
for func_name, desc in self.derived.items():
_, _, code = desc
self.interpreter.interpret(code)
def load(self):
def tokenize(case):
return case.replace('(',' ( ').replace(')',' ) ').split()
def read_from(tokens):
reserved_words = ('quote', 'if', 'define', 'lambda')
token = tokens.pop(0)
if '(' == token:
L = []
while tokens[0] != ')':
L.append(read_from(tokens))
tokens.pop(0) # pop off ')'
return L
else:
if token.isdigit():
return int(token)
if token in reserved_words:
return token
return lis.Symbol(token)
def to_tuple(l):
if isinstance(l, list) or isinstance(l, tuple):
return tuple([to_tuple(e) for e in l])
else:
return l
def parse(case):
return to_tuple(read_from(tokenize(case)))
res = collections.OrderedDict()
with open(self.fname) as f:
for line in f:
name, desc = [e.strip() for e in line.split(':')]
tuple_desc = parse(desc)
res[name] = tuple_desc
return res
def save(self):
def to_tuple_string(l):
if isinstance(l, list) or isinstance(l, tuple):
res = ' '.join([to_tuple_string(e) for e in l])
return '('+res+')'
else:
return str(l)
with open(self.fname, 'w') as f:
for func_name, description in self.derived.items():
desc_string = to_tuple_string(description)
f.write(func_name+': '+str(desc_string)+'\n')
def consider(self, *func_names):
for func_name in func_names:
assert func_name in self.derived or func_name in ground.primitives
if func_name in self.derived:
in_types, out_type, code = self.derived[func_name]
self.interpreter.interpret(code)
self.teach_primitive(func_name, (in_types, out_type))
elif func_name in ground.primitives:
self.teach_primitive(func_name, ground.primitives[func_name])
def _reset(self):
self.primitives.clear()
self.arities.clear()
self.in_types.clear()
self.out_types.clear()
def teach_primitive(self, func_name, f):
self.primitives[func_name] = f
in_types, out_type = f
self.arities[len(in_types)].add(func_name)
self.in_types[in_types].add(func_name)
# Find all ascendant and descendant output types
for typ in ground.type_tree.find(out_type).ascendants():
self.out_types[typ].add(func_name)
for typ in ground.type_tree.find(out_type).descendants():
self.out_types[typ].add(func_name)
def available_functions(self):
return tuple(self.primitives.keys())
def _check(self, name, code, examples):
res = self.interpreter.interpret(code)
if res != None:
# Sanity check
import ipdb; ipdb.set_trace() # EVIL_DEBUG
for question, answer in examples:
# TODO: If I ever get to implement functions as first-class objects,
# I'll have to make sure to assign Symbol status to some arguments
q = (lis.Symbol(name),)+question
try:
interpreter_answer = self.interpreter.interpret(q)
except:
return False
if interpreter_answer != answer:
return False
return True
def fit(self, name, in_names, in_types, out_type, examples, debug=False):
"""
self.typed_trees.func.reset() # Forget previous runs
# (functools.partial gets in the way)
"""
self.returned_constants = {}
if (debug and name in self.derived and
self._check(name, self.derived[name][2], examples)):
if debug:
print("Definition of '{}' already computed.".format(name))
return
self.target_function_name = name
# Update the dictionaries of available operations with those provided by
# the parameters of this particular problem
self.extended_primitives = copy.deepcopy(self.primitives)
self.extended_arities = copy.deepcopy(self.arities)
self.extended_in_types = copy.deepcopy(self.in_types)
self.extended_out_types = copy.deepcopy(self.out_types)
for param_name, param_type in zip(in_names, in_types):
self.extended_arities[0].add(param_name)
self.extended_in_types[()].add(param_name)
for asc_par_type in ground.type_tree.find(param_type).ascendants():
self.extended_out_types[asc_par_type].add(param_name)
for asc_par_type in ground.type_tree.find(param_type).descendants():
self.extended_out_types[asc_par_type].add(param_name)
# Include the possibility of recursion
self.extended_arities[len(in_names)].add(name)
self.extended_primitives[name] = (in_types, out_type)
# Find all descendant type combinations that match our types
for tc in ground.descendant_combinations(in_types):
self.extended_in_types[tc].add(name)
for asc_par_type in ground.type_tree.find(out_type).ascendants():
self.extended_out_types[asc_par_type].add(name)
# Restrict possible shapes of trees to those with present arities
n_nodes = 1
found = False
while not found:
if debug: print('Nodes:', n_nodes)
t0 = time.time()
n_evaluations = 0
for t in self.typed_trees(n_nodes, out_type, in_names,
verbose=False):
code = ('define', name, ('lambda', in_names, t))
n_evaluations += 1
found = self._check(name, code, examples)
if found: break
n_nodes += 1
t1 = time.time()
if debug:
print('%d evals, %.2f seconds, %g s/eval'%(n_evaluations,
t1-t0,
(t1-t0)/\
(n_evaluations+1)))
if debug:
print('Found!')
print(code)
t = tree.Tree.from_code(code)
print(t)
self.derived[name] = (in_types, out_type, code)
self.save()
self.target_function_name = None
return code
#@Memo
def typed_trees(self, n_nodes, out_type, var_names, verbose = False):
if n_nodes == 1:
compatible_fs = (self.extended_arities[0] &
self.extended_out_types[out_type])
for f in compatible_fs:
if isinstance(f, int) or f == ('quote', ()):
if f not in self.returned_constants:
self.returned_constants[f] = (n_nodes, f)
if verbose: print('--->', f)
yield f
elif self.returned_constants[f] == (n_nodes, f):
if verbose: print('--->', f)
yield f
# else: this constant should have been returned earlier
else:
if verbose: print('--->', f)
yield lis.Symbol(f)
else:
arities = sorted([arity for arity in self.extended_arities.keys()
if arity>0 and arity<n_nodes], reverse=True)
for arity in arities:
compatible_fs = (self.extended_out_types[out_type] &
self.extended_arities[arity])
for f in compatible_fs:
inputs = self.extended_primitives[f][0]
# Find all the possible combinations of arities that sum
# up to the target arity-1. Then find the typed_trees
# with those arities and plug them to the candidate
# function for evaluation
# This is going to get ugly fast
for part in topology.limited_partitions(n_nodes-1, arity):
parametrizations = [list(e)+[var_names]
for e in zip(part, inputs)]
for children in itertools.product(
*(self.typed_trees(*parametrization)
for parametrization in parametrizations)):
if (self.target_function_name != f and
not contains(children, var_names)):
# Constant
try:
code = (lis.Symbol(f),)+children
k = self.interpreter.interpret(code)
except:
k=None
if k != None:
if k not in self.returned_constants:
self.returned_constants[k] = (n_nodes,
code)
if verbose: print('--->', k)
yield k
elif self.returned_constants[k] == (n_nodes,
code):
if verbose: print('--->', k)
yield k
# else: this constant should have been
# returned earlier. Don't return anything
else:
if verbose: print('--->', f, children)
yield (lis.Symbol(f),)+children