File size: 13,685 Bytes
3bdb76c |
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 |
from dreamcoder.type import *
from dreamcoder.program import *
from dreamcoder.frontier import *
from collections import Counter
class MatchFailure(Exception):
pass
class Matcher(object):
def __init__(self, context):
self.context = context
self.variableBindings = {}
@staticmethod
def match(context, fragment, expression, numberOfArguments):
if not mightMatch(fragment, expression):
raise MatchFailure()
m = Matcher(context)
tp = fragment.visit(m, expression, [], numberOfArguments)
return m.context, tp, m.variableBindings
def application(
self,
fragment,
expression,
environment,
numberOfArguments):
'''returns tp of fragment.'''
if not isinstance(expression, Application):
raise MatchFailure()
ft = fragment.f.visit(
self,
expression.f,
environment,
numberOfArguments + 1)
xt = fragment.x.visit(self, expression.x, environment, 0)
self.context, returnType = self.context.makeVariable()
try:
self.context = self.context.unify(ft, arrow(xt, returnType))
except UnificationFailure:
raise MatchFailure()
return returnType.apply(self.context)
def index(self, fragment, expression, environment, numberOfArguments):
# This is a bound variable
surroundingAbstractions = len(environment)
if fragment.bound(surroundingAbstractions):
if expression == fragment:
return environment[fragment.i].apply(self.context)
else:
raise MatchFailure()
# This is a free variable
i = fragment.i - surroundingAbstractions
# Make sure that it doesn't refer to anything bound by a
# lambda in the fragment. Otherwise it cannot be safely lifted
# out of the fragment and preserve semantics
for fv in expression.freeVariables():
if fv < len(environment):
raise MatchFailure()
# The value is going to be lifted out of the fragment
try:
expression = expression.shift(-surroundingAbstractions)
except ShiftFailure:
raise MatchFailure()
# Wrap it in the appropriate number of lambda expressions & applications
# This is because everything has to be in eta-longform
if numberOfArguments > 0:
expression = expression.shift(numberOfArguments)
for j in reversed(range(numberOfArguments)):
expression = Application(expression, Index(j))
for _ in range(numberOfArguments):
expression = Abstraction(expression)
# Added to the bindings
if i in self.variableBindings:
(tp, binding) = self.variableBindings[i]
if binding != expression:
raise MatchFailure()
else:
self.context, tp = self.context.makeVariable()
self.variableBindings[i] = (tp, expression)
return tp
def abstraction(
self,
fragment,
expression,
environment,
numberOfArguments):
if not isinstance(expression, Abstraction):
raise MatchFailure()
self.context, argumentType = self.context.makeVariable()
returnType = fragment.body.visit(
self, expression.body, [argumentType] + environment, 0)
return arrow(argumentType, returnType)
def primitive(self, fragment, expression, environment, numberOfArguments):
if fragment != expression:
raise MatchFailure()
self.context, tp = fragment.tp.instantiate(self.context)
return tp
def invented(self, fragment, expression, environment, numberOfArguments):
if fragment != expression:
raise MatchFailure()
self.context, tp = fragment.tp.instantiate(self.context)
return tp
def fragmentVariable(
self,
fragment,
expression,
environment,
numberOfArguments):
raise Exception(
'Deprecated: matching against fragment variables. Convert fragment to canonical form to get rid of fragment variables.')
def mightMatch(f, e, d=0):
'''Checks whether fragment f might be able to match against expression e'''
if f.isIndex:
if f.bound(d):
return f == e
return True
if f.isPrimitive or f.isInvented:
return f == e
if f.isAbstraction:
return e.isAbstraction and mightMatch(f.body, e.body, d + 1)
if f.isApplication:
return e.isApplication and mightMatch(
f.x, e.x, d) and mightMatch(
f.f, e.f, d)
assert False
def canonicalFragment(expression):
'''
Puts a fragment into a canonical form:
1. removes all FragmentVariable's
2. renames all free variables based on depth first traversal
'''
return expression.visit(CanonicalVisitor(), 0)
class CanonicalVisitor(object):
def __init__(self):
self.numberOfAbstractions = 0
self.mapping = {}
def fragmentVariable(self, e, d):
self.numberOfAbstractions += 1
return Index(self.numberOfAbstractions + d - 1)
def primitive(self, e, d): return e
def invented(self, e, d): return e
def application(self, e, d):
return Application(e.f.visit(self, d), e.x.visit(self, d))
def abstraction(self, e, d):
return Abstraction(e.body.visit(self, d + 1))
def index(self, e, d):
if e.bound(d):
return e
i = e.i - d
if i in self.mapping:
return Index(d + self.mapping[i])
self.mapping[i] = self.numberOfAbstractions
self.numberOfAbstractions += 1
return Index(self.numberOfAbstractions - 1 + d)
def fragmentSize(f, boundVariableCost=0.1, freeVariableCost=0.01):
freeVariables = 0
leaves = 0
boundVariables = 0
for surroundingAbstractions, e in f.walk():
if isinstance(e, (Primitive, Invented)):
leaves += 1
if isinstance(e, Index):
if e.bound(surroundingAbstractions):
boundVariables += 1
else:
freeVariables += 1
assert not isinstance(e, FragmentVariable)
return leaves + boundVariableCost * \
boundVariables + freeVariableCost * freeVariables
def primitiveSize(e):
if e.isInvented:
e = e.body
return fragmentSize(e)
def defragment(expression):
'''Converts a fragment into an invented primitive'''
if isinstance(expression, (Primitive, Invented)):
return expression
expression = canonicalFragment(expression)
for _ in range(expression.numberOfFreeVariables):
expression = Abstraction(expression)
return Invented(expression)
class RewriteFragments(object):
def __init__(self, fragment):
self.fragment = fragment
self.concrete = defragment(fragment)
def tryRewrite(self, e, numberOfArguments):
try:
context, t, bindings = Matcher.match(
Context.EMPTY, self.fragment, e, numberOfArguments)
except MatchFailure:
return None
assert frozenset(bindings.keys()) == frozenset(range(len(bindings))),\
"Perhaps the fragment is not in canonical form?"
e = self.concrete
for j in range(len(bindings) - 1, -1, -1):
_, b = bindings[j]
e = Application(e, b)
return e
def application(self, e, numberOfArguments):
e = Application(e.f.visit(self, numberOfArguments + 1),
e.x.visit(self, 0))
return self.tryRewrite(e, numberOfArguments) or e
def index(self, e, numberOfArguments): return e
def invented(self, e, numberOfArguments): return e
def primitive(self, e, numberOfArguments): return e
def abstraction(self, e, numberOfArguments):
e = Abstraction(e.body.visit(self, 0))
return self.tryRewrite(e, numberOfArguments) or e
def rewrite(self, e): return e.visit(self, 0)
@staticmethod
def rewriteFrontier(frontier, fragment):
worker = RewriteFragments(fragment)
return Frontier([FrontierEntry(program=worker.rewrite(e.program),
logLikelihood=e.logLikelihood,
logPrior=e.logPrior,
logPosterior=e.logPosterior)
for e in frontier],
task=frontier.task)
def proposeFragmentsFromFragment(f):
'''Abstracts out repeated structure within a single fragment'''
yield f
freeVariables = f.numberOfFreeVariables
closedSubtrees = Counter(
subtree for _,
subtree in f.walk() if not isinstance(
subtree,
Index) and subtree.closed)
del closedSubtrees[f]
for subtree, freq in closedSubtrees.items():
if freq < 2:
continue
yield canonicalFragment(f.substitute(subtree, Index(freeVariables)))
def nontrivial(f):
if not isinstance(f, Application):
return False
# Curry
if isinstance(f.x, FragmentVariable):
return False
if isinstance(f.x, Index):
# Make sure that the index is used somewhere else
if not any(
isinstance(
child,
Index) and child.i -
surroundingAbstractions == f.x.i for surroundingAbstractions,
child in f.f.walk()):
return False
numberOfHoles = 0
numberOfVariables = 0
numberOfPrimitives = 0
for surroundingAbstractions, child in f.walk():
if isinstance(child, (Primitive, Invented)):
numberOfPrimitives += 1
if isinstance(child, FragmentVariable):
numberOfHoles += 1
if isinstance(child, Index) and child.free(surroundingAbstractions):
numberOfVariables += 1
#eprint("Fragment %s has %d calls and %d variables and %d primitives"%(f,numberOfHoles,numberOfVariables,numberOfPrimitives))
return numberOfPrimitives + 0.5 * \
(numberOfHoles + numberOfVariables) > 1.5 and numberOfPrimitives >= 1
def violatesLaziness(fragment):
"""
conditionals are lazy on the second and third arguments. this
invariant must be maintained by learned fragments.
"""
for surroundingAbstractions, child in fragment.walkUncurried():
if not child.isApplication:
continue
f, xs = child.applicationParse()
if not (f.isPrimitive and f.name == "if"):
continue
# curried conditionals always violate laziness
if len(xs) != 3:
return True
# yes/no branches
y = xs[1]
n = xs[2]
return \
any(yc.isIndex and yc.i >= yd
for yd, yc in y.walk(surroundingAbstractions)) or \
any(nc.isIndex and nc.i >= nd
for nd, nc in n.walk(surroundingAbstractions))
return False
def proposeFragmentsFromProgram(p, arity):
def fragment(expression, a, toplevel=True):
"""Generates fragments that unify with expression"""
if a == 1:
yield FragmentVariable.single
if a == 0:
yield expression
return
if isinstance(expression, Abstraction):
# Symmetry breaking: (\x \y \z ... f(x,y,z,...)) defragments to be
# the same as f(x,y,z,...)
if not toplevel:
for b in fragment(expression.body, a, toplevel=False):
yield Abstraction(b)
elif isinstance(expression, Application):
for fa in range(a + 1):
for f in fragment(expression.f, fa, toplevel=False):
for x in fragment(expression.x, a - fa, toplevel=False):
yield Application(f, x)
else:
assert isinstance(expression, (Invented, Primitive, Index))
def fragments(expression, a):
"""Generates fragments that unify with subexpressions of expression"""
yield from fragment(expression, a)
if isinstance(expression, Application):
curry = True
if curry:
yield from fragments(expression.f, a)
yield from fragments(expression.x, a)
else:
# Pretend that it is not curried
function, arguments = expression.applicationParse()
yield from fragments(function, a)
for argument in arguments:
yield from fragments(argument, a)
elif isinstance(expression, Abstraction):
yield from fragments(expression.body, a)
else:
assert isinstance(expression, (Invented, Primitive, Index))
return {canonicalFragment(f) for b in range(arity + 1)
for f in fragments(p, b) if nontrivial(f)}
def proposeFragmentsFromFrontiers(frontiers, a, CPUs=1):
fragmentsFromEachFrontier = parallelMap(
CPUs, lambda frontier: {
fp for entry in frontier.entries for f in proposeFragmentsFromProgram(
entry.program, a) for fp in proposeFragmentsFromFragment(f)}, frontiers)
allFragments = Counter(f for frontierFragments in fragmentsFromEachFrontier
for f in frontierFragments)
return [fragment for fragment, frequency in allFragments.items()
if frequency >= 2 and fragment.wellTyped() and nontrivial(fragment)]
|