summaryrefslogtreecommitdiffstats
path: root/src/sat/bsat/satProof.c
blob: b3cf2b6ce27e0112d34727aa3c79c52923de1ebd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/**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 $]

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

#include "satSolver.h"
#include "vec.h"
#include "aig.h"

ABC_NAMESPACE_IMPL_START


/*
    Proof is represented as a vector of integers.
    The first entry is -1.
    The clause is represented as an offset in this array.
    One clause's entry is <size><label><ant1><ant2>...<antN>
    Label is initialized to 0.
    Root clauses are 1-based. They are marked by prepending bit 1;
*/

////////////////////////////////////////////////////////////////////////
///                        DECLARATIONS                              ///
////////////////////////////////////////////////////////////////////////

typedef struct Sat_Set_t_ Sat_Set_t;
struct Sat_Set_t_
{
    int nEnts;
    int Label;
    int pEnts[0];
};

static inline int          Sat_SetCheck( Vec_Int_t * p, Sat_Set_t * pNode ) { return (int *)pNode > Vec_IntArray(p) && (int *)pNode < Vec_IntLimit(p); }
static inline int          Sat_SetId( Vec_Int_t * p, Sat_Set_t * pNode )    { return (int *)pNode - Vec_IntArray(p);                                   }
static inline Sat_Set_t *  Sat_SetFromId( Vec_Int_t * p, int i )             { return (Sat_Set_t *)(Vec_IntArray(p) + i);                              }
static inline int          Sat_SetSize( Sat_Set_t * pNode )                 { return pNode->nEnts + 2;          }

#define Sat_PoolForEachSet( p, pNode, i )  \
    for ( i = 1; (i < Vec_IntSize(p)) && ((pNode) = Sat_SetFromId(p, Vec_IntEntry(p,i))); i += Sat_SetSize(pNode) )
#define Sat_SetForEachSet( p, pSet, pNode, i )  \
    for ( i = 0; (i < pSet->nEnts) && (((pNode) = ((pSet->pEnts[i] & 1) ? NULL : Sat_SetFromId(p, pSet->pEnts[i]))), 1); i++ )
#define Sat_VecForEachSet( pVec, p, pNode, i )  \
    for ( i = 0; (i < Vec_IntSize(pVec)) && ((pNode) = Sat_SetFromId(p, Vec_IntEntry(pVec,i))); i++ )

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

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

  Synopsis    [Recursively visits useful proof nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
int Sat_ProofReduceOne( Vec_Int_t * p, Sat_Set_t * pNode, int * pnSize, Vec_Int_t * vStack )
{
    Sat_Set_t * pNext;
    int i, NodeId;
    if ( pNode->Label )
        return pNode->Label;
    // start with node
    pNode->Label = 1;
    Vec_IntPush( vStack, Sat_SetId(p, pNode) );
    // perform DFS search
    while ( Vec_IntSize(vStack) )
    {
        NodeId = Vec_IntPop( vStack );
        if ( NodeId & 1 ) // extrated second time
        {
            pNode = Sat_SetFromId( p, NodeId ^ 1 );
            pNode->Label = *pnSize;
            *pnSize += Sat_SetSize(pNode);
            // update fanins
            Sat_SetForEachSet( p, pNode, pNext, i )
                if ( pNext )
                    pNode->pEnts[i] = pNext->Label;
            continue;
        }
        // extracted first time
        // add second time
        Vec_IntPush( vStack, NodeId ^ 1 );
        // add its anticedents
        pNode = Sat_SetFromId( p, NodeId );
        Sat_SetForEachSet( p, pNode, pNext, i )
            if ( pNext && !pNext->Label )
            {
                pNext->Label = 1;
                Vec_IntPush( vStack, Sat_SetId(p, pNode) ); // add first time
            }
    }
    return pNode->Label;
}

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

  Synopsis    [Recursively visits useful proof nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
/*
int Sat_ProofReduce_rec( Vec_Int_t * p, Sat_Set_t * pNode, int * pnSize )
{
    int * pBeg;
    assert( Sat_SetCheck(p, pNode) );
    if ( pNode->Label )
        return pNode->Label;
    for ( pBeg = pNode->pEnts; pBeg < pNode->pEnts + pNode->nEnts; pBeg++ )
        if ( !(*pBeg & 1) )
            *pBeg = Sat_ProofReduce_rec( p, Sat_SetFromId(p, *pBeg), pnSize );
    pNode->Label = *pnSize;
    *pnSize += Sat_SetSize(pNode);
    return pNode->Label;
}
*/

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

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

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

  SeeAlso     []

***********************************************************************/
void Sat_ProofReduce( Vec_Int_t * p, Vec_Int_t * vRoots )
{
    int i, nSize = 1;
    int * pBeg, * pEnd, * pNew;
    Vec_Int_t * vStack;
    Sat_Set_t * pNode;
    // mark used nodes
    vStack = Vec_IntAlloc( 1000 );
    Sat_VecForEachSet( vRoots, p, pNode, i )
        vRoots->pArray[i] = Sat_ProofReduceOne( p, pNode, &nSize, vStack );
    Vec_IntFree( vStack );
    // compact proof
    pNew = Vec_IntArray(p) + 1;
    Sat_PoolForEachSet( p, pNode, i )
    {
        if ( !pNode->Label )
            continue;
        assert( pNew - Vec_IntArray(p) == pNode->Label );
        pNode->Label = 0;
        pBeg = (int *)pNode;
        pEnd = pBeg + Sat_SetSize(pNode);
        while ( pBeg < pEnd )
            *pNew++ = *pBeg++;
    }
    // report the result
    printf( "The proof was reduced from %d to %d (by %6.2f %%)\n", 
        Vec_IntSize(p), nSize, 100.0 * (Vec_IntSize(p) - nSize) / Vec_IntSize(p) );
    assert( pNew - Vec_IntArray(p) == nSize );
    Vec_IntShrink( p, nSize );
}   

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

  Synopsis    [Recursively visits useful proof nodes.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Sat_ProofLabel( Vec_Int_t * p, Sat_Set_t * pNode, Vec_Int_t * vUsed, Vec_Int_t * vStack )
{
    Sat_Set_t * pNext;
    int i, NodeId;
    if ( pNode->Label )
        return;
    // start with node
    pNode->Label = 1;
    Vec_IntPush( vStack, Sat_SetId(p, pNode) );
    // perform DFS search
    while ( Vec_IntSize(vStack) )
    {
        NodeId = Vec_IntPop( vStack );
        if ( NodeId & 1 ) // extrated second time
        {
            Vec_IntPush( vUsed, NodeId ^ 1 );
            continue;
        }
        // extracted first time
        // add second time
        Vec_IntPush( vStack, NodeId ^ 1 );
        // add its anticedents
        pNode = Sat_SetFromId( p, NodeId );
        Sat_SetForEachSet( p, pNode, pNext, i )
            if ( pNext && !pNext->Label )
            {
                pNext->Label = 1;
                Vec_IntPush( vStack, Sat_SetId(p, pNode) ); // add first time
            }
    }
}

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

  Synopsis    [Computes UNSAT core.]

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

  SeeAlso     []

***********************************************************************/
Vec_Int_t * Sat_ProofCore( Vec_Int_t * p, int nRoots, int * pBeg, int * pEnd )
{
    unsigned * pSeen;
    Vec_Int_t * vCore, * vUsed, * vStack;
    Sat_Set_t * pNode;
    int i;
    // collect visited clauses
    vUsed = Vec_IntAlloc( 1000 );
    vStack = Vec_IntAlloc( 1000 );
    while ( pBeg < pEnd )
        Sat_ProofLabel( p, Sat_SetFromId(p, *pBeg++), vUsed, vStack );
    Vec_IntFree( vStack );
    // find the core
    vCore = Vec_IntAlloc( 1000 );
    pSeen = ABC_CALLOC( unsigned, Aig_BitWordNum(nRoots) );
    Sat_VecForEachSet( vUsed, p, pNode, i )
    {
        pNode->Label = 0;
        for ( pBeg = pNode->pEnts; pBeg < pNode->pEnts + pNode->nEnts; pBeg++ )
            if ( (*pBeg & 1) && !Aig_InfoHasBit(pBeg, *pBeg>>1) )
            {
                Aig_InfoSetBit( pBeg, *pBeg>>1 );
                Vec_IntPush( vCore, (*pBeg>>1)-1 );
            }
    }
    Vec_IntFree( vUsed );
    ABC_FREE( pSeen );
    return vCore;
}


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

  Synopsis    [Performs one resultion step.]

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

  SeeAlso     []

***********************************************************************/
Sat_Set_t * Sat_ProofResolve( Vec_Int_t * p, Sat_Set_t * c1, Sat_Set_t * c2 )
{
    int i, k, Id, Var = -1, Count = 0;
    // find resolution variable
    for ( i = 0; i < c1->nEnts; i++ )
    for ( k = 0; k < 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" );
        return NULL;
    }
    if ( Count > 1 )
    {
        printf( "Found more than 1 resolution variables\n" );
        return NULL;
    }
    // perform resolution
    Id = Vec_IntSize( p );
    Vec_IntPush( p, 0 ); // placeholder
    Vec_IntPush( p, 0 );
    for ( i = 0; i < c1->nEnts; i++ )
    {
        if ( (c1->pEnts[i] >> 1) == Var )
            continue;
        for ( k = Id + 2; k < Vec_IntSize(p); k++ )
            if ( p->pArray[k] == c1->pEnts[i] )
                break;
        if ( k == Vec_IntSize(p) )
            Vec_IntPush( p, c1->pEnts[i] );
    }
    for ( i = 0; i < c2->nEnts; i++ )
    {
        if ( (c2->pEnts[i] >> 1) == Var )
            continue;
        for ( k = Id + 2; k < Vec_IntSize(p); k++ )
            if ( p->pArray[k] == c2->pEnts[i] )
                break;
        if ( k == Vec_IntSize(p) )
            Vec_IntPush( p, c2->pEnts[i] );
    }
    Vec_IntWriteEntry( p, Id, Vec_IntSize(p) - Id - 2 );
    return Sat_SetFromId( p, Id );
}

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

  Synopsis    [Checks the proof for consitency.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Sat_Set_t * Sat_ProofCheckGetOne( Vec_Int_t * vClauses, Vec_Int_t * vProof, Vec_Int_t * vResolves, int iAnt )
{
    Sat_Set_t * pAnt;
    if ( iAnt & 1 )
        return Sat_SetFromId( vClauses, iAnt >> 1 );
    pAnt = Sat_SetFromId( vProof, iAnt );
    return Sat_SetFromId( vResolves, pAnt->Label );
}

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

  Synopsis    [Checks the proof for consitency.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Sat_ProofCheck( Vec_Int_t * vClauses, Vec_Int_t * vProof, int iRoot )
{
    Vec_Int_t * vOrigs, * vStack, * vUsed, * vResolves;
    Sat_Set_t * pSet, * pSet0, * pSet1;
    int i, k;
    // collect original clauses
    vOrigs = Vec_IntAlloc( 1000 );
    Vec_IntPush( vOrigs, -1 );
    Sat_PoolForEachSet( vClauses, pSet, i )
        Vec_IntPush( vOrigs, Sat_SetId(vClauses, pSet) );
    // collect visited clauses
    vUsed = Vec_IntAlloc( 1000 );
    vStack = Vec_IntAlloc( 1000 );
    Sat_ProofLabel( vProof, Sat_SetFromId(vProof, iRoot), vUsed, vStack );
    Vec_IntFree( vStack );
    // perform resolution steps
    vResolves = Vec_IntAlloc( 1000 );
    Vec_IntPush( vResolves, -1 );
    Sat_VecForEachSet( vUsed, vProof, pSet, i )
    {
        pSet0 = Sat_ProofCheckGetOne( vOrigs, vProof, vResolves, pSet->pEnts[0] );
        for ( k = 1; k < pSet->nEnts; k++ )
        {
            pSet1 = Sat_ProofCheckGetOne( vOrigs, vProof, vResolves, pSet->pEnts[k] );
            pSet0 = Sat_ProofResolve( vResolves, pSet0, pSet1 );
        }
        pSet->Label = Sat_SetId( vResolves, pSet0 );
    }
    // clean the proof
    Sat_VecForEachSet( vUsed, vProof, pSet, i )
        pSet->Label = 0;
    // compare the final clause
    if ( pSet0->nEnts > 0 )
        printf( "Cound not derive the empty clause\n" );
    Vec_IntFree( vResolves );
    Vec_IntFree( vUsed );
    Vec_IntFree( vOrigs );
}


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

  Synopsis    [Creates variable map.]

  Description [1=A, 2=B, neg=global]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
void Sat_ProofVarMapCheck( Vec_Int_t * vClauses, Vec_Int_t * vVarMap )
{
    Sat_Set_t * pSet;
    int i, k, fSeeA, fSeeB;
    // make sure all clauses are either A or B
    Sat_PoolForEachSet( vClauses, pSet, i )
    {
        fSeeA = fSeeB = 0;
        for ( k = 0; k < pSet->nEnts; k++ )
        {
            fSeeA += (Vec_IntEntry(vVarMap, pSet->pEnts[k]) == 1);
            fSeeB += (Vec_IntEntry(vVarMap, pSet->pEnts[k]) == 2);
        }
        if ( fSeeA && fSeeB )
            printf( "VarMap error!\n" );
    }
}

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

  Synopsis    [Computes interpolant of the proof.]

  Description [Assuming that res vars are recorded, too...]
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Aig_Man_t * Sat_ProofInterpolant( Vec_Int_t * vProof, int iRoot, Vec_Int_t * vClauses, Vec_Int_t * vVarMap )
{
    Vec_Int_t * vOrigs, * vUsed, * vStack;
    Aig_Man_t * pAig;
    Sat_Set_t * pSet;
    int i;

    Sat_ProofVarMapCheck( vClauses, vVarMap );

    // collect original clauses
    vOrigs = Vec_IntAlloc( 1000 );
    Vec_IntPush( vOrigs, -1 );
    Sat_PoolForEachSet( vClauses, pSet, i )
        Vec_IntPush( vOrigs, Sat_SetId(vClauses, pSet) );

    // collect visited clauses
    vUsed = Vec_IntAlloc( 1000 );
    vStack = Vec_IntAlloc( 1000 );
    Sat_ProofLabel( vProof, Sat_SetFromId(vProof, iRoot), vUsed, vStack );
    Vec_IntFree( vStack );

    // start the AIG
    pAig = Aig_ManStart( 10000 );
    pAig->pName = Aig_UtilStrsav( "interpol" );
    for ( i = 0; i < Vec_IntSize(vVarMap); i++ )
        if ( Vec_IntEntry(vVarMap, i) < 0 )
            Aig_ObjCreatePi( pAig );
 
    return pAig;
}

////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////

ABC_NAMESPACE_IMPL_END