source: src/ResolvExpr/SatisfyAssertions.cpp@ 0497b6ba

Last change on this file since 0497b6ba was 597f284, checked in by Peter A. Buhr <pabuhr@…>, 15 months ago

update 'Too many recursive assertions' error message with possible cause

  • Property mode set to 100644
File size: 20.9 KB
RevLine 
[396037d]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// SatisfyAssertions.cpp --
8//
9// Author : Aaron B. Moss
10// Created On : Mon Jun 10 17:45:00 2019
[597f284]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue Jul 2 18:15:51 2024
13// Update Count : 5
[396037d]14//
15
16#include "SatisfyAssertions.hpp"
17
[46da46b]18#include <iostream>
[b69233ac]19#include <algorithm>
[396037d]20#include <cassert>
[b69233ac]21#include <sstream>
22#include <string>
23#include <unordered_map>
24#include <vector>
25
[5bf3976]26#include "AdjustExprType.hpp"
[b69233ac]27#include "Candidate.hpp"
28#include "CandidateFinder.hpp"
[5bf3976]29#include "CommonType.hpp"
[c92bdcc]30#include "Cost.hpp"
31#include "RenameVars.hpp"
[5bf3976]32#include "SpecCost.hpp"
[c92bdcc]33#include "Typeops.hpp"
34#include "Unify.hpp"
[b69233ac]35#include "AST/Decl.hpp"
36#include "AST/Expr.hpp"
37#include "AST/Node.hpp"
38#include "AST/Pass.hpp"
39#include "AST/Print.hpp"
40#include "AST/SymbolTable.hpp"
41#include "AST/TypeEnvironment.hpp"
[c92bdcc]42#include "FindOpenVars.hpp"
43#include "Common/FilterCombos.hpp"
44#include "Common/Indenter.hpp"
45#include "GenPoly/GenPoly.hpp"
46#include "SymTab/Mangler.hpp"
[396037d]47
48namespace ResolvExpr {
49
[b69233ac]50// in CandidateFinder.cpp; unique ID for assertion satisfaction
[fa761c2]51extern ast::UniqueId globalResnSlot;
[b69233ac]52
53namespace {
54 /// Post-unification assertion satisfaction candidate
55 struct AssnCandidate {
56 ast::SymbolTable::IdData cdata; ///< Satisfying declaration
57 ast::ptr< ast::Type > adjType; ///< Satisfying type
58 ast::TypeEnvironment env; ///< Post-unification environment
59 ast::AssertionSet have; ///< Post-unification have-set
60 ast::AssertionSet need; ///< Post-unification need-set
61 ast::OpenVarSet open; ///< Post-unification open-var-set
62 ast::UniqueId resnSlot; ///< Slot for any recursive assertion IDs
63
[aca6a54c]64 AssnCandidate(
65 const ast::SymbolTable::IdData c, const ast::Type * at, ast::TypeEnvironment && e,
[b69233ac]66 ast::AssertionSet && h, ast::AssertionSet && n, ast::OpenVarSet && o, ast::UniqueId rs )
[aca6a54c]67 : cdata( c ), adjType( at ), env( std::move( e ) ), have( std::move( h ) ),
[46da46b]68 need( std::move( n ) ), open( std::move( o ) ), resnSlot( rs ) {
69 if (!have.empty()) {
[da4a570]70 // std::cerr << c.id->location << ':' << c.id->name << std::endl; // I think this was debugging code so I commented it
[46da46b]71 }
72 }
[b69233ac]73 };
74
75 /// List of assertion satisfaction candidates
76 using AssnCandidateList = std::vector< AssnCandidate >;
77
78 /// Reference to a single deferred item
79 struct DeferRef {
[3e5dd913]80 const ast::VariableExpr * expr;
[b69233ac]81 const ast::AssertionSetValue & info;
82 const AssnCandidate & match;
83 };
[aca6a54c]84
85 /// Wrapper for the deferred items from a single assertion satisfaction.
[b69233ac]86 /// Acts like an indexed list of DeferRef
87 struct DeferItem {
[3e5dd913]88 const ast::VariableExpr * expr;
[b69233ac]89 const ast::AssertionSetValue & info;
90 AssnCandidateList matches;
91
[aca6a54c]92 DeferItem(
[3e5dd913]93 const ast::VariableExpr * d, const ast::AssertionSetValue & i, AssnCandidateList && ms )
94 : expr( d ), info( i ), matches( std::move( ms ) ) {}
[b69233ac]95
96 bool empty() const { return matches.empty(); }
97
98 AssnCandidateList::size_type size() const { return matches.size(); }
99
[3e5dd913]100 DeferRef operator[] ( unsigned i ) const { return { expr, info, matches[i] }; }
[b69233ac]101 };
102
103 /// List of deferred satisfaction items
104 using DeferList = std::vector< DeferItem >;
105
106 /// Set of assertion satisfactions, grouped by resolution ID
107 using InferCache = std::unordered_map< ast::UniqueId, ast::InferredParams >;
108
109 /// Lexicographically-ordered vector of costs.
110 /// Lexicographic order comes from default operator< on std::vector.
111 using CostVec = std::vector< Cost >;
112
113 /// Flag for state iteration
114 enum IterateFlag { IterateState };
115
116 /// Intermediate state for satisfying a set of assertions
117 struct SatState {
118 CandidateRef cand; ///< Candidate assertion is rooted on
119 ast::AssertionList need; ///< Assertions to find
120 ast::AssertionSet newNeed; ///< Recursive assertions from current satisfied assertions
121 DeferList deferred; ///< Deferred matches
122 InferCache inferred; ///< Cache of already-inferred assertions
123 CostVec costs; ///< Disambiguating costs of recursive assertion satisfaction
124 ast::SymbolTable symtab; ///< Name lookup (depends on previous assertions)
125
126 /// Initial satisfaction state for a candidate
127 SatState( CandidateRef & c, const ast::SymbolTable & syms )
[aca6a54c]128 : cand( c ), need(), newNeed(), deferred(), inferred(), costs{ Cost::zero },
[b69233ac]129 symtab( syms ) { need.swap( c->need ); }
[aca6a54c]130
[b69233ac]131 /// Update satisfaction state for next step after previous state
132 SatState( SatState && o, IterateFlag )
[aca6a54c]133 : cand( std::move( o.cand ) ), need( o.newNeed.begin(), o.newNeed.end() ), newNeed(),
134 deferred(), inferred( std::move( o.inferred ) ), costs( std::move( o.costs ) ),
[b69233ac]135 symtab( o.symtab ) { costs.emplace_back( Cost::zero ); }
[aca6a54c]136
[b69233ac]137 /// Field-wise next step constructor
138 SatState(
[aca6a54c]139 CandidateRef && c, ast::AssertionSet && nn, InferCache && i, CostVec && cs,
[b69233ac]140 ast::SymbolTable && syms )
[aca6a54c]141 : cand( std::move( c ) ), need( nn.begin(), nn.end() ), newNeed(), deferred(),
[b69233ac]142 inferred( std::move( i ) ), costs( std::move( cs ) ), symtab( std::move( syms ) )
143 { costs.emplace_back( Cost::zero ); }
144 };
145
[0c840fc]146 enum AssertionResult {Fail, Skip, Success} ;
[b69233ac]147
148 /// Binds a single assertion, updating satisfaction state
[aca6a54c]149 void bindAssertion(
[eef8dfb]150 const ast::VariableExpr * expr, const ast::AssertionSetValue & info, CandidateRef & cand,
[b69233ac]151 AssnCandidate & match, InferCache & inferred
152 ) {
153 const ast::DeclWithType * candidate = match.cdata.id;
[aca6a54c]154 assertf( candidate->uniqueId,
[b69233ac]155 "Assertion candidate does not have a unique ID: %s", toString( candidate ).c_str() );
[aca6a54c]156
[46da46b]157 ast::Expr * varExpr = match.cdata.combine( cand->expr->location, cand->cost );
[b69233ac]158 varExpr->result = match.adjType;
159 if ( match.resnSlot ) { varExpr->inferred.resnSlots().emplace_back( match.resnSlot ); }
160
161 // place newly-inferred assertion in proper location in cache
[3e5dd913]162 inferred[ info.resnSlot ][ expr->var->uniqueId ] = ast::ParamEntry{
163 candidate->uniqueId, candidate, match.adjType, expr->result, varExpr };
[b69233ac]164 }
165
166 /// Satisfy a single assertion
[0c840fc]167 AssertionResult satisfyAssertion( ast::AssertionList::value_type & assn, SatState & sat, bool skipUnbound = false) {
[b69233ac]168 // skip unused assertions
[da4a570]169 // static unsigned int cnt = 0; // I think this was debugging code so I commented it
[0c840fc]170 if ( ! assn.second.isUsed ) return AssertionResult::Success;
[b69233ac]171
[da4a570]172 // if (assn.first->var->name[1] == '|') std::cerr << ++cnt << std::endl; // I think this was debugging code so I commented it
[46da46b]173
[b69233ac]174 // find candidates that unify with the desired type
[46da46b]175 AssnCandidateList matches, inexactMatches;
[e5c3811]176
177 std::vector<ast::SymbolTable::IdData> candidates;
[3e5dd913]178 auto kind = ast::SymbolTable::getSpecialFunctionKind(assn.first->var->name);
[e5c3811]179 if (kind != ast::SymbolTable::SpecialFunctionKind::NUMBER_OF_KINDS) {
180 // prefilter special decls by argument type, if already known
[3e5dd913]181 ast::ptr<ast::Type> thisArgType = assn.first->result.strict_as<ast::PointerType>()->base
[e5c3811]182 .strict_as<ast::FunctionType>()->params[0]
183 .strict_as<ast::ReferenceType>()->base;
[24d6572]184 // sat.cand->env.apply(thisArgType);
185
186 if (auto inst = thisArgType.as<ast::TypeInstType>()) {
187 auto cls = sat.cand->env.lookup(*inst);
188 if (cls && cls->bound) thisArgType = cls->bound;
189 }
[e5c3811]190
191 std::string otypeKey = "";
192 if (thisArgType.as<ast::PointerType>()) otypeKey = Mangle::Encoding::pointer;
193 else if (!isUnboundType(thisArgType)) otypeKey = Mangle::mangle(thisArgType, Mangle::Type | Mangle::NoGenericParams);
[0c840fc]194 else if (skipUnbound) return AssertionResult::Skip;
[e5c3811]195
196 candidates = sat.symtab.specialLookupId(kind, otypeKey);
197 }
198 else {
[3e5dd913]199 candidates = sat.symtab.lookupId(assn.first->var->name);
[e5c3811]200 }
201 for ( const ast::SymbolTable::IdData & cdata : candidates ) {
[b69233ac]202 const ast::DeclWithType * candidate = cdata.id;
203
[2fb35df]204 // ignore deleted candidates.
205 // NOTE: this behavior is different from main resolver.
206 // further investigations might be needed to determine
207 // if we should implement the same rule here
208 // (i.e. error if unique best match is deleted)
[5b9a0ae]209 if (candidate->isDeleted && candidate->linkage == ast::Linkage::AutoGen) continue;
[2fb35df]210
[b69233ac]211 // build independent unification context for candidate
212 ast::AssertionSet have, newNeed;
213 ast::TypeEnvironment newEnv{ sat.cand->env };
214 ast::OpenVarSet newOpen{ sat.cand->open };
[3e5dd913]215 ast::ptr< ast::Type > toType = assn.first->result;
[aca6a54c]216 ast::ptr< ast::Type > adjType =
[7583c02]217 renameTyVars( adjustExprType( candidate->get_type(), newEnv, sat.symtab ), GEN_USAGE, false );
[b69233ac]218
219 // only keep candidates which unify
[ef1da0e2]220
221 ast::OpenVarSet closed;
[46da46b]222 // findOpenVars( toType, newOpen, closed, newNeed, have, FirstClosed );
223 findOpenVars( adjType, newOpen, closed, newNeed, have, newEnv, FirstOpen );
224 ast::TypeEnvironment tempNewEnv {newEnv};
225
[24d6572]226 if ( unifyExact( toType, adjType, tempNewEnv, newNeed, have, newOpen, WidenMode {true, true} ) ) {
[46da46b]227 // set up binding slot for recursive assertions
228 ast::UniqueId crntResnSlot = 0;
229 if ( ! newNeed.empty() ) {
230 crntResnSlot = ++globalResnSlot;
231 for ( auto & a : newNeed ) { a.second.resnSlot = crntResnSlot; }
[b69233ac]232 }
[46da46b]233
234 matches.emplace_back(
235 cdata, adjType, std::move( tempNewEnv ), std::move( have ), std::move( newNeed ),
236 std::move( newOpen ), crntResnSlot );
[ef1da0e2]237 }
[46da46b]238 else if ( matches.empty() ) {
239 // restore invalidated env
240 // newEnv = sat.cand->env;
241 // newNeed.clear();
[24d6572]242 if ( auto c = commonType( toType, adjType, newEnv, newNeed, have, newOpen, WidenMode {true, true} ) ) {
[ef1da0e2]243 // set up binding slot for recursive assertions
244 ast::UniqueId crntResnSlot = 0;
245 if ( ! newNeed.empty() ) {
246 crntResnSlot = ++globalResnSlot;
247 for ( auto & a : newNeed ) { a.second.resnSlot = crntResnSlot; }
248 }
[b69233ac]249
[46da46b]250 inexactMatches.emplace_back(
[ef1da0e2]251 cdata, adjType, std::move( newEnv ), std::move( have ), std::move( newNeed ),
252 std::move( newOpen ), crntResnSlot );
253 }
[b69233ac]254 }
255 }
256
257 // break if no satisfying match
[46da46b]258 if ( matches.empty() ) matches = std::move(inexactMatches);
[0c840fc]259 if ( matches.empty() ) return AssertionResult::Fail;
[b69233ac]260
261 // defer if too many satisfying matches
262 if ( matches.size() > 1 ) {
263 sat.deferred.emplace_back( assn.first, assn.second, std::move( matches ) );
[0c840fc]264 return AssertionResult::Success;
[b69233ac]265 }
266
267 // otherwise bind unique match in ongoing scope
268 AssnCandidate & match = matches.front();
[46da46b]269 // addToSymbolTable( match.have, sat.symtab );
[b69233ac]270 sat.newNeed.insert( match.need.begin(), match.need.end() );
271 sat.cand->env = std::move( match.env );
272 sat.cand->open = std::move( match.open );
273
274 bindAssertion( assn.first, assn.second, sat.cand, match, sat.inferred );
[0c840fc]275 return AssertionResult::Success;
[b69233ac]276 }
277
278 /// Map of candidate return types to recursive assertion satisfaction costs
279 using PruneMap = std::unordered_map< std::string, CostVec >;
280
281 /// Gets the pruning key for a candidate (derived from environment-adjusted return type)
282 std::string pruneKey( const Candidate & cand ) {
283 ast::ptr< ast::Type > resType = cand.expr->result;
284 cand.env.apply( resType );
[0026d67]285 return Mangle::mangleType( resType );
[b69233ac]286 }
287
288 /// Associates inferred parameters with an expression
289 struct InferMatcher final {
290 InferCache & inferred;
291
292 InferMatcher( InferCache & inferred ) : inferred( inferred ) {}
293
[81da70a5]294 const ast::Expr * postvisit( const ast::Expr * expr ) {
[b69233ac]295 // Skip if no slots to find
[07d867b]296 if ( !expr->inferred.hasSlots() ) return expr;
297 // if ( expr->inferred.mode != ast::Expr::InferUnion::Slots ) return expr;
[fa761c2]298 std::vector<ast::UniqueId> missingSlots;
[b69233ac]299 // find inferred parameters for resolution slots
[07d867b]300 ast::InferredParams * newInferred = new ast::InferredParams();
[fa761c2]301 for ( ast::UniqueId slot : expr->inferred.resnSlots() ) {
[b69233ac]302 // fail if no matching assertions found
303 auto it = inferred.find( slot );
304 if ( it == inferred.end() ) {
[cdacb73]305 // std::cerr << "missing assertion " << slot << std::endl;
[07d867b]306 missingSlots.push_back(slot);
[81da70a5]307 continue;
[b69233ac]308 }
309
310 // place inferred parameters into new map
311 for ( auto & entry : it->second ) {
312 // recurse on inferParams of resolved expressions
[81da70a5]313 entry.second.expr = postvisit( entry.second.expr );
[07d867b]314 auto res = newInferred->emplace( entry );
[b69233ac]315 assert( res.second && "all assertions newly placed" );
316 }
317 }
318
319 ast::Expr * ret = mutate( expr );
[07d867b]320 ret->inferred.set_inferParams( newInferred );
321 if (!missingSlots.empty()) ret->inferred.resnSlots() = missingSlots;
[b69233ac]322 return ret;
323 }
324 };
325
[aca6a54c]326 /// Replace ResnSlots with InferParams and add alternative to output list, if it meets pruning
[b69233ac]327 /// threshold.
[aca6a54c]328 void finalizeAssertions(
329 CandidateRef & cand, InferCache & inferred, PruneMap & thresholds, CostVec && costs,
330 CandidateList & out
[b69233ac]331 ) {
332 // prune if cheaper alternative for same key has already been generated
333 std::string key = pruneKey( *cand );
334 auto it = thresholds.find( key );
335 if ( it != thresholds.end() ) {
336 if ( it->second < costs ) return;
337 } else {
338 thresholds.emplace_hint( it, key, std::move( costs ) );
339 }
340
341 // replace resolution slots with inferred parameters, add to output
342 ast::Pass< InferMatcher > matcher{ inferred };
343 cand->expr = cand->expr->accept( matcher );
344 out.emplace_back( cand );
345 }
346
[aca6a54c]347 /// Combo iterator that combines candidates into an output list, merging their environments.
348 /// Rejects an appended candidate if environments cannot be merged. See `Common/FilterCombos.h`
[b69233ac]349 /// for description of "combo iterator".
350 class CandidateEnvMerger {
351 /// Current list of merged candidates
352 std::vector< DeferRef > crnt;
353 /// Stack of environments to support backtracking
354 std::vector< ast::TypeEnvironment > envs;
355 /// Stack of open variables to support backtracking
356 std::vector< ast::OpenVarSet > opens;
357 /// Symbol table to use for merges
358 const ast::SymbolTable & symtab;
359
360 public:
361 /// The merged environment/open variables and the list of candidates
362 struct OutType {
363 ast::TypeEnvironment env;
364 ast::OpenVarSet open;
365 std::vector< DeferRef > assns;
366 Cost cost;
367
[aca6a54c]368 OutType(
369 const ast::TypeEnvironment & e, const ast::OpenVarSet & o,
[b69233ac]370 const std::vector< DeferRef > & as, const ast::SymbolTable & symtab )
371 : env( e ), open( o ), assns( as ), cost( Cost::zero ) {
372 // compute combined conversion cost
373 for ( const DeferRef & assn : assns ) {
374 // compute conversion cost from satisfying decl to assertion
[aca6a54c]375 cost += computeConversionCost(
[3e5dd913]376 assn.match.adjType, assn.expr->result, false, symtab, env );
[aca6a54c]377
[b69233ac]378 // mark vars+specialization on function-type assertions
[aca6a54c]379 const ast::FunctionType * func =
[b69233ac]380 GenPoly::getFunctionType( assn.match.cdata.id->get_type() );
381 if ( ! func ) continue;
382
[954c954]383 for ( const auto & param : func->params ) {
384 cost.decSpec( specCost( param ) );
[b69233ac]385 }
[aca6a54c]386
[b69233ac]387 cost.incVar( func->forall.size() );
[aca6a54c]388
[3e5dd913]389 cost.decSpec( func->assertions.size() );
[b69233ac]390 }
391 }
392
393 bool operator< ( const OutType & o ) const { return cost < o.cost; }
394 };
395
[aca6a54c]396 CandidateEnvMerger(
397 const ast::TypeEnvironment & env, const ast::OpenVarSet & open,
[b69233ac]398 const ast::SymbolTable & syms )
399 : crnt(), envs{ env }, opens{ open }, symtab( syms ) {}
400
401 bool append( DeferRef i ) {
402 ast::TypeEnvironment env = envs.back();
403 ast::OpenVarSet open = opens.back();
404 mergeOpenVars( open, i.match.open );
405
[251ce80]406 if ( ! env.combine( i.match.env, open ) ) return false;
[b69233ac]407
408 crnt.emplace_back( i );
409 envs.emplace_back( std::move( env ) );
410 opens.emplace_back( std::move( open ) );
411 return true;
412 }
413
414 void backtrack() {
415 crnt.pop_back();
416 envs.pop_back();
417 opens.pop_back();
418 }
419
420 OutType finalize() { return { envs.back(), opens.back(), crnt, symtab }; }
421 };
422
423 /// Limit to depth of recursion of assertion satisfaction
[1958fec]424 static const int recursionLimit = 8;
[b69233ac]425 /// Maximum number of simultaneously-deferred assertions to attempt concurrent satisfaction of
426 static const int deferLimit = 10;
427} // anonymous namespace
428
[aca6a54c]429void satisfyAssertions(
430 CandidateRef & cand, const ast::SymbolTable & symtab, CandidateList & out,
[396037d]431 std::vector<std::string> & errors
432) {
[b69233ac]433 // finish early if no assertions to satisfy
434 if ( cand->need.empty() ) {
435 out.emplace_back( cand );
436 return;
437 }
438
439 // build list of possible combinations of satisfying declarations
440 std::vector< SatState > sats{ SatState{ cand, symtab } };
441 std::vector< SatState > nextSats{};
442
443 // pruning thresholds by result type of output candidates.
444 // Candidates *should* be generated in sorted order, so no need to retroactively prune
445 PruneMap thresholds;
446
447 // satisfy assertions in breadth-first order over the recursion tree of assertion satisfaction.
448 // Stop recursion at a limited number of levels deep to avoid infinite loops.
449 for ( unsigned level = 0; level < recursionLimit; ++level ) {
450 // for each current mutually-compatible set of assertions
451 for ( SatState & sat : sats ) {
452 // stop this branch if a better option is already found
453 auto it = thresholds.find( pruneKey( *sat.cand ) );
454 if ( it != thresholds.end() && it->second < sat.costs ) goto nextSat;
455
[7583c02]456 // should a limit be imposed? worst case here is O(n^2) but very unlikely to happen.
[ef1da0e2]457
[eef8dfb]458 for (unsigned resetCount = 0; ; ++resetCount) {
[7583c02]459 ast::AssertionList next;
460 // make initial pass at matching assertions
461 for ( auto & assn : sat.need ) {
[34b4268]462 resetTyVarRenaming();
[7583c02]463 // fail early if any assertion is not satisfiable
[0c840fc]464 auto result = satisfyAssertion( assn, sat, !next.empty() );
465 if ( result == AssertionResult::Fail ) {
[ef1da0e2]466 Indenter tabs{ 3 };
467 std::ostringstream ss;
468 ss << tabs << "Unsatisfiable alternative:\n";
469 print( ss, *sat.cand, ++tabs );
470 ss << (tabs-1) << "Could not satisfy assertion:\n";
[0c840fc]471 ast::print( ss, assn.first, tabs );
[ef1da0e2]472
473 errors.emplace_back( ss.str() );
474 goto nextSat;
[2908f08]475 } else if ( result == AssertionResult::Skip ) {
[0c840fc]476 next.emplace_back(assn);
477 // goto nextSat;
478 }
[b69233ac]479 }
[0c840fc]480 // success
481 if (next.empty()) break;
482
[7583c02]483 sat.need = std::move(next);
[b69233ac]484 }
485
486 if ( sat.deferred.empty() ) {
487 // either add successful match or push back next state
488 if ( sat.newNeed.empty() ) {
[aca6a54c]489 finalizeAssertions(
[b69233ac]490 sat.cand, sat.inferred, thresholds, std::move( sat.costs ), out );
491 } else {
492 nextSats.emplace_back( std::move( sat ), IterateState );
493 }
494 } else if ( sat.deferred.size() > deferLimit ) {
495 // too many deferred assertions to attempt mutual compatibility
496 Indenter tabs{ 3 };
497 std::ostringstream ss;
498 ss << tabs << "Unsatisfiable alternative:\n";
499 print( ss, *sat.cand, ++tabs );
500 ss << (tabs-1) << "Too many non-unique satisfying assignments for assertions:\n";
501 for ( const auto & d : sat.deferred ) {
[3e5dd913]502 ast::print( ss, d.expr, tabs );
[b69233ac]503 }
504
505 errors.emplace_back( ss.str() );
506 goto nextSat;
507 } else {
508 // combine deferred assertions by mutual compatibility
509 std::vector< CandidateEnvMerger::OutType > compatible = filterCombos(
510 sat.deferred, CandidateEnvMerger{ sat.cand->env, sat.cand->open, sat.symtab } );
[aca6a54c]511
[b69233ac]512 // fail early if no mutually-compatible assertion satisfaction
513 if ( compatible.empty() ) {
514 Indenter tabs{ 3 };
515 std::ostringstream ss;
516 ss << tabs << "Unsatisfiable alternative:\n";
517 print( ss, *sat.cand, ++tabs );
518 ss << (tabs-1) << "No mutually-compatible satisfaction for assertions:\n";
519 for ( const auto& d : sat.deferred ) {
[3e5dd913]520 ast::print( ss, d.expr, tabs );
[b69233ac]521 }
522
523 errors.emplace_back( ss.str() );
524 goto nextSat;
525 }
526
527 // sort by cost (for overall pruning order)
528 std::sort( compatible.begin(), compatible.end() );
529
530 // process mutually-compatible combinations
531 for ( auto & compat : compatible ) {
532 // set up next satisfaction state
533 CandidateRef nextCand = std::make_shared<Candidate>(
[aca6a54c]534 sat.cand->expr, std::move( compat.env ), std::move( compat.open ),
[b69233ac]535 ast::AssertionSet{} /* need moved into satisfaction state */,
[46da46b]536 sat.cand->cost );
[b69233ac]537
538 ast::AssertionSet nextNewNeed{ sat.newNeed };
539 InferCache nextInferred{ sat.inferred };
[aca6a54c]540
[b69233ac]541 CostVec nextCosts{ sat.costs };
542 nextCosts.back() += compat.cost;
[aca6a54c]543
[b69233ac]544 ast::SymbolTable nextSymtab{ sat.symtab };
545
546 // add compatible assertions to new satisfaction state
547 for ( DeferRef r : compat.assns ) {
548 AssnCandidate match = r.match;
[46da46b]549 // addToSymbolTable( match.have, nextSymtab );
[b69233ac]550 nextNewNeed.insert( match.need.begin(), match.need.end() );
551
[3e5dd913]552 bindAssertion( r.expr, r.info, nextCand, match, nextInferred );
[b69233ac]553 }
554
555 // either add successful match or push back next state
556 if ( nextNewNeed.empty() ) {
[aca6a54c]557 finalizeAssertions(
[b69233ac]558 nextCand, nextInferred, thresholds, std::move( nextCosts ), out );
559 } else {
[aca6a54c]560 nextSats.emplace_back(
561 std::move( nextCand ), std::move( nextNewNeed ),
562 std::move( nextInferred ), std::move( nextCosts ),
[b69233ac]563 std::move( nextSymtab ) );
564 }
565 }
566 }
567 nextSat:; }
568
569 // finish or reset for next round
570 if ( nextSats.empty() ) return;
571 sats.swap( nextSats );
572 nextSats.clear();
573 }
[aca6a54c]574
[b69233ac]575 // exceeded recursion limit if reaches here
576 if ( out.empty() ) {
[597f284]577 SemanticError( cand->expr->location, "Too many recursive assertions, possible cause is circular relationship between a forall assertion and defined function prototype." );
[b69233ac]578 }
[396037d]579}
580
581} // namespace ResolvExpr
582
583// Local Variables: //
584// tab-width: 4 //
585// mode: c++ //
586// compile-command: "make install" //
[aca6a54c]587// End: //
Note: See TracBrowser for help on using the repository browser.