satProof.c 31.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/**CFile****************************************************************

  FileName    [satProof.c]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [SAT solver.]

  Synopsis    [Proof manipulation procedures.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - June 20, 2005.]

  Revision    [$Id: satProof.c,v 1.4 2005/09/16 22:55:03 casem Exp $]

***********************************************************************/

21 22 23 24 25
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

26 27 28
#include "misc/vec/vec.h"
#include "misc/vec/vecSet.h"
#include "aig/aig/aig.h"
29
#include "satTruth.h"
30 31 32 33

ABC_NAMESPACE_IMPL_START

/*
34 35
    Proof is represented as a vector of records.
    A resolution record is represented by a handle (an offset in this vector).
36
    A resolution record entry is <size><label><ant1><ant2>...<antN>
37
    Label is initialized to 0. Root clauses are given by their handles.
38
    They are marked by bitshifting by 2 bits up and setting the LSB to 1
39
*/
40 41 42 43 44 45 46 47 48

typedef struct satset_t satset;
struct satset_t 
{
    unsigned learnt :  1;
    unsigned mark   :  1;
    unsigned partA  :  1;
    unsigned nEnts  : 29;
    int      Id;
49
    int      pEnts[0];
50 51
};

52 53 54 55
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

56 57 58
//static inline satset* Proof_ClauseRead  ( Vec_Int_t* p, int h )     { assert( h > 0 );     return satset_read( (veci *)p, h );    }
//static inline satset* Proof_ClauseRead  ( Vec_Int_t* p, int h )     { assert( h > 0 );     return (satset *)Vec_IntEntryP( p, h );}
static inline satset* Proof_NodeRead    ( Vec_Set_t* p, int h )     { assert( h > 0 );     return (satset*)Vec_SetEntry( p, h );  }
59
static inline int     Proof_NodeWordNum ( int nEnts )               { assert( nEnts > 0 ); return 1 + ((nEnts + 1) >> 1);         }
60

61 62
void Proof_ClauseSetEnts( Vec_Set_t* p, int h, int nEnts )          { Proof_NodeRead(p, h)->nEnts = nEnts;             }

63
// iterating through nodes in the proof
64 65
#define Proof_ForeachClauseVec( pVec, p, pNode, i )          \
    for ( i = 0; (i < Vec_IntSize(pVec)) && ((pNode) = Proof_ClauseRead(p, Vec_IntEntry(pVec,i))); i++ )
66
#define Proof_ForeachNodeVec( pVec, p, pNode, i )            \
67
    for ( i = 0; (i < Vec_IntSize(pVec)) && ((pNode) = Proof_NodeRead(p, Vec_IntEntry(pVec,i))); i++ )
68 69
#define Proof_ForeachNodeVec1( pVec, p, pNode, i )            \
    for ( i = 1; (i < Vec_IntSize(pVec)) && ((pNode) = Proof_NodeRead(p, Vec_IntEntry(pVec,i))); i++ )
70 71

// iterating through fanins of a proof node
72 73
#define Proof_NodeForeachFanin( pProof, pNode, pFanin, i )        \
    for ( i = 0; (i < (int)pNode->nEnts) && (((pFanin) = (pNode->pEnts[i] & 1) ? NULL : Proof_NodeRead(pProof, pNode->pEnts[i] >> 2)), 1); i++ )
Alan Mishchenko committed
74 75 76 77 78 79
/*
#define Proof_NodeForeachLeaf( pClauses, pNode, pLeaf, i )   \
    for ( i = 0; (i < (int)pNode->nEnts) && (((pLeaf) = (pNode->pEnts[i] & 1) ? Proof_ClauseRead(pClauses, pNode->pEnts[i] >> 2) : NULL), 1); i++ )
#define Proof_NodeForeachFaninLeaf( pProof, pClauses, pNode, pFanin, i )    \
    for ( i = 0; (i < (int)pNode->nEnts) && ((pFanin) = (pNode->pEnts[i] & 1) ? Proof_ClauseRead(pClauses, pNode->pEnts[i] >> 2) : Proof_NodeRead(pProof, pNode->pEnts[i] >> 2)); i++ )
*/
80

81 82 83 84 85 86
////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

/**Function*************************************************************

87
  Synopsis    [Cleans collected resultion nodes belonging to the proof.]
88 89 90 91 92 93 94 95

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
96
void Proof_CleanCollected( Vec_Set_t * vProof, Vec_Int_t * vUsed )
97
{
98 99 100 101
    satset * pNode;
    int hNode;
    Proof_ForeachNodeVec( vUsed, vProof, pNode, hNode )
        pNode->Id = 0;
102 103 104 105
}

/**Function*************************************************************

106
  Synopsis    []
107

108
  Description []
109 110 111 112 113 114
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
115
void Proof_CollectUsed_iter( Vec_Set_t * vProof, int hNode, Vec_Int_t * vUsed, Vec_Int_t * vStack )
116
{
117 118
    satset * pNext, * pNode = Proof_NodeRead( vProof, hNode );
    int i;
119 120 121 122
    if ( pNode->Id )
        return;
    // start with node
    pNode->Id = 1;
123
    Vec_IntPush( vStack, hNode << 1 );
124 125
    // perform DFS search
    while ( Vec_IntSize(vStack) )
126
    {
127
        hNode = Vec_IntPop( vStack );
128
        if ( hNode & 1 ) // extracted second time
129 130
        {
            Vec_IntPush( vUsed, hNode >> 1 );
131
            continue;
132 133 134 135
        }
        // extracted first time        
        Vec_IntPush( vStack, hNode ^ 1 ); // add second time
        // add its anticedents        ;
136 137
        pNode = Proof_NodeRead( vProof, hNode >> 1 );
        Proof_NodeForeachFanin( vProof, pNode, pNext, i )
138 139 140
            if ( pNext && !pNext->Id )
            {
                pNext->Id = 1;
141
                Vec_IntPush( vStack, (pNode->pEnts[i] >> 2) << 1 ); // add first time
142
            }
143
    }
144
}
145
Vec_Int_t * Proof_CollectUsedIter( Vec_Set_t * vProof, Vec_Int_t * vRoots, int fSort )
146
{
147
    int fVerify = 0;
148
    Vec_Int_t * vUsed, * vStack;
149
    abctime clk = Abc_Clock();
150
    int i, Entry, iPrev = 0;
151 152
    vUsed = Vec_IntAlloc( 1000 );
    vStack = Vec_IntAlloc( 1000 );
153 154
    Vec_IntForEachEntry( vRoots, Entry, i )
        if ( Entry >= 0 )
155
            Proof_CollectUsed_iter( vProof, Entry, vUsed, vStack );
156
    Vec_IntFree( vStack );
157 158
//    Abc_PrintTime( 1, "Iterative clause collection time", Abc_Clock() - clk );
    clk = Abc_Clock();
159
    Abc_MergeSort( Vec_IntArray(vUsed), Vec_IntSize(vUsed) );
160
//    Abc_PrintTime( 1, "Postprocessing with sorting time", Abc_Clock() - clk );
161
    // verify topological order
162
    if ( fVerify )
163
    {
164 165 166 167 168 169 170 171
        iPrev = 0;
        Vec_IntForEachEntry( vUsed, Entry, i )
        {
            if ( iPrev >= Entry )
                printf( "Out of topological order!!!\n" );
            assert( iPrev < Entry );
            iPrev = Entry;
        }
172
    }
173 174 175 176 177
    return vUsed;
}

/**Function*************************************************************

178
  Synopsis    [Recursively collects useful proof nodes.]
179 180 181 182 183 184 185 186

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
187
void Proof_CollectUsed_rec( Vec_Set_t * vProof, int hNode, Vec_Int_t * vUsed )
188
{
189
    satset * pNext, * pNode = Proof_NodeRead( vProof, hNode );
190 191
    int i;
    if ( pNode->Id )
192
        return;
193
    pNode->Id = 1;
194
    Proof_NodeForeachFanin( vProof, pNode, pNext, i )
195
        if ( pNext && !pNext->Id )
196 197
            Proof_CollectUsed_rec( vProof, pNode->pEnts[i] >> 2, vUsed );
    Vec_IntPush( vUsed, hNode );
198
}
199 200 201 202 203 204 205 206 207 208
Vec_Int_t * Proof_CollectUsedRec( Vec_Set_t * vProof, Vec_Int_t * vRoots )
{
    Vec_Int_t * vUsed;
    int i, Entry;
    vUsed = Vec_IntAlloc( 1000 );
    Vec_IntForEachEntry( vRoots, Entry, i )
        if ( Entry >= 0 )
            Proof_CollectUsed_rec( vProof, Entry, vUsed );
    return vUsed;
}
209 210 211

/**Function*************************************************************

212
  Synopsis    [Recursively visits useful proof nodes.]
213

214
  Description []
215 216 217 218 219 220
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
221
int Proof_MarkUsed_rec( Vec_Set_t * vProof, int hNode )
222
{
223 224 225 226 227 228 229 230 231 232 233 234 235
    satset * pNext, * pNode = Proof_NodeRead( vProof, hNode );
    int i, Counter = 1;
    if ( pNode->Id )
        return 0;
    pNode->Id = 1;
    Proof_NodeForeachFanin( vProof, pNode, pNext, i )
        if ( pNext && !pNext->Id )
            Counter += Proof_MarkUsed_rec( vProof, pNode->pEnts[i] >> 2 );
    return Counter;
}
int Proof_MarkUsedRec( Vec_Set_t * vProof, Vec_Int_t * vRoots )
{
    int i, Entry, Counter = 0;
236 237
    Vec_IntForEachEntry( vRoots, Entry, i )
        if ( Entry >= 0 )
238 239
            Counter += Proof_MarkUsed_rec( vProof, Entry );
    return Counter;
240 241 242 243
}



244 245 246 247 248 249 250 251 252 253 254 255
  
/**Function*************************************************************

  Synopsis    [Checks the validity of the check point.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
256
/*
257
void Sat_ProofReduceCheck_rec( Vec_Set_t * vProof, Vec_Int_t * vClauses, satset * pNode, int hClausePivot, Vec_Ptr_t * vVisited )
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
{
    satset * pFanin;
    int k;
    if ( pNode->Id )
        return;
    pNode->Id = -1;
    Proof_NodeForeachFaninLeaf( vProof, vClauses, pNode, pFanin, k )
        if ( (pNode->pEnts[k] & 1) == 0 ) // proof node
            Sat_ProofReduceCheck_rec( vProof, vClauses, pFanin, hClausePivot, vVisited );
        else // problem clause
            assert( (pNode->pEnts[k] >> 2) < hClausePivot );
    Vec_PtrPush( vVisited, pNode );
}
void Sat_ProofReduceCheckOne( sat_solver2 * s, int iLearnt, Vec_Ptr_t * vVisited )
{
273
    Vec_Set_t * vProof   = (Vec_Set_t *)&s->Proofs;
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
    Vec_Int_t * vClauses = (Vec_Int_t *)&s->clauses;
    Vec_Int_t * vRoots   = (Vec_Int_t *)&s->claProofs;
    int hProofNode = Vec_IntEntry( vRoots, iLearnt );
    satset * pNode = Proof_NodeRead( vProof, hProofNode );
    Sat_ProofReduceCheck_rec( vProof, vClauses, pNode, s->hClausePivot, vVisited );
}
void Sat_ProofReduceCheck( sat_solver2 * s )
{
    Vec_Ptr_t * vVisited;
    satset * c;
    int h, i = 1;
    vVisited = Vec_PtrAlloc( 1000 );
    sat_solver_foreach_learnt( s, c, h )
        if ( h < s->hLearntPivot )
            Sat_ProofReduceCheckOne( s, i++, vVisited );
    Vec_PtrForEachEntry( satset *, vVisited, c, i )
        c->Id = 0;
    Vec_PtrFree( vVisited );
}
293
*/
294

295 296 297 298 299 300 301 302 303 304 305
/**Function*************************************************************

  Synopsis    [Reduces the proof to contain only roots and their children.]

  Description [The result is updated proof and updated roots.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
306
/*
307
void Sat_ProofReduce2( sat_solver2 * s )
308
{
309
    Vec_Set_t * vProof   = (Vec_Set_t *)&s->Proofs;
310
    Vec_Int_t * vRoots   = (Vec_Int_t *)&s->claProofs;
311
    Vec_Int_t * vClauses = (Vec_Int_t *)&s->clauses;
312

313 314
    int fVerbose = 0;
    Vec_Int_t * vUsed;
315
    satset * pNode, * pFanin, * pPivot;
316
    int i, k, hTemp;
317 318
    abctime clk = Abc_Clock();
    static abctime TimeTotal = 0;
319 320

    // collect visited nodes
321
    vUsed = Proof_CollectUsedIter( vProof, vRoots, 1 );
322 323

    // relabel nodes to use smaller space
324
    Vec_SetShrinkS( vProof, 2 );
325 326
    Proof_ForeachNodeVec( vUsed, vProof, pNode, i )
    {
327
        pNode->Id = Vec_SetAppendS( vProof, 2+pNode->nEnts );
328 329
        Proof_NodeForeachFaninLeaf( vProof, vClauses, pNode, pFanin, k )
            if ( (pNode->pEnts[k] & 1) == 0 ) // proof node
330
                pNode->pEnts[k] = (pFanin->Id << 2) | (pNode->pEnts[k] & 2);
331 332
            else // problem clause
                assert( (int*)pFanin >= Vec_IntArray(vClauses) && (int*)pFanin < Vec_IntArray(vClauses)+Vec_IntSize(vClauses) );
333 334
    }
    // update roots
335
    Proof_ForeachNodeVec1( vRoots, vProof, pNode, i )
336
        Vec_IntWriteEntry( vRoots, i, pNode->Id );
337 338 339 340
    // determine new pivot
    assert( s->hProofPivot >= 1 && s->hProofPivot <= Vec_SetHandCurrent(vProof) );
    pPivot = Proof_NodeRead( vProof, s->hProofPivot );
    s->hProofPivot = Vec_SetHandCurrentS(vProof);
341 342 343 344
    // compact the nodes
    Proof_ForeachNodeVec( vUsed, vProof, pNode, i )
    {
        hTemp = pNode->Id; pNode->Id = 0;
345
        memmove( Vec_SetEntry(vProof, hTemp), pNode, sizeof(word)*Proof_NodeWordNum(pNode->nEnts) );
346 347 348 349 350
        if ( pPivot && pPivot <= pNode )
        { 
            s->hProofPivot = hTemp;
            pPivot = NULL;
        }
351
    }
352
    Vec_SetWriteEntryNum( vProof, Vec_IntSize(vUsed) );
353
    Vec_IntFree( vUsed );
354

355 356 357
    // report the result
    if ( fVerbose )
    {
358
        printf( "\n" );
359
        printf( "The proof was reduced from %6.2f MB to %6.2f MB (by %6.2f %%)  ", 
360 361
            1.0 * Vec_SetMemory(vProof) / (1<<20), 1.0 * Vec_SetMemoryS(vProof) / (1<<20), 
            100.0 * (Vec_SetMemory(vProof) - Vec_SetMemoryS(vProof)) / Vec_SetMemory(vProof) );
362
        TimeTotal += Abc_Clock() - clk;
363 364
        Abc_PrintTime( 1, "Time", TimeTotal );
    }
365
    Vec_SetShrink( vProof, Vec_SetHandCurrentS(vProof) );
366
//    Sat_ProofReduceCheck( s );
367
} 
368 369
*/

370

371 372 373 374 375 376 377 378 379 380 381
void Sat_ProofCheck0( Vec_Set_t * vProof )
{
    satset * pNode, * pFanin;
    int i, j, k, nSize;
    Vec_SetForEachEntry( satset *, vProof, nSize, pNode, i, j )
    {
        nSize = Vec_SetWordNum( 2 + pNode->nEnts );
        Proof_NodeForeachFanin( vProof, pNode, pFanin, k )
            assert( (pNode->pEnts[k] >> 2) );
    }
}
382

383
int Sat_ProofReduce( Vec_Set_t * vProof, void * pRoots, int hProofPivot )
384
{
385 386 387 388
//    Vec_Set_t * vProof   = (Vec_Set_t *)&s->Proofs;
//    Vec_Int_t * vRoots   = (Vec_Int_t *)&s->claProofs;
    Vec_Int_t * vRoots   = (Vec_Int_t *)pRoots;
//    Vec_Int_t * vClauses = (Vec_Int_t *)&s->clauses;
389 390 391
    int fVerbose = 0;
    Vec_Ptr_t * vUsed;
    satset * pNode, * pFanin, * pPivot;
392
    int i, j, k, hTemp, nSize;
393 394
    abctime clk = Abc_Clock();
    static abctime TimeTotal = 0;
395
    int RetValue;
396
//Sat_ProofCheck0( vProof );
397

398 399 400
    // collect visited nodes
    nSize = Proof_MarkUsedRec( vProof, vRoots );
    vUsed = Vec_PtrAlloc( nSize );
401
//Sat_ProofCheck0( vProof );
402

403
    // relabel nodes to use smaller space
404
    Vec_SetShrinkS( vProof, 2 );
405 406 407 408 409 410
    Vec_SetForEachEntry( satset *, vProof, nSize, pNode, i, j )
    {
        nSize = Vec_SetWordNum( 2 + pNode->nEnts );
        if ( pNode->Id == 0 ) 
            continue;
        pNode->Id = Vec_SetAppendS( vProof, 2 + pNode->nEnts );
411
        assert( pNode->Id > 0 );
412 413
        Vec_PtrPush( vUsed, pNode );
        // update fanins
414
        Proof_NodeForeachFanin( vProof, pNode, pFanin, k )
415
            if ( (pNode->pEnts[k] & 1) == 0 ) // proof node
416
            {
417
                assert( pFanin->Id > 0 );
418
                pNode->pEnts[k] = (pFanin->Id << 2) | (pNode->pEnts[k] & 2);
419
            }
420 421
//            else // problem clause
//                assert( (int*)pFanin >= Vec_IntArray(vClauses) && (int*)pFanin < Vec_IntArray(vClauses)+Vec_IntSize(vClauses) );
422 423 424 425 426
    }
    // update roots
    Proof_ForeachNodeVec1( vRoots, vProof, pNode, i )
        Vec_IntWriteEntry( vRoots, i, pNode->Id );
    // determine new pivot
427 428 429
    assert( hProofPivot >= 1 && hProofPivot <= Vec_SetHandCurrent(vProof) );
    pPivot = Proof_NodeRead( vProof, hProofPivot );
    RetValue = Vec_SetHandCurrentS(vProof);
430 431 432 433 434 435 436
    // compact the nodes
    Vec_PtrForEachEntry( satset *, vUsed, pNode, i )
    {
        hTemp = pNode->Id; pNode->Id = 0;
        memmove( Vec_SetEntry(vProof, hTemp), pNode, sizeof(word)*Proof_NodeWordNum(pNode->nEnts) );
        if ( pPivot && pPivot <= pNode )
        {
437
            RetValue = hTemp;
438 439 440 441 442
            pPivot = NULL;
        }
    }
    Vec_SetWriteEntryNum( vProof, Vec_PtrSize(vUsed) );
    Vec_PtrFree( vUsed );
443

444 445 446 447
    // report the result
    if ( fVerbose )
    {
        printf( "\n" );
448
        printf( "The proof was reduced from %6.2f MB to %6.2f MB (by %6.2f %%)  ", 
449 450
            1.0 * Vec_SetMemory(vProof) / (1<<20), 1.0 * Vec_SetMemoryS(vProof) / (1<<20), 
            100.0 * (Vec_SetMemory(vProof) - Vec_SetMemoryS(vProof)) / Vec_SetMemory(vProof) );
451
        TimeTotal += Abc_Clock() - clk;
452 453 454 455 456
        Abc_PrintTime( 1, "Time", TimeTotal );
    }
    Vec_SetShrink( vProof, Vec_SetHandCurrentS(vProof) );
    Vec_SetShrinkLimits( vProof );
//    Sat_ProofReduceCheck( s );
457
//Sat_ProofCheck0( vProof );
458

459
    return RetValue;
460
}
461

462
#if 0
463 464 465 466 467 468 469 470 471 472 473 474

/**Function*************************************************************

  Synopsis    [Performs one resultion step.]

  Description [Returns ID of the resolvent if success, and -1 if failure.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
475
int Sat_ProofCheckResolveOne( Vec_Set_t * p, satset * c1, satset * c2, Vec_Int_t * vTemp )
476 477
{
    satset * c;
478
    int h, i, k, Var = -1, Count = 0;
479 480 481 482 483 484 485 486 487 488 489
    // find resolution variable
    for ( i = 0; i < (int)c1->nEnts; i++ )
    for ( k = 0; k < (int)c2->nEnts; k++ )
        if ( (c1->pEnts[i] ^ c2->pEnts[k]) == 1 )
        {
            Var = (c1->pEnts[i] >> 1);
            Count++;
        }
    if ( Count == 0 )
    {
        printf( "Cannot find resolution variable\n" );
490
        return 0;
491 492 493 494
    }
    if ( Count > 1 )
    {
        printf( "Found more than 1 resolution variables\n" );
495
        return 0;
496 497 498 499 500 501 502 503 504 505 506 507
    }
    // perform resolution
    Vec_IntClear( vTemp );
    Vec_IntPush( vTemp, 0 ); // placeholder
    Vec_IntPush( vTemp, 0 );
    for ( i = 0; i < (int)c1->nEnts; i++ )
        if ( (c1->pEnts[i] >> 1) != Var )
            Vec_IntPush( vTemp, c1->pEnts[i] );
    for ( i = 0; i < (int)c2->nEnts; i++ )
        if ( (c2->pEnts[i] >> 1) != Var )
            Vec_IntPushUnique( vTemp, c2->pEnts[i] );
    // create new resolution entry
508
    h = Vec_SetAppend( p, Vec_IntArray(vTemp), Vec_IntSize(vTemp) );
509
    // return the new entry
510
    c = Proof_NodeRead( p, h );
511
    c->nEnts = Vec_IntSize(vTemp)-2;
512
    return h;
513 514 515 516 517 518 519 520 521 522 523 524 525
}

/**Function*************************************************************

  Synopsis    [Checks the proof for consitency.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
526
satset * Sat_ProofCheckReadOne( Vec_Int_t * vClauses, Vec_Set_t * vProof, Vec_Set_t * vResolves, int iAnt )
527 528 529
{
    satset * pAnt;
    if ( iAnt & 1 )
530
        return Proof_ClauseRead( vClauses, iAnt >> 2 );
531 532 533
    assert( iAnt > 0 );
    pAnt = Proof_NodeRead( vProof, iAnt >> 2 );
    assert( pAnt->Id > 0 );
534
    return Proof_NodeRead( vResolves, pAnt->Id );
535 536 537 538 539 540 541 542 543 544 545 546 547
}

/**Function*************************************************************

  Synopsis    [Checks the proof for consitency.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
548
void Sat_ProofCheck( sat_solver2 * s )
549
{
550
    Vec_Int_t * vClauses = (Vec_Int_t *)&s->clauses;
551 552 553
    Vec_Set_t * vProof   = (Vec_Set_t *)&s->Proofs;
    Vec_Int_t Roots = { 1, 1, &s->hProofLast }, * vRoots = &Roots;
    Vec_Set_t * vResolves;
554
    Vec_Int_t * vUsed, * vTemp;
555
    satset * pSet, * pSet0 = NULL, * pSet1;
556
    int i, k, hRoot, Handle, Counter = 0;
557
    abctime clk = Abc_Clock(); 
558 559
    hRoot = s->hProofLast;
    if ( hRoot == -1 )
560
        return;
561
    // collect visited clauses
562
    vUsed = Proof_CollectUsedIter( vProof, vRoots, 1 );
563 564 565
    Proof_CleanCollected( vProof, vUsed );
    // perform resolution steps
    vTemp = Vec_IntAlloc( 1000 );
566
    vResolves = Vec_SetAlloc( 20 );
567 568
    Proof_ForeachNodeVec( vUsed, vProof, pSet, i )
    {
569
        Handle = -1;
570
        pSet0 = Sat_ProofCheckReadOne( vClauses, vProof, vResolves, pSet->pEnts[0] );
571 572
        for ( k = 1; k < (int)pSet->nEnts; k++ )
        {
573 574
            pSet1  = Sat_ProofCheckReadOne( vClauses, vProof, vResolves, pSet->pEnts[k] );
            Handle = Sat_ProofCheckResolveOne( vResolves, pSet0, pSet1, vTemp );
575
            pSet0  = Proof_NodeRead( vResolves, Handle );
576
        }
577
        pSet->Id = Handle;
578 579 580 581 582 583
        Counter++;
    }
    Vec_IntFree( vTemp );
    // clean the proof
    Proof_CleanCollected( vProof, vUsed );
    // compare the final clause
584
    printf( "Used %6.2f MB for resolvents.\n", 1.0 * Vec_SetMemory(vResolves) / (1<<20) );
585
    if ( pSet0->nEnts > 0 )
586
        printf( "Derived clause with %d lits instead of the empty clause.  ", pSet0->nEnts );
587 588
    else
        printf( "Proof verification successful.  " );
589
    Abc_PrintTime( 1, "Time", Abc_Clock() - clk );
590
    // cleanup
591
    Vec_SetFree( vResolves );
592 593
    Vec_IntFree( vUsed );
}
594
#endif
595

596 597 598 599
/**Function*************************************************************

  Synopsis    [Collects nodes belonging to the UNSAT core.]

600
  Description [The resulting array contains 1-based IDs of root clauses.]
601 602 603 604 605 606
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
607
Vec_Int_t * Sat_ProofCollectCore( Vec_Set_t * vProof, Vec_Int_t * vUsed )
608 609 610
{
    Vec_Int_t * vCore;
    satset * pNode, * pFanin;
611 612 613 614 615 616 617 618 619 620
    unsigned * pBitMap;
    int i, k, MaxCla = 0;
    // find the largest number
    Proof_ForeachNodeVec( vUsed, vProof, pNode, i )
        Proof_NodeForeachFanin( vProof, pNode, pFanin, k )
            if ( pFanin == NULL )
                MaxCla = Abc_MaxInt( MaxCla, pNode->pEnts[k] >> 2 );
    // allocate bitmap
    pBitMap = ABC_CALLOC( unsigned, Abc_BitWordNum(MaxCla) + 1 );
    // collect leaves
621 622
    vCore = Vec_IntAlloc( 1000 );
    Proof_ForeachNodeVec( vUsed, vProof, pNode, i )
623 624
        Proof_NodeForeachFanin( vProof, pNode, pFanin, k )
            if ( pFanin == NULL )
625
            {
626 627 628 629 630
                int Entry = (pNode->pEnts[k] >> 2);
                if ( Abc_InfoHasBit(pBitMap, Entry) )
                    continue;
                Abc_InfoSetBit(pBitMap, Entry);
                Vec_IntPush( vCore, Entry );
631
            }
632 633
    ABC_FREE( pBitMap );
//    Vec_IntUniqify( vCore );
634 635 636
    return vCore;
}

637 638
#if 0

639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
/**Function*************************************************************

  Synopsis    [Verifies that variables are labeled correctly.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Sat_ProofInterpolantCheckVars( sat_solver2 * s, Vec_Int_t * vGloVars )
{
    satset* c; 
    Vec_Int_t * vVarMap;
    int i, k, Entry, * pMask;
    int Counts[5] = {0};
    // map variables into their type (A, B, or AB)
    vVarMap = Vec_IntStart( s->size );
    sat_solver_foreach_clause( s, c, i )
        for ( k = 0; k < (int)c->nEnts; k++ )
            *Vec_IntEntryP(vVarMap, lit_var(c->pEnts[k])) |= 2 - c->partA;
    // analyze variables
    Vec_IntForEachEntry( vGloVars, Entry, i )
    {
        pMask = Vec_IntEntryP(vVarMap, Entry);
        assert( *pMask >= 0 && *pMask <= 3 );
        Counts[(*pMask & 3)]++;
        *pMask = 0;
    }
    // count the number of global variables not listed
    Vec_IntForEachEntry( vVarMap, Entry, i )
        if ( Entry == 3 )
            Counts[4]++;
    Vec_IntFree( vVarMap );
    // report
    if ( Counts[0] )
        printf( "Warning: %6d variables listed as global do not appear in clauses (this is normal)\n", Counts[0] );
    if ( Counts[1] )
        printf( "Warning: %6d variables listed as global appear only in A-clauses (this is a BUG)\n", Counts[1] );
    if ( Counts[2] )
        printf( "Warning: %6d variables listed as global appear only in B-clauses (this is a BUG)\n", Counts[2] );
    if ( Counts[3] )
        printf( "Warning: %6d (out of %d) variables listed as global appear in both A- and B-clauses (this is normal)\n", Counts[3], Vec_IntSize(vGloVars) );
    if ( Counts[4] )
        printf( "Warning: %6d variables not listed as global appear in both A- and B-clauses (this is a BUG)\n", Counts[4] );
}
686

687 688 689 690
/**Function*************************************************************

  Synopsis    [Computes interpolant of the proof.]

691
  Description [Aassuming that vars/clause of partA are marked.]
692 693 694 695 696 697
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
698
void * Sat_ProofInterpolant( sat_solver2 * s, void * pGloVars )
699
{
700
    Vec_Int_t * vClauses  = (Vec_Int_t *)&s->clauses;
701
    Vec_Set_t * vProof    = (Vec_Set_t *)&s->Proofs;
702
    Vec_Int_t * vGlobVars = (Vec_Int_t *)pGloVars;
703
    Vec_Int_t Roots = { 1, 1, &s->hProofLast }, * vRoots = &Roots;
704 705 706
    Vec_Int_t * vUsed, * vCore, * vCoreNums, * vVarMap;
    satset * pNode, * pFanin;
    Aig_Man_t * pAig;
707
    Aig_Obj_t * pObj = NULL;
708
    int i, k, iVar, Lit, Entry, hRoot;
709 710 711 712 713
//    if ( s->hLearntLast < 0 )
//        return NULL;
//    hRoot = veci_begin(&s->claProofs)[satset_read(&s->learnts, s->hLearntLast>>1)->Id];
    hRoot = s->hProofLast;
    if ( hRoot == -1 )
714
        return NULL;
715 716

    Sat_ProofInterpolantCheckVars( s, vGlobVars );
717 718

    // collect visited nodes
719
    vUsed = Proof_CollectUsedIter( vProof, vRoots, 1 );
720
    // collect core clauses (cleans vUsed and vCore)
721 722
    vCore = Sat_ProofCollectCore( vProof, vUsed ); 
    // vCore arrived in terms of clause handles
723 724

    // map variables into their global numbers
725
    vVarMap = Vec_IntStartFull( s->size );
726 727 728 729 730
    Vec_IntForEachEntry( vGlobVars, Entry, i )
        Vec_IntWriteEntry( vVarMap, Entry, i );

    // start the AIG
    pAig = Aig_ManStart( 10000 );
731
    pAig->pName = Abc_UtilStrsav( "interpol" );
732
    for ( i = 0; i < Vec_IntSize(vGlobVars); i++ )
733
        Aig_ObjCreateCi( pAig );
734 735 736

    // copy the numbers out and derive interpol for clause
    vCoreNums = Vec_IntAlloc( Vec_IntSize(vCore) );
737
    Proof_ForeachClauseVec( vCore, vClauses, pNode, i )
738 739 740 741
    {
        if ( pNode->partA )
        {
            pObj = Aig_ManConst0( pAig );
742 743 744
            satset_foreach_lit( pNode, Lit, k, 0 )
                if ( (iVar = Vec_IntEntry(vVarMap, lit_var(Lit))) >= 0 )
                    pObj = Aig_Or( pAig, pObj, Aig_NotCond(Aig_IthVar(pAig, iVar), lit_sign(Lit)) );
745 746 747 748 749 750 751 752
        }
        else
            pObj = Aig_ManConst1( pAig );
        // remember the interpolant
        Vec_IntPush( vCoreNums, pNode->Id );
        pNode->Id = Aig_ObjToLit(pObj);
    }
    Vec_IntFree( vVarMap );
753

754 755 756
    // copy the numbers out and derive interpol for resolvents
    Proof_ForeachNodeVec( vUsed, vProof, pNode, i )
    {
757
//        satset_print( pNode );
758 759 760
        assert( pNode->nEnts > 1 );
        Proof_NodeForeachFaninLeaf( vProof, vClauses, pNode, pFanin, k )
        {
761
            assert( pFanin->Id < 2*Aig_ManObjNumMax(pAig) );
762 763 764 765 766 767 768 769 770 771 772
            if ( k == 0 )
                pObj = Aig_ObjFromLit( pAig, pFanin->Id );
            else if ( pNode->pEnts[k] & 2 ) // variable of A
                pObj = Aig_Or( pAig, pObj, Aig_ObjFromLit(pAig, pFanin->Id) );
            else
                pObj = Aig_And( pAig, pObj, Aig_ObjFromLit(pAig, pFanin->Id) );
        }
        // remember the interpolant
        pNode->Id = Aig_ObjToLit(pObj);
    }
    // save the result
773
//    assert( Proof_NodeHandle(vProof, pNode) == hRoot );
774
    Aig_ObjCreateCo( pAig, pObj );
775 776 777
    Aig_ManCleanup( pAig );

    // move the results back
778
    Proof_ForeachClauseVec( vCore, vClauses, pNode, i )
779 780 781 782
        pNode->Id = Vec_IntEntry( vCoreNums, i );
    Proof_ForeachNodeVec( vUsed, vProof, pNode, i )
        pNode->Id = 0;
    // cleanup
783
    Vec_IntFree( vCore );
784
    Vec_IntFree( vUsed );
785 786 787 788
    Vec_IntFree( vCoreNums );
    return pAig;
}

789 790 791 792 793 794 795 796 797 798 799 800 801 802 803

/**Function*************************************************************

  Synopsis    [Computes interpolant of the proof.]

  Description [Aassuming that vars/clause of partA are marked.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
word * Sat_ProofInterpolantTruth( sat_solver2 * s, void * pGloVars )
{
    Vec_Int_t * vClauses  = (Vec_Int_t *)&s->clauses;
804
    Vec_Set_t * vProof    = (Vec_Set_t *)&s->Proofs;
805
    Vec_Int_t * vGlobVars = (Vec_Int_t *)pGloVars;
806
    Vec_Int_t Roots = { 1, 1, &s->hProofLast }, * vRoots = &Roots;
807 808 809 810 811 812
    Vec_Int_t * vUsed, * vCore, * vCoreNums, * vVarMap;
    satset * pNode, * pFanin;
    Tru_Man_t * pTru;
    int nVars = Vec_IntSize(vGlobVars);
    int nWords = (nVars < 6) ? 1 : (1 << (nVars-6));
    word * pRes = ABC_ALLOC( word, nWords );
813
    int i, k, iVar, Lit, Entry, hRoot;
814
    assert( nVars > 0 && nVars <= 16 );
815 816
    hRoot = s->hProofLast;
    if ( hRoot == -1 )
817
        return NULL;
818 819 820 821

    Sat_ProofInterpolantCheckVars( s, vGlobVars );

    // collect visited nodes
822
    vUsed = Proof_CollectUsedIter( vProof, vRoots, 1 );
823
    // collect core clauses (cleans vUsed and vCore)
824 825
    vCore = Sat_ProofCollectCore( vProof, vUsed, 0 );
    // vCore arrived in terms of clause handles
826 827 828 829 830 831 832 833 834 835 836

    // map variables into their global numbers
    vVarMap = Vec_IntStartFull( s->size );
    Vec_IntForEachEntry( vGlobVars, Entry, i )
        Vec_IntWriteEntry( vVarMap, Entry, i );

    // start the AIG
    pTru = Tru_ManAlloc( nVars );

    // copy the numbers out and derive interpol for clause
    vCoreNums = Vec_IntAlloc( Vec_IntSize(vCore) );
837
    Proof_ForeachClauseVec( vCore, vClauses, pNode, i )
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
    {
        if ( pNode->partA )
        {
//            pObj = Aig_ManConst0( pAig );
            Tru_ManClear( pRes, nWords );
            satset_foreach_lit( pNode, Lit, k, 0 )
                if ( (iVar = Vec_IntEntry(vVarMap, lit_var(Lit))) >= 0 )
//                    pObj = Aig_Or( pAig, pObj, Aig_NotCond(Aig_IthVar(pAig, iVar), lit_sign(Lit)) );
                    pRes = Tru_ManOrNotCond( pRes, Tru_ManVar(pTru, iVar), nWords, lit_sign(Lit) );
        }
        else
//            pObj = Aig_ManConst1( pAig );
            Tru_ManFill( pRes, nWords );
        // remember the interpolant
        Vec_IntPush( vCoreNums, pNode->Id );
//        pNode->Id = Aig_ObjToLit(pObj);
        pNode->Id = Tru_ManInsert( pTru, pRes );
    }
    Vec_IntFree( vVarMap );

    // copy the numbers out and derive interpol for resolvents
    Proof_ForeachNodeVec( vUsed, vProof, pNode, i )
    {
//        satset_print( pNode );
        assert( pNode->nEnts > 1 );
        Proof_NodeForeachFaninLeaf( vProof, vClauses, pNode, pFanin, k )
        {
//            assert( pFanin->Id < 2*Aig_ManObjNumMax(pAig) );
866
//            assert( pFanin->Id <= Tru_ManHandleMax(pTru) );
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
            if ( k == 0 )
//                pObj = Aig_ObjFromLit( pAig, pFanin->Id );
                pRes = Tru_ManCopyNotCond( pRes, Tru_ManFunc(pTru, pFanin->Id & ~1), nWords, pFanin->Id & 1 );
            else if ( pNode->pEnts[k] & 2 ) // variable of A
//                pObj = Aig_Or( pAig, pObj, Aig_ObjFromLit(pAig, pFanin->Id) );
                pRes = Tru_ManOrNotCond( pRes, Tru_ManFunc(pTru, pFanin->Id & ~1), nWords, pFanin->Id & 1 );
            else
//                pObj = Aig_And( pAig, pObj, Aig_ObjFromLit(pAig, pFanin->Id) );
                pRes = Tru_ManAndNotCond( pRes, Tru_ManFunc(pTru, pFanin->Id & ~1), nWords, pFanin->Id & 1 );
        }
        // remember the interpolant
//        pNode->Id = Aig_ObjToLit(pObj);
        pNode->Id = Tru_ManInsert( pTru, pRes );
    }
    // save the result
882
//    assert( Proof_NodeHandle(vProof, pNode) == hRoot );
883
//    Aig_ObjCreateCo( pAig, pObj );
884 885 886
//    Aig_ManCleanup( pAig );

    // move the results back
887
    Proof_ForeachClauseVec( vCore, vClauses, pNode, i )
888 889 890 891 892 893 894 895 896 897 898 899
        pNode->Id = Vec_IntEntry( vCoreNums, i );
    Proof_ForeachNodeVec( vUsed, vProof, pNode, i )
        pNode->Id = 0;
    // cleanup
    Vec_IntFree( vCore );
    Vec_IntFree( vUsed );
    Vec_IntFree( vCoreNums );
    Tru_ManFree( pTru );
//    ABC_FREE( pRes );
    return pRes;
}

900 901
#endif

902 903 904 905 906 907 908 909 910 911 912
/**Function*************************************************************

  Synopsis    [Computes UNSAT core.]

  Description [The result is the array of root clause indexes.]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
913
void * Proof_DeriveCore( Vec_Set_t * vProof, int hRoot )
914
{
915
    Vec_Int_t Roots = { 1, 1, &hRoot }, * vRoots = &Roots;
916
    Vec_Int_t * vCore, * vUsed;
917
    if ( hRoot == -1 )
918
        return NULL;
919
    // collect visited clauses
920
    vUsed = Proof_CollectUsedIter( vProof, vRoots, 0 );
921
    // collect core clauses 
922
    vCore = Sat_ProofCollectCore( vProof, vUsed );
923
    Vec_IntFree( vUsed );
924
    Vec_IntSort( vCore, 1 );
925
    return vCore;
926 927
}

928

929 930 931 932 933 934
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////

ABC_NAMESPACE_IMPL_END