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
|
/*
This file is part of the Maude 2 interpreter.
Copyright 1997-2006 SRI International, Menlo Park, CA 94025, USA.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
//
// Implementation for abstract class Strategy.
//
// utility stuff
#include "macros.hh"
#include "vector.hh"
// forward declarations
#include "interface.hh"
#include "core.hh"
#include "higher.hh"
#include "strategyLanguage.hh"
// interface class definitions
#include "term.hh"
#include "subproblem.hh"
// core class definitions
#include "cachedDag.hh"
#include "rule.hh"
#include "rewritingContext.hh"
// higher class definitions
#include "assignmentConditionFragment.hh"
#include "rewriteConditionFragment.hh"
// strategy language class definitions
#include "strategicSearch.hh"
#include "strategyExpression.hh"
#include "applicationStrategy.hh"
#include "decompositionProcess.hh"
#include "matchProcess.hh"
#include "rewriteTask.hh"
#include "applicationProcess.hh"
ApplicationProcess::ApplicationProcess(StrategicSearch& searchObject,
int startIndex,
ApplicationStrategy* strategy,
StrategyStackManager::StackId pending,
StrategicExecution* taskSibling,
StrategicProcess* insertionPoint)
: StrategicProcess(taskSibling, insertionPoint),
rewriteState(searchObject.getContext(),
searchObject.getCanonical(startIndex),
strategy->getLabel(),
strategy->getTop() ? NONE : UNBOUNDED),
pending(pending),
strategy(strategy)
{
Vector<CachedDag>& values = strategy->getValues();
int nrValues = values.size();
if (nrValues > 0)
{
Vector<Term*> tmpVariables(strategy->getVariables());
Vector<DagRoot*> tmpValues(nrValues);
for (int i = 0; i < nrValues; ++i)
tmpValues[i] = values[i].getDagRoot();
rewriteState->setInitialSubstitution(tmpVariables, tmpValues);
}
}
StrategicExecution::Survival
ApplicationProcess::run(StrategicSearch& searchObject)
{
if (rewriteState->findNextRewrite())
{
Rule* rule = rewriteState->getRule();
if (rule->hasCondition())
{
//
// Need to check that we have the correct number of substrategies for our rule.
//
int nrStrategies = strategy->getStrategies().size();
int nrRewriteFragments = 0;
const Vector<ConditionFragment*>& condition = rule->getCondition();
FOR_EACH_CONST(i, Vector<ConditionFragment*>, condition)
{
if (dynamic_cast<RewriteConditionFragment*>(*i))
++nrRewriteFragments;
}
if (nrStrategies != nrRewriteFragments)
return SURVIVE; // might match a different rule on later runs
//
// We need to check the condition in a fair way, given
// that rewrite conditions may have to follow a given strategy
// may not terminate. Also processing the condition may
// bind substitution entries needed to build an instance of
// the rhs of the rule.
//
if (resolveRemainingConditionFragments(searchObject,
rewriteState,
rewriteState->getPositionIndex(),
rewriteState->getExtensionInfo(),
rewriteState->getContext(),
rule,
0,
strategy->getStrategies(),
0,
pending,
this,
this) == SURVIVE)
return SURVIVE;
}
else
{
if (strategy->getStrategies().size() > 0)
return SURVIVE; // might match a different rule on later runs
int resultIndex = doRewrite(searchObject,
rewriteState,
rewriteState->getPositionIndex(),
rewriteState->getExtensionInfo(),
rewriteState->getContext(),
rule);
if (resultIndex != NONE)
{
(void) new DecompositionProcess(resultIndex, pending, this, this);
return SURVIVE; // stick around to look for another rewrite
}
}
}
finished(this); // need to unlink ourself from slave list before we request deletion
return DIE; // request deletion
}
int
ApplicationProcess::doRewrite(StrategicSearch& searchObject,
SharedRewriteSearchState::Ptr rewriteState,
PositionState::PositionIndex redexIndex,
ExtensionInfo* extensionInfo,
Substitution* substitution,
Rule* rule)
{
//
// The rule for using substitution is tricky:
// It does not belong to us and we should never bind variables that
// occur in the rule pattern or any fragment pattern in case we
// disrupt matching in other branches of the search. But it is fine
// to bind constructed entries, protected or not, since they will
// simply be overwritten in other branches.
//
RewritingContext* baseContext = rewriteState->getContext();
bool trace = RewritingContext::getTraceStatus();
if (trace)
{
//
// We have a problem: the whole term that is being rewritten is in the
// rewriting context rewriteState->getContext() while the substitution we
// will use is in context which may be a different rewriting context with
// a different whole term.
//
// We resolve the issue by creating a special context containing the correct
// whole term and substitution just for tracing.
//
RewritingContext* tracingContext = baseContext->makeSubcontext(baseContext->root());
//
// We use clone() rather than copy() because tracingContext will have copy size of 0.
//
tracingContext->clone(*substitution);
tracingContext->tracePreRuleRewrite(rewriteState->getDagNode(redexIndex), rule);
delete tracingContext;
if (baseContext->traceAbort())
return NONE;
}
//
// Instantiate the rhs of the rule with the substitution.
//
//cout << "ApplicationProcess::doRewrite() nrFragileBindings = " << substitution->nrFragileBindings() << endl;
DagNode* replacement = rule->getRhsBuilder().construct(*substitution);
//
// Rebuild the original term, using the replacement in place of the redex,
// also accounting for any extension info; count this a rule rewrite
//
RewriteSearchState::DagPair r = rewriteState->rebuildDag(replacement, extensionInfo, redexIndex);
searchObject.getContext()->incrementRlCount();
//
// Make a new subcontext to equationally reduce the new term.
//
RewritingContext* c = baseContext->makeSubcontext(r.first);
if (trace)
{
c->tracePostRuleRewrite(r.second);
if (c->traceAbort())
{
delete c;
return NONE;
}
}
c->reduce();
if (c->traceAbort())
{
delete c;
return NONE;
}
searchObject.getContext()->addInCount(*c);
int dagIndex = searchObject.insert(c->root());
delete c;
return dagIndex;
}
StrategicExecution::Survival
ApplicationProcess::resolveRemainingConditionFragments(StrategicSearch& searchObject,
SharedRewriteSearchState::Ptr rewriteState,
PositionState::PositionIndex redexIndex,
ExtensionInfo* extensionInfo,
Substitution* substitutionSoFar,
Rule* rule,
int fragmentNr,
const Vector<StrategyExpression*>& strategies,
int strategyNr,
StrategyStackManager::StackId pending,
StrategicExecution* taskSibling,
StrategicProcess* other)
{
const Vector<ConditionFragment*>& fragments = rule->getCondition();
int nrFragments = fragments.size();
for (; fragmentNr < nrFragments; ++fragmentNr)
{
ConditionFragment* fragment = fragments[fragmentNr];
if (dynamic_cast<RewriteConditionFragment*>(fragment) != 0)
{
(void) new RewriteTask(searchObject,
rewriteState,
redexIndex,
extensionInfo,
substitutionSoFar,
rule,
fragmentNr,
strategies,
strategyNr,
pending,
taskSibling,
other);
return SURVIVE;
}
else if (AssignmentConditionFragment* acf = dynamic_cast<AssignmentConditionFragment*>(fragment))
{
//
// Make a subcontext, construct and evalutate the instance of R.
//
RewritingContext* newContext = rewriteState->getContext()->
makeSubcontext(acf->makeRhsInstance(*substitutionSoFar), RewritingContext::CONDITION_EVAL);
newContext->reduce();
//
// We transfer, rather than simply add in the rewrite count because MatchProcess may do
// some more rewriting with newContext.
//
searchObject.getContext()->transferCount(*newContext);
//
// We use clone() rather than copy() because newContext will have the wrong copy size.
//
newContext->clone(*substitutionSoFar); // BUG: this seems to be reducing substitution size!
Subproblem* subproblem;
if (acf->matchRoot(*newContext, subproblem))
{
(void) new MatchProcess(rewriteState,
redexIndex,
extensionInfo,
newContext, // MatchProcess takes over ownership of newContext
subproblem, // MatchProcess takes over ownership of subproblem
rule,
fragmentNr,
strategies,
strategyNr,
pending,
taskSibling,
other);
}
else
{
delete subproblem;
delete newContext;
}
return SURVIVE;
}
//
// Since we don't recognize the fragment as needing special treatment, assume it
// is branch free and solve it with a specially created context (to have the correct
// root and substitution) and dummy state stack.
//
RewritingContext* baseContext = rewriteState->getContext();
RewritingContext* solveContext = baseContext->makeSubcontext(baseContext->root());
//
// We use clone() rather than copy() because newContext will have copy size 0.
//
solveContext->clone(*substitutionSoFar);
stack<ConditionState*> dummy;
bool success = fragment->solve(true, *solveContext, dummy);
searchObject.getContext()->addInCount(*solveContext);
if (!success)
{
//
// A fragment failed so this branch of the search is pruned. But the process
// or task that called us should survive to generate other possibilities.
//
delete solveContext;
return SURVIVE;
}
//
// solve() may have bound some protected slots in solveContext as part of
// making instantiations; for example eager copies of variables. These
// must be copied back into substitutionSoFar.
//
substitutionSoFar->copy(*solveContext);
delete solveContext;
}
//
// The condition succeeded so now we need to do the rewrite and resume the strategy.
//
int resultIndex = doRewrite(searchObject, rewriteState, redexIndex, extensionInfo, substitutionSoFar, rule);
if (resultIndex != NONE)
{
(void) new DecompositionProcess(resultIndex, pending, taskSibling, other);
return SURVIVE; // stick around to look for another rewrite
}
return DIE; // only happens when we are aborting
}
|