pr.c 37.1 KB
Newer Older
Alan Mishchenko committed
1 2
/**CFile****************************************************************

Alan Mishchenko committed
3
  FileName    [pr.c]
Alan Mishchenko committed
4 5 6

  SystemName  [ABC: Logic synthesis and verification system.]

Alan Mishchenko committed
7
  PackageName [Proof recording.]
Alan Mishchenko committed
8

Alan Mishchenko committed
9
  Synopsis    [Core procedures of the package.]
Alan Mishchenko committed
10 11 12 13 14 15 16

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

Alan Mishchenko committed
17
  Revision    [$Id: pr.c,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
Alan Mishchenko committed
18 19 20 21 22 23 24

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
25

26
#include "misc/util/abc_global.h"
Alan Mishchenko committed
27
#include "pr.h"
Alan Mishchenko committed
28

29 30 31
ABC_NAMESPACE_IMPL_START


Alan Mishchenko committed
32 33 34 35
////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

Alan Mishchenko committed
36
typedef unsigned lit;
Alan Mishchenko committed
37

Alan Mishchenko committed
38 39
typedef struct Pr_Cls_t_ Pr_Cls_t;
struct Pr_Cls_t_
Alan Mishchenko committed
40
{
Alan Mishchenko committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    unsigned        uTruth;       // interpolant
    void *          pProof;       // the proof node 
//    void *          pAntis;       // the anticedents
    Pr_Cls_t *      pNext;        // the next clause
    Pr_Cls_t *      pNext0;       // the next 0-watch
    Pr_Cls_t *      pNext1;       // the next 0-watch
    int             Id;           // the clause ID
    unsigned        fA     :  1;  // belongs to A
    unsigned        fRoot  :  1;  // original clause
    unsigned        fVisit :  1;  // visited clause
    unsigned        nLits  : 24;  // the number of literals
    lit             pLits[0];     // literals of this clause
};

struct Pr_Man_t_
{
    // general data
Alan Mishchenko committed
58
    int             fProofWrite;  // writes the proof file
Alan Mishchenko committed
59 60 61 62 63 64 65 66 67 68
    int             fProofVerif;  // verifies the proof
    int             nVars;        // the number of variables
    int             nVarsAB;      // the number of global variables
    int             nRoots;       // the number of root clauses
    int             nClauses;     // the number of all clauses
    int             nClausesA;    // the number of clauses of A 
    Pr_Cls_t *      pHead;        // the head clause
    Pr_Cls_t *      pTail;        // the tail clause
    Pr_Cls_t *      pLearnt;      // the tail clause
    Pr_Cls_t *      pEmpty;       // the empty clause
Alan Mishchenko committed
69 70 71 72 73 74 75
    // internal BCP
    int             nRootSize;    // the number of root level assignments
    int             nTrailSize;   // the number of assignments made 
    lit *           pTrail;       // chronological order of assignments (size nVars)
    lit *           pAssigns;     // assignments by variable (size nVars) 
    char *          pSeens;       // temporary mark (size nVars)
    char *          pVarTypes;    // variable type (size nVars) [1=A, 0=B, <0=AB]
Alan Mishchenko committed
76 77 78
    Pr_Cls_t **     pReasons;     // reasons for each assignment (size nVars)          
    Pr_Cls_t **     pWatches;     // watched clauses for each literal (size 2*nVars)          
    int             nVarsAlloc;   // the allocated size of arrays
Alan Mishchenko committed
79
    // proof recording
Alan Mishchenko committed
80
    void *          pManProof;    // proof manager
Alan Mishchenko committed
81
    int             Counter;      // counter of resolved clauses
Alan Mishchenko committed
82 83 84 85
    // memory management
    int             nChunkSize;   // the number of bytes in a chunk
    int             nChunkUsed;   // the number of bytes used in the last chunk
    char *          pChunkLast;   // the last memory chunk
Alan Mishchenko committed
86 87 88 89 90
    // internal verification
    lit *           pResLits;     // the literals of the resolvent   
    int             nResLits;     // the number of literals of the resolvent
    int             nResLitsAlloc;// the number of literals of the resolvent
    // runtime stats
91 92 93 94
    abctime         timeBcp;
    abctime         timeTrace;
    abctime         timeRead;
    abctime         timeTotal;
Alan Mishchenko committed
95 96
};

Alan Mishchenko committed
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
// variable assignments 
static const lit  LIT_UNDEF = 0xffffffff;

// variable/literal conversions (taken from MiniSat)
static inline lit   toLit    (int v)        { return v + v;  }
static inline lit   toLitCond(int v, int c) { return v + v + (c != 0); }
static inline lit   lit_neg  (lit l)        { return l ^ 1;  }
static inline int   lit_var  (lit l)        { return l >> 1; }
static inline int   lit_sign (lit l)        { return l & 1;  }
static inline int   lit_print(lit l)        { return lit_sign(l)? -lit_var(l)-1 : lit_var(l)+1; }
static inline lit   lit_read (int s)        { return s > 0 ? toLit(s-1) : lit_neg(toLit(-s-1)); }
static inline int   lit_check(lit l, int n) { return l >= 0 && lit_var(l) < n;                  }

// iterators through the clauses
#define Pr_ManForEachClause( p, pCls )        for( pCls = p->pHead; pCls; pCls = pCls->pNext )
#define Pr_ManForEachClauseRoot( p, pCls )    for( pCls = p->pHead; pCls != p->pLearnt; pCls = pCls->pNext )
#define Pr_ManForEachClauseLearnt( p, pCls )  for( pCls = p->pLearnt; pCls; pCls = pCls->pNext )

// static procedures
static char *     Pr_ManMemoryFetch( Pr_Man_t * p, int nBytes );
static void       Pr_ManMemoryStop( Pr_Man_t * p );
static void       Pr_ManResize( Pr_Man_t * p, int nVarsNew );

// exported procedures
extern Pr_Man_t * Pr_ManAlloc( int nVarsAlloc );
extern void       Pr_ManFree( Pr_Man_t * p );
extern int        Pr_ManAddClause( Pr_Man_t * p, lit * pBeg, lit * pEnd, int fRoot, int fClauseA );
extern int        Pr_ManProofWrite( Pr_Man_t * p );
Alan Mishchenko committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

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

  Synopsis    [Allocate proof manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
141
Pr_Man_t * Pr_ManAlloc( int nVarsAlloc )
Alan Mishchenko committed
142
{
Alan Mishchenko committed
143
    Pr_Man_t * p;
Alan Mishchenko committed
144
    // allocate the manager
Alan Mishchenko committed
145
    p = (Pr_Man_t *)ABC_ALLOC( char, sizeof(Pr_Man_t) );
Alan Mishchenko committed
146 147 148 149 150 151 152
    memset( p, 0, sizeof(Pr_Man_t) );
    // allocate internal arrays
    Pr_ManResize( p, nVarsAlloc? nVarsAlloc : 256 );
    // set the starting number of variables
    p->nVars = 0;
    // memory management
    p->nChunkSize = (1<<16); // use 64K chunks
Alan Mishchenko committed
153 154
    // verification
    p->nResLitsAlloc = (1<<16);
Alan Mishchenko committed
155
    p->pResLits = ABC_ALLOC( lit, p->nResLitsAlloc );
Alan Mishchenko committed
156 157
    // parameters
    p->fProofWrite = 0;
Alan Mishchenko committed
158
    p->fProofVerif = 0;
Alan Mishchenko committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172
    return p;    
}

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

  Synopsis    [Resize proof manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
173
void Pr_ManResize( Pr_Man_t * p, int nVarsNew )
Alan Mishchenko committed
174 175
{
    // check if resizing is needed
Alan Mishchenko committed
176
    if ( p->nVarsAlloc < nVarsNew )
Alan Mishchenko committed
177
    {
Alan Mishchenko committed
178
        int nVarsAllocOld = p->nVarsAlloc;
Alan Mishchenko committed
179 180 181
        // find the new size
        if ( p->nVarsAlloc == 0 )
            p->nVarsAlloc = 1;
Alan Mishchenko committed
182
        while ( p->nVarsAlloc < nVarsNew ) 
Alan Mishchenko committed
183 184
            p->nVarsAlloc *= 2;
        // resize the arrays
Alan Mishchenko committed
185 186 187 188 189 190
        p->pTrail    = ABC_REALLOC(lit,        p->pTrail,    p->nVarsAlloc );
        p->pAssigns  = ABC_REALLOC(lit,        p->pAssigns,  p->nVarsAlloc );
        p->pSeens    = ABC_REALLOC(char,       p->pSeens,    p->nVarsAlloc );
        p->pVarTypes = ABC_REALLOC(char,       p->pVarTypes, p->nVarsAlloc );
        p->pReasons  = ABC_REALLOC(Pr_Cls_t *, p->pReasons,  p->nVarsAlloc );
        p->pWatches  = ABC_REALLOC(Pr_Cls_t *, p->pWatches,  p->nVarsAlloc*2 );
191
        // clean the free space
Alan Mishchenko committed
192 193 194 195 196
        memset( p->pAssigns  + nVarsAllocOld, 0xff, sizeof(lit) * (p->nVarsAlloc - nVarsAllocOld) );
        memset( p->pSeens    + nVarsAllocOld, 0, sizeof(char) * (p->nVarsAlloc - nVarsAllocOld) );
        memset( p->pVarTypes + nVarsAllocOld, 0, sizeof(char) * (p->nVarsAlloc - nVarsAllocOld) );
        memset( p->pReasons  + nVarsAllocOld, 0, sizeof(Pr_Cls_t *) * (p->nVarsAlloc - nVarsAllocOld) );
        memset( p->pWatches  + nVarsAllocOld, 0, sizeof(Pr_Cls_t *) * (p->nVarsAlloc - nVarsAllocOld)*2 );
Alan Mishchenko committed
197
    }
Alan Mishchenko committed
198 199 200
    // adjust the number of variables
    if ( p->nVars < nVarsNew )
        p->nVars = nVarsNew;
Alan Mishchenko committed
201 202 203 204 205 206 207 208 209 210 211 212 213
}

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

  Synopsis    [Deallocate proof manager.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
214
void Pr_ManFree( Pr_Man_t * p )
Alan Mishchenko committed
215 216
{
    printf( "Runtime stats:\n" );
Alan Mishchenko committed
217 218 219 220
ABC_PRT( "Reading ", p->timeRead  );
ABC_PRT( "BCP     ", p->timeBcp   );
ABC_PRT( "Trace   ", p->timeTrace );
ABC_PRT( "TOTAL   ", p->timeTotal );
Alan Mishchenko committed
221 222

    Pr_ManMemoryStop( p );
Alan Mishchenko committed
223 224 225 226 227 228 229 230
    ABC_FREE( p->pTrail );
    ABC_FREE( p->pAssigns );
    ABC_FREE( p->pSeens );
    ABC_FREE( p->pVarTypes );
    ABC_FREE( p->pReasons );
    ABC_FREE( p->pWatches );
    ABC_FREE( p->pResLits );
    ABC_FREE( p );
Alan Mishchenko committed
231 232
}

Alan Mishchenko committed
233 234 235
/**Function*************************************************************

  Synopsis    [Adds one clause to the watcher list.]
Alan Mishchenko committed
236

Alan Mishchenko committed
237 238 239
  Description []
               
  SideEffects []
Alan Mishchenko committed
240

Alan Mishchenko committed
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
  SeeAlso     []

***********************************************************************/
static inline void Pr_ManWatchClause( Pr_Man_t * p, Pr_Cls_t * pClause, lit Lit )
{
    assert( lit_check(Lit, p->nVars) );
    if ( pClause->pLits[0] == Lit )
        pClause->pNext0 = p->pWatches[lit_neg(Lit)];  
    else
    {
        assert( pClause->pLits[1] == Lit );
        pClause->pNext1 = p->pWatches[lit_neg(Lit)];  
    }
    p->pWatches[lit_neg(Lit)] = pClause;
}
Alan Mishchenko committed
256 257 258

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

Alan Mishchenko committed
259
  Synopsis    [Adds one clause to the manager.]
Alan Mishchenko committed
260 261 262 263 264 265 266 267

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
268
int Pr_ManAddClause( Pr_Man_t * p, lit * pBeg, lit * pEnd, int fRoot, int fClauseA )
Alan Mishchenko committed
269
{
Alan Mishchenko committed
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
    Pr_Cls_t * pClause;
    lit Lit, * i, * j;
    int nSize, VarMax;

    // process the literals
    if ( pBeg < pEnd )
    {
        // insertion sort
        VarMax = lit_var( *pBeg );
        for ( i = pBeg + 1; i < pEnd; i++ )
        {
            Lit = *i;
            VarMax = lit_var(Lit) > VarMax ? lit_var(Lit) : VarMax;
            for ( j = i; j > pBeg && *(j-1) > Lit; j-- )
                *j = *(j-1);
            *j = Lit;
        }
        // make sure there is no duplicated variables
        for ( i = pBeg + 1; i < pEnd; i++ )
            assert( lit_var(*(i-1)) != lit_var(*i) );
        // resize the manager
        Pr_ManResize( p, VarMax+1 );
    }

    // get memory for the clause
    nSize = sizeof(Pr_Cls_t) + sizeof(lit) * (pEnd - pBeg);
    pClause = (Pr_Cls_t *)Pr_ManMemoryFetch( p, nSize );
    memset( pClause, 0, sizeof(Pr_Cls_t) );

    // assign the clause
    assert( !fClauseA || fRoot ); // clause of A is always a root clause
    p->nRoots += fRoot;
    p->nClausesA += fClauseA;
    pClause->Id = p->nClauses++;
    pClause->fA = fClauseA;
    pClause->fRoot = fRoot;
    pClause->nLits = pEnd - pBeg;
    memcpy( pClause->pLits, pBeg, sizeof(lit) * (pEnd - pBeg) );

    // add the clause to the list
    if ( p->pHead == NULL )
        p->pHead = pClause;
    if ( p->pTail == NULL )
        p->pTail = pClause;
    else
    {
        p->pTail->pNext = pClause;
        p->pTail = pClause;
    }

    // mark the first learnt clause
    if ( p->pLearnt == NULL && !pClause->fRoot )
        p->pLearnt = pClause;

    // add the empty clause
    if ( pClause->nLits == 0 )
    {
        if ( p->pEmpty )
            printf( "More than one empty clause!\n" );
        p->pEmpty = pClause;
    }
    return 1;
Alan Mishchenko committed
332 333 334 335
}

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

Alan Mishchenko committed
336
  Synopsis    [Fetches memory.]
Alan Mishchenko committed
337 338 339 340 341 342 343 344

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
345
char * Pr_ManMemoryFetch( Pr_Man_t * p, int nBytes )
Alan Mishchenko committed
346
{
Alan Mishchenko committed
347 348 349
    char * pMem;
    if ( p->pChunkLast == NULL || nBytes > p->nChunkSize - p->nChunkUsed )
    {
Alan Mishchenko committed
350
        pMem = (char *)ABC_ALLOC( char, p->nChunkSize );
Alan Mishchenko committed
351 352 353 354 355 356 357
        *(char **)pMem = p->pChunkLast;
        p->pChunkLast = pMem;
        p->nChunkUsed = sizeof(char *);
    }
    pMem = p->pChunkLast + p->nChunkUsed;
    p->nChunkUsed += nBytes;
    return pMem;
Alan Mishchenko committed
358 359 360 361
}

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

Alan Mishchenko committed
362
  Synopsis    [Frees memory manager.]
Alan Mishchenko committed
363 364 365 366 367 368 369 370

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
371
void Pr_ManMemoryStop( Pr_Man_t * p )
Alan Mishchenko committed
372
{
Alan Mishchenko committed
373 374 375 376
    char * pMem, * pNext;
    if ( p->pChunkLast == NULL )
        return;
    for ( pMem = p->pChunkLast; pNext = *(char **)pMem; pMem = pNext )
Alan Mishchenko committed
377 378
        ABC_FREE( pMem );
    ABC_FREE( pMem );
Alan Mishchenko committed
379
}
Alan Mishchenko committed
380

Alan Mishchenko committed
381
/**Function*************************************************************
Alan Mishchenko committed
382

Alan Mishchenko committed
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
  Synopsis    [Reports memory usage in bytes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Pr_ManMemoryReport( Pr_Man_t * p )
{
    int Total;
    char * pMem, * pNext;
    if ( p->pChunkLast == NULL )
        return 0;
    Total = p->nChunkUsed; 
    for ( pMem = p->pChunkLast; pNext = *(char **)pMem; pMem = pNext )
        Total += p->nChunkSize;
    return Total;
Alan Mishchenko committed
402 403 404 405
}

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

Alan Mishchenko committed
406
  Synopsis    [Records the proof.]
Alan Mishchenko committed
407 408 409 410 411 412 413 414

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
415
void Extra_PrintBinary_( FILE * pFile, unsigned Sign[], int nBits )
Alan Mishchenko committed
416
{
Alan Mishchenko committed
417 418
    int Remainder, nWords;
    int w, i;
Alan Mishchenko committed
419

Alan Mishchenko committed
420 421
    Remainder = (nBits%(sizeof(unsigned)*8));
    nWords    = (nBits/(sizeof(unsigned)*8)) + (Remainder>0);
Alan Mishchenko committed
422

Alan Mishchenko committed
423 424 425 426
    for ( w = nWords-1; w >= 0; w-- )
        for ( i = ((w == nWords-1 && Remainder)? Remainder-1: 31); i >= 0; i-- )
            fprintf( pFile, "%c", '0' + (int)((Sign[w] & (1<<i)) > 0) );
}
Alan Mishchenko committed
427 428 429

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

Alan Mishchenko committed
430
  Synopsis    [Prints the interpolant for one clause.]
Alan Mishchenko committed
431 432 433 434 435 436 437 438

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
439
void Pr_ManPrintInterOne( Pr_Man_t * p, Pr_Cls_t * pClause )
Alan Mishchenko committed
440
{
Alan Mishchenko committed
441 442 443
    printf( "Clause %2d :  ", pClause->Id );
    Extra_PrintBinary_( stdout, &pClause->uTruth, (1 << p->nVarsAB) );
    printf( "\n" );
Alan Mishchenko committed
444 445 446
}


Alan Mishchenko committed
447

Alan Mishchenko committed
448 449 450 451 452 453 454 455 456 457 458
/**Function*************************************************************

  Synopsis    [Records implication.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
459
static inline int Pr_ManEnqueue( Pr_Man_t * p, lit Lit, Pr_Cls_t * pReason )
Alan Mishchenko committed
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
{
    int Var = lit_var(Lit);
    if ( p->pAssigns[Var] != LIT_UNDEF )
        return p->pAssigns[Var] == Lit;
    p->pAssigns[Var] = Lit;
    p->pReasons[Var] = pReason;
    p->pTrail[p->nTrailSize++] = Lit;
//printf( "assigning var %d value %d\n", Var, !lit_sign(Lit) );
    return 1;
}

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

  Synopsis    [Records implication.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
482
static inline void Pr_ManCancelUntil( Pr_Man_t * p, int Level )
Alan Mishchenko committed
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
{
    lit Lit;
    int i, Var;
    for ( i = p->nTrailSize - 1; i >= Level; i-- )
    {
        Lit = p->pTrail[i];
        Var = lit_var( Lit );
        p->pReasons[Var] = NULL;
        p->pAssigns[Var] = LIT_UNDEF;
//printf( "cancelling var %d\n", Var );
    }
    p->nTrailSize = Level;
}

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

  Synopsis    [Propagate one assignment.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
508
static inline Pr_Cls_t * Pr_ManPropagateOne( Pr_Man_t * p, lit Lit )
Alan Mishchenko committed
509
{
Alan Mishchenko committed
510
    Pr_Cls_t ** ppPrev, * pCur, * pTemp;
Alan Mishchenko committed
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
    lit LitF = lit_neg(Lit);
    int i;
    // iterate through the literals
    ppPrev = p->pWatches + Lit;
    for ( pCur = p->pWatches[Lit]; pCur; pCur = *ppPrev )
    {
        // make sure the false literal is in the second literal of the clause
        if ( pCur->pLits[0] == LitF )
        {
            pCur->pLits[0] = pCur->pLits[1];
            pCur->pLits[1] = LitF;
            pTemp = pCur->pNext0;
            pCur->pNext0 = pCur->pNext1;
            pCur->pNext1 = pTemp;
        }
        assert( pCur->pLits[1] == LitF );

        // if the first literal is true, the clause is satisfied
        if ( pCur->pLits[0] == p->pAssigns[lit_var(pCur->pLits[0])] )
        {
            ppPrev = &pCur->pNext1;
            continue;
        }

        // look for a new literal to watch
        for ( i = 2; i < (int)pCur->nLits; i++ )
        {
            // skip the case when the literal is false
            if ( lit_neg(pCur->pLits[i]) == p->pAssigns[lit_var(pCur->pLits[i])] )
                continue;
            // the literal is either true or unassigned - watch it
            pCur->pLits[1] = pCur->pLits[i];
            pCur->pLits[i] = LitF;
            // remove this clause from the watch list of Lit
            *ppPrev = pCur->pNext1;
            // add this clause to the watch list of pCur->pLits[i] (now it is pCur->pLits[1])
Alan Mishchenko committed
547
            Pr_ManWatchClause( p, pCur, pCur->pLits[1] );
Alan Mishchenko committed
548 549 550 551 552 553
            break;
        }
        if ( i < (int)pCur->nLits ) // found new watch
            continue;

        // clause is unit - enqueue new implication
Alan Mishchenko committed
554
        if ( Pr_ManEnqueue(p, pCur->pLits[0], pCur) )
Alan Mishchenko committed
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
        {
            ppPrev = &pCur->pNext1;
            continue;
        }

        // conflict detected - return the conflict clause
        return pCur;
    }
    return NULL;
}

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

  Synopsis    [Propagate the current assignments.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
577
Pr_Cls_t * Pr_ManPropagate( Pr_Man_t * p, int Start )
Alan Mishchenko committed
578
{
Alan Mishchenko committed
579
    Pr_Cls_t * pClause;
Alan Mishchenko committed
580
    int i;
581
    abctime clk = Abc_Clock();
Alan Mishchenko committed
582 583
    for ( i = Start; i < p->nTrailSize; i++ )
    {
Alan Mishchenko committed
584
        pClause = Pr_ManPropagateOne( p, p->pTrail[i] );
Alan Mishchenko committed
585 586
        if ( pClause )
        {
587
p->timeBcp += Abc_Clock() - clk;
Alan Mishchenko committed
588 589 590
            return pClause;
        }
    }
591
p->timeBcp += Abc_Clock() - clk;
Alan Mishchenko committed
592 593 594 595 596 597
    return NULL;
}


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

Alan Mishchenko committed
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
  Synopsis    [Prints the clause.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Pr_ManPrintClause( Pr_Cls_t * pClause )
{
    int i;
    printf( "Clause ID = %d. Proof = %d. {", pClause->Id, (int)pClause->pProof );
    for ( i = 0; i < (int)pClause->nLits; i++ )
        printf( " %d", pClause->pLits[i] );
    printf( " }\n" );
}

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

  Synopsis    [Prints the resolvent.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Pr_ManPrintResolvent( lit * pResLits, int nResLits )
{
    int i;
    printf( "Resolvent: {" );
    for ( i = 0; i < nResLits; i++ )
        printf( " %d", pResLits[i] );
    printf( " }\n" );
}

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

Alan Mishchenko committed
638 639 640 641 642 643 644 645 646
  Synopsis    [Writes one root clause into a file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
647
void Pr_ManProofWriteOne( Pr_Man_t * p, Pr_Cls_t * pClause )
Alan Mishchenko committed
648
{
Alan Mishchenko committed
649
    pClause->pProof = (void *)++p->Counter;
Alan Mishchenko committed
650 651 652 653

    if ( p->fProofWrite )
    {
        int v;
654
        fprintf( (FILE *)p->pManProof, "%d", (int)pClause->pProof );
Alan Mishchenko committed
655
        for ( v = 0; v < (int)pClause->nLits; v++ )
656 657
            fprintf( (FILE *)p->pManProof, " %d", lit_print(pClause->pLits[v]) );
        fprintf( (FILE *)p->pManProof, " 0 0\n" );
Alan Mishchenko committed
658 659 660 661 662 663 664 665 666 667 668 669 670 671
    }
}

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

  Synopsis    [Traces the proof for one clause.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
672
int Pr_ManProofTraceOne( Pr_Man_t * p, Pr_Cls_t * pConflict, Pr_Cls_t * pFinal )
Alan Mishchenko committed
673
{
Alan Mishchenko committed
674
    Pr_Cls_t * pReason;
Alan Mishchenko committed
675 676
    int i, v, Var, PrevId;
    int fPrint = 0;
677
    abctime clk = Abc_Clock();
Alan Mishchenko committed
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694

    // collect resolvent literals
    if ( p->fProofVerif )
    {
        assert( (int)pConflict->nLits <= p->nResLitsAlloc );
        memcpy( p->pResLits, pConflict->pLits, sizeof(lit) * pConflict->nLits );
        p->nResLits = pConflict->nLits;
    }

    // mark all the variables in the conflict as seen
    for ( v = 0; v < (int)pConflict->nLits; v++ )
        p->pSeens[lit_var(pConflict->pLits[v])] = 1;

    // start the anticedents
//    pFinal->pAntis = Vec_PtrAlloc( 32 );
//    Vec_PtrPush( pFinal->pAntis, pConflict );

Alan Mishchenko committed
695 696 697
    if ( p->nClausesA )
        pFinal->uTruth = pConflict->uTruth;
   
Alan Mishchenko committed
698
    // follow the trail backwards
Alan Mishchenko committed
699
    PrevId = (int)pConflict->pProof;
Alan Mishchenko committed
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
    for ( i = p->nTrailSize - 1; i >= 0; i-- )
    {
        // skip literals that are not involved
        Var = lit_var(p->pTrail[i]);
        if ( !p->pSeens[Var] )
            continue;
        p->pSeens[Var] = 0;

        // skip literals of the resulting clause
        pReason = p->pReasons[Var];
        if ( pReason == NULL )
            continue;
        assert( p->pTrail[i] == pReason->pLits[0] );

        // add the variables to seen
        for ( v = 1; v < (int)pReason->nLits; v++ )
            p->pSeens[lit_var(pReason->pLits[v])] = 1;


        // record the reason clause
Alan Mishchenko committed
720
        assert( pReason->pProof > 0 );
Alan Mishchenko committed
721 722
        p->Counter++;
        if ( p->fProofWrite )
723
            fprintf( (FILE *)p->pManProof, "%d * %d %d 0\n", p->Counter, PrevId, (int)pReason->pProof );
Alan Mishchenko committed
724 725
        PrevId = p->Counter;

Alan Mishchenko committed
726
        if ( p->nClausesA )
Alan Mishchenko committed
727 728
        {
            if ( p->pVarTypes[Var] == 1 ) // var of A
Alan Mishchenko committed
729
                pFinal->uTruth |= pReason->uTruth;
Alan Mishchenko committed
730
            else
Alan Mishchenko committed
731
                pFinal->uTruth &= pReason->uTruth;
Alan Mishchenko committed
732 733 734 735 736 737 738
        }
 
        // resolve the temporary resolvent with the reason clause
        if ( p->fProofVerif )
        {
            int v1, v2; 
            if ( fPrint )
Alan Mishchenko committed
739
                Pr_ManPrintResolvent( p->pResLits, p->nResLits );
Alan Mishchenko committed
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
            // check that the var is present in the resolvent
            for ( v1 = 0; v1 < p->nResLits; v1++ )
                if ( lit_var(p->pResLits[v1]) == Var )
                    break;
            if ( v1 == p->nResLits )
                printf( "Recording clause %d: Cannot find variable %d in the temporary resolvent.\n", pFinal->Id, Var );
            if ( p->pResLits[v1] != lit_neg(pReason->pLits[0]) )
                printf( "Recording clause %d: The resolved variable %d is in the wrong polarity.\n", pFinal->Id, Var );
            // remove this variable from the resolvent
            assert( lit_var(p->pResLits[v1]) == Var );
            p->nResLits--;
            for ( ; v1 < p->nResLits; v1++ )
                p->pResLits[v1] = p->pResLits[v1+1];
            // add variables of the reason clause
            for ( v2 = 1; v2 < (int)pReason->nLits; v2++ )
            {
                for ( v1 = 0; v1 < p->nResLits; v1++ )
                    if ( lit_var(p->pResLits[v1]) == lit_var(pReason->pLits[v2]) )
                        break;
                // if it is a new variable, add it to the resolvent
                if ( v1 == p->nResLits ) 
                {
                    if ( p->nResLits == p->nResLitsAlloc )
Alan Mishchenko committed
763
                        printf( "Recording clause %d: Ran out of space for intermediate resolvent.\n, pFinal->Id" );
Alan Mishchenko committed
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
                    p->pResLits[ p->nResLits++ ] = pReason->pLits[v2];
                    continue;
                }
                // if the variable is the same, the literal should be the same too
                if ( p->pResLits[v1] == pReason->pLits[v2] )
                    continue;
                // the literal is different
                printf( "Recording clause %d: Trying to resolve the clause with more than one opposite literal.\n", pFinal->Id );
            }
        }

//        Vec_PtrPush( pFinal->pAntis, pReason );
    }

    // unmark all seen variables
//    for ( i = p->nTrailSize - 1; i >= 0; i-- )
//        p->pSeens[lit_var(p->pTrail[i])] = 0;
    // check that the literals are unmarked
//    for ( i = p->nTrailSize - 1; i >= 0; i-- )
//        assert( p->pSeens[lit_var(p->pTrail[i])] == 0 );

    // use the resulting clause to check the correctness of resolution
    if ( p->fProofVerif )
    {
        int v1, v2; 
        if ( fPrint )
Alan Mishchenko committed
790
            Pr_ManPrintResolvent( p->pResLits, p->nResLits );
Alan Mishchenko committed
791 792 793 794 795 796 797 798 799 800 801 802
        for ( v1 = 0; v1 < p->nResLits; v1++ )
        {
            for ( v2 = 0; v2 < (int)pFinal->nLits; v2++ )
                if ( pFinal->pLits[v2] == p->pResLits[v1] )
                    break;
            if ( v2 < (int)pFinal->nLits )
                continue;
            break;
        }
        if ( v1 < p->nResLits )
        {
            printf( "Recording clause %d: The final resolvent is wrong.\n", pFinal->Id );
Alan Mishchenko committed
803 804 805
            Pr_ManPrintClause( pConflict );
            Pr_ManPrintResolvent( p->pResLits, p->nResLits );
            Pr_ManPrintClause( pFinal );
Alan Mishchenko committed
806 807
        }
    }
808
p->timeTrace += Abc_Clock() - clk;
Alan Mishchenko committed
809 810

    // return the proof pointer 
Alan Mishchenko committed
811
    if ( p->nClausesA )
Alan Mishchenko committed
812
    {
Alan Mishchenko committed
813
        Pr_ManPrintInterOne( p, pFinal );
Alan Mishchenko committed
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
    }
    return p->Counter;
}

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

  Synopsis    [Records the proof for one clause.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
829
int Pr_ManProofRecordOne( Pr_Man_t * p, Pr_Cls_t * pClause )
Alan Mishchenko committed
830
{
Alan Mishchenko committed
831
    Pr_Cls_t * pConflict;
Alan Mishchenko committed
832 833 834 835 836 837 838 839 840 841 842
    int i;

    // empty clause never ends up there
    assert( pClause->nLits > 0 );
    if ( pClause->nLits == 0 )
        printf( "Error: Empty clause is attempted.\n" );

    // add assumptions to the trail
    assert( !pClause->fRoot );
    assert( p->nTrailSize == p->nRootSize );
    for ( i = 0; i < (int)pClause->nLits; i++ )
Alan Mishchenko committed
843
        if ( !Pr_ManEnqueue( p, lit_neg(pClause->pLits[i]), NULL ) )
Alan Mishchenko committed
844 845 846 847 848 849
        {
            assert( 0 ); // impossible
            return 0;
        }

    // propagate the assumptions
Alan Mishchenko committed
850
    pConflict = Pr_ManPropagate( p, p->nRootSize );
Alan Mishchenko committed
851 852 853 854 855 856 857
    if ( pConflict == NULL )
    {
        assert( 0 ); // cannot prove
        return 0;
    }

    // construct the proof
Alan Mishchenko committed
858
    pClause->pProof = (void *)Pr_ManProofTraceOne( p, pConflict, pClause );
Alan Mishchenko committed
859 860

    // undo to the root level
Alan Mishchenko committed
861
    Pr_ManCancelUntil( p, p->nRootSize );
Alan Mishchenko committed
862 863 864 865

    // add large clauses to the watched lists
    if ( pClause->nLits > 1 )
    {
Alan Mishchenko committed
866 867
        Pr_ManWatchClause( p, pClause, pClause->pLits[0] );
        Pr_ManWatchClause( p, pClause, pClause->pLits[1] );
Alan Mishchenko committed
868 869 870 871 872
        return 1;
    }
    assert( pClause->nLits == 1 );

    // if the clause proved is unit, add it and propagate
Alan Mishchenko committed
873
    if ( !Pr_ManEnqueue( p, pClause->pLits[0], pClause ) )
Alan Mishchenko committed
874 875 876 877 878 879
    {
        assert( 0 ); // impossible
        return 0;
    }

    // propagate the assumption
Alan Mishchenko committed
880
    pConflict = Pr_ManPropagate( p, p->nRootSize );
Alan Mishchenko committed
881 882 883
    if ( pConflict )
    {
        // construct the proof
Alan Mishchenko committed
884 885
        p->pEmpty->pProof = (void *)Pr_ManProofTraceOne( p, pConflict, p->pEmpty );
        printf( "Found last conflict after adding unit clause number %d!\n", pClause->Id );
Alan Mishchenko committed
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
        return 0;
    }

    // update the root level
    p->nRootSize = p->nTrailSize;
    return 1;
}

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

  Synopsis    [Propagate the root clauses.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
905
int Pr_ManProcessRoots( Pr_Man_t * p )
Alan Mishchenko committed
906
{
Alan Mishchenko committed
907
    Pr_Cls_t * pClause;
Alan Mishchenko committed
908 909 910 911
    int Counter;

    // make sure the root clauses are preceeding the learnt clauses
    Counter = 0;
Alan Mishchenko committed
912
    Pr_ManForEachClause( p, pClause )
Alan Mishchenko committed
913
    {
Alan Mishchenko committed
914 915
        assert( (int)pClause->fA    == (Counter < (int)p->nClausesA) );
        assert( (int)pClause->fRoot == (Counter < (int)p->nRoots)    );
Alan Mishchenko committed
916 917
        Counter++;
    }
Alan Mishchenko committed
918
    assert( p->nClauses == Counter );
Alan Mishchenko committed
919 920

    // make sure the last clause if empty
Alan Mishchenko committed
921
    assert( p->pTail->nLits == 0 );
Alan Mishchenko committed
922 923 924

    // go through the root unit clauses
    p->nTrailSize = 0;
Alan Mishchenko committed
925
    Pr_ManForEachClauseRoot( p, pClause )
Alan Mishchenko committed
926 927 928 929
    {
        // create watcher lists for the root clauses
        if ( pClause->nLits > 1 )
        {
Alan Mishchenko committed
930 931
            Pr_ManWatchClause( p, pClause, pClause->pLits[0] );
            Pr_ManWatchClause( p, pClause, pClause->pLits[1] );
Alan Mishchenko committed
932 933 934 935 936
        }
        // empty clause and large clauses
        if ( pClause->nLits != 1 )
            continue;
        // unit clause
Alan Mishchenko committed
937 938
        assert( lit_check(pClause->pLits[0], p->nVars) );
        if ( !Pr_ManEnqueue( p, pClause->pLits[0], pClause ) )
Alan Mishchenko committed
939 940
        {
            // detected root level conflict
Alan Mishchenko committed
941
            printf( "Pr_ManProcessRoots(): Detected a root-level conflict\n" );
Alan Mishchenko committed
942 943 944 945 946 947
            assert( 0 );
            return 0;
        }
    }

    // propagate the root unit clauses
Alan Mishchenko committed
948
    pClause = Pr_ManPropagate( p, 0 );
Alan Mishchenko committed
949 950 951
    if ( pClause )
    {
        // detected root level conflict
Alan Mishchenko committed
952 953
        printf( "Pr_ManProcessRoots(): Detected a root-level conflict\n" );
        assert( 0 );
Alan Mishchenko committed
954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
        return 0;
    }

    // set the root level
    p->nRootSize = p->nTrailSize;
    return 1;
}

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

  Synopsis    [Records the proof.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
973
void Pr_ManPrepareInter( Pr_Man_t * p )
Alan Mishchenko committed
974
{
Alan Mishchenko committed
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
    unsigned uTruths[5] = { 0xAAAAAAAA, 0xCCCCCCCC, 0xF0F0F0F0, 0xFF00FF00, 0xFFFF0000 };
    Pr_Cls_t * pClause;
    int Var, v;

    // mark the variable encountered in the clauses of A
    Pr_ManForEachClauseRoot( p, pClause )
    {
        if ( !pClause->fA )
            break;
        for ( v = 0; v < (int)pClause->nLits; v++ )
            p->pVarTypes[lit_var(pClause->pLits[v])] = 1;
    }

    // check variables that appear in clauses of B
    p->nVarsAB = 0;
    Pr_ManForEachClauseRoot( p, pClause )
    {
        if ( pClause->fA )
            continue;
        for ( v = 0; v < (int)pClause->nLits; v++ )
        {
            Var = lit_var(pClause->pLits[v]);
            if ( p->pVarTypes[Var] == 1 ) // var of A
            {
                // change it into a global variable
                p->nVarsAB++;
                p->pVarTypes[Var] = -1;
            }
        }
    }

    // order global variables
    p->nVarsAB = 0;
    for ( v = 0; v < p->nVars; v++ )
        if ( p->pVarTypes[v] == -1 )
            p->pVarTypes[v] -= p->nVarsAB++;
printf( "There are %d global variables.\n", p->nVarsAB );
Alan Mishchenko committed
1012 1013

    // set interpolants for root clauses
Alan Mishchenko committed
1014
    Pr_ManForEachClauseRoot( p, pClause )
Alan Mishchenko committed
1015 1016 1017
    {
        if ( !pClause->fA ) // clause of B
        {
Alan Mishchenko committed
1018 1019
            pClause->uTruth = ~0;
            Pr_ManPrintInterOne( p, pClause );
Alan Mishchenko committed
1020 1021 1022
            continue;
        }
        // clause of A
Alan Mishchenko committed
1023
        pClause->uTruth = 0;
Alan Mishchenko committed
1024 1025 1026 1027 1028 1029
        for ( v = 0; v < (int)pClause->nLits; v++ )
        {
            Var = lit_var(pClause->pLits[v]);
            if ( p->pVarTypes[Var] < 0 ) // global var
            {
                if ( lit_sign(pClause->pLits[v]) ) // negative var
Alan Mishchenko committed
1030
                    pClause->uTruth |= ~uTruths[ -p->pVarTypes[Var]-1 ];
Alan Mishchenko committed
1031
                else
Alan Mishchenko committed
1032
                    pClause->uTruth |= uTruths[ -p->pVarTypes[Var]-1 ];
Alan Mishchenko committed
1033 1034
            }
        }
Alan Mishchenko committed
1035
        Pr_ManPrintInterOne( p, pClause );
Alan Mishchenko committed
1036 1037 1038 1039 1040
    }
}

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

Alan Mishchenko committed
1041
  Synopsis    [Records the proof.]
Alan Mishchenko committed
1042

Alan Mishchenko committed
1043
  Description []
Alan Mishchenko committed
1044 1045 1046 1047 1048 1049
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
1050
int Pr_ManProofWrite( Pr_Man_t * p )
Alan Mishchenko committed
1051
{
Alan Mishchenko committed
1052
    Pr_Cls_t * pClause;
Alan Mishchenko committed
1053 1054
    int RetValue = 1;

Alan Mishchenko committed
1055 1056
    // propagate root level assignments
    Pr_ManProcessRoots( p );
Alan Mishchenko committed
1057 1058

    // prepare the interpolant computation
Alan Mishchenko committed
1059 1060
    if ( p->nClausesA )
        Pr_ManPrepareInter( p );
Alan Mishchenko committed
1061 1062 1063 1064

    // construct proof for each clause
    // start the proof
    if ( p->fProofWrite )
Alan Mishchenko committed
1065 1066
        p->pManProof = fopen( "proof.cnf_", "w" );
    p->Counter = 0;
Alan Mishchenko committed
1067 1068

    // write the root clauses
Alan Mishchenko committed
1069 1070
    Pr_ManForEachClauseRoot( p, pClause )
        Pr_ManProofWriteOne( p, pClause );
Alan Mishchenko committed
1071

Alan Mishchenko committed
1072 1073
    // consider each learned clause
    Pr_ManForEachClauseLearnt( p, pClause )
Alan Mishchenko committed
1074
    {
Alan Mishchenko committed
1075
        if ( !Pr_ManProofRecordOne( p, pClause ) )
Alan Mishchenko committed
1076
        {
Alan Mishchenko committed
1077 1078
            RetValue = 0;
            break;
Alan Mishchenko committed
1079 1080 1081
        }
    }

Alan Mishchenko committed
1082 1083 1084 1085 1086 1087
    if ( p->nClausesA )
    {
        printf( "Interpolant:  " );
    }


Alan Mishchenko committed
1088 1089 1090
    // stop the proof
    if ( p->fProofWrite )
    {
1091
        fclose( (FILE *)p->pManProof );
Alan Mishchenko committed
1092
        p->pManProof = NULL;    
Alan Mishchenko committed
1093
    }
Alan Mishchenko committed
1094 1095 1096 1097
    return RetValue;
}

/**Function*************************************************************
Alan Mishchenko committed
1098

Alan Mishchenko committed
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
  Synopsis    [Reads clauses from file.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Pr_Man_t * Pr_ManProofRead( char * pFileName )
{
    Pr_Man_t * p = NULL;
    char * pCur, * pBuffer = NULL;
    int * pArray = NULL;
    FILE * pFile;
    int RetValue, Counter, nNumbers, Temp;
    int nClauses, nClausesA, nRoots, nVars;

    // open the file
    pFile = fopen( pFileName, "r" );
    if ( pFile == NULL )
Alan Mishchenko committed
1120
    {
Alan Mishchenko committed
1121 1122 1123 1124 1125
        printf( "Count not open input file \"%s\".\n", pFileName );
        return NULL;
    }

    // read the file
Alan Mishchenko committed
1126
    pBuffer = (char *)ABC_ALLOC( char, (1<<16) );
Alan Mishchenko committed
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
    for ( Counter = 0; fgets( pBuffer, (1<<16), pFile ); )
    {
        if ( pBuffer[0] == 'c' )
            continue;
        if ( pBuffer[0] == 'p' )
        {
            assert( p == NULL );
            nClausesA = 0;
            RetValue = sscanf( pBuffer + 1, "%d %d %d %d", &nVars, &nClauses, &nRoots, &nClausesA );
            if ( RetValue != 3 && RetValue != 4 )
            {
                printf( "Wrong input file format.\n" );
            }
            p = Pr_ManAlloc( nVars );
Alan Mishchenko committed
1141
            pArray = (int *)ABC_ALLOC( char, sizeof(int) * (nVars + 10) );
Alan Mishchenko committed
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
            continue;
        }
        // skip empty lines
        for ( pCur = pBuffer; *pCur; pCur++ )
            if ( !(*pCur == ' ' || *pCur == '\t' || *pCur == '\r' || *pCur == '\n') )
                break;
        if ( *pCur == 0 )
            continue;
        // scan the numbers from file
        nNumbers = 0;
        pCur = pBuffer;
        while ( *pCur )
        {
            // skip spaces
            for ( ; *pCur && *pCur == ' '; pCur++ );
            // read next number
            Temp = 0;
            sscanf( pCur, "%d", &Temp );
            if ( Temp == 0 )
                break;
            pArray[ nNumbers++ ] = lit_read( Temp );
            // skip non-spaces
            for ( ; *pCur && *pCur != ' '; pCur++ );
        }
        // add the clause
1167
        if ( !Pr_ManAddClause( p, (unsigned *)pArray, (unsigned *)pArray + nNumbers, Counter < nRoots, Counter < nClausesA ) )
Alan Mishchenko committed
1168 1169 1170 1171 1172 1173
        {
            printf( "Bad clause number %d.\n", Counter );
            return NULL;
        }
        // count the clauses
        Counter++;
Alan Mishchenko committed
1174
    }
Alan Mishchenko committed
1175 1176 1177 1178 1179
    // check the number of clauses
    if ( Counter != nClauses )
        printf( "Expected %d clauses but read %d.\n", nClauses, Counter );

    // finish
Alan Mishchenko committed
1180 1181
    if ( pArray ) ABC_FREE( pArray );
    if ( pBuffer ) ABC_FREE( pBuffer );
Alan Mishchenko committed
1182 1183 1184 1185 1186
    fclose( pFile );
    return p;
}

/**Function*************************************************************
Alan Mishchenko committed
1187

Alan Mishchenko committed
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
  Synopsis    [Records the proof.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
/*
int Pr_ManProofCount_rec( Pr_Cls_t * pClause )
{
    Pr_Cls_t * pNext;
    int i, Counter;    
    if ( pClause->fRoot )
        return 0;
    if ( pClause->fVisit )
        return 0;
    pClause->fVisit = 1;
    // count the number of visited clauses
    Counter = 1;
1209
    Vec_PtrForEachEntry( Pr_Cls_t *, pClause->pAntis, pNext, i )
Alan Mishchenko committed
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
        Counter += Pr_ManProofCount_rec( pNext );
    return Counter;
}
*/

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

  Synopsis    [Records the proof.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Pr_ManProofTest( char * pFileName )
{
    Pr_Man_t * p;
1229
    abctime clk, clkTotal = Abc_Clock();
Alan Mishchenko committed
1230

1231
clk = Abc_Clock();
Alan Mishchenko committed
1232
    p = Pr_ManProofRead( pFileName );
1233
p->timeRead = Abc_Clock() - clk;
Alan Mishchenko committed
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
    if ( p == NULL )
        return 0;

    Pr_ManProofWrite( p );

    // print stats
/*
    nUsed = Pr_ManProofCount_rec( p->pEmpty );
    printf( "Roots = %d. Learned = %d. Total = %d. Steps = %d.  Ave = %.2f.  Used = %d. Ratio = %.2f. \n", 
        p->nRoots, p->nClauses-p->nRoots, p->nClauses, p->Counter,  
        1.0*(p->Counter-p->nRoots)/(p->nClauses-p->nRoots),
        nUsed, 1.0*nUsed/(p->nClauses-p->nRoots)  );
*/
1247
    printf( "Vars = %d. Roots = %d. Learned = %d. Resol steps = %d.  Ave = %.2f.  Mem = %.2f MB\n", 
Alan Mishchenko committed
1248 1249 1250 1251
        p->nVars, p->nRoots, p->nClauses-p->nRoots, p->Counter,  
        1.0*(p->Counter-p->nRoots)/(p->nClauses-p->nRoots), 
        1.0*Pr_ManMemoryReport(p)/(1<<20) );

1252
p->timeTotal = Abc_Clock() - clkTotal;
Alan Mishchenko committed
1253 1254
    Pr_ManFree( p );
    return 1;
Alan Mishchenko committed
1255 1256
}

Alan Mishchenko committed
1257

Alan Mishchenko committed
1258 1259 1260 1261 1262
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////


1263 1264
ABC_NAMESPACE_IMPL_END