1/*****************************************************************************************[Proof.h]
2MiniSat -- Copyright (c) 2003-2005, Niklas Een, Niklas Sorensson
3
4Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
5associated documentation files (the "Software"), to deal in the Software without restriction,
6including without limitation the rights to use, copy, modify, merge, publish, distribute,
7sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8furnished to do so, subject to the following conditions:
9
10The above copyright notice and this permission notice shall be included in all copies or
11substantial portions of the Software.
12
13THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
14NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
17OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18**************************************************************************************************/
19
20#ifndef Proof_h
21#define Proof_h
22
23#include "SolverTypes.h"
24#include "File.h"
25#ifdef DEBUG
26#include <fstream>
27#endif
28
29//=================================================================================================
30
31
32// A "listner" for the proof. Proof events will be passed onto (online mode) or replayed to
33// (offline mode) this class.  Each call to 'root()' or 'chain()' produces a new clause. The first
34// clause has ID 0, the next 1 and so on. These are the IDs passed to 'chain()'s 'cs' parameter.
35//
36struct ProofTraverser {
37  virtual void root   (const vec<Lit>& c) {}
38  virtual void chain  (const vec<ClauseId>& cs, const vec<Var>& xs) {}
39  virtual void deleted(ClauseId c) {}
40  virtual void done   () {}
41  virtual ~ProofTraverser() {}
42};
43
44
45class Proof {
46    File            fp;
47    cchar*          fp_name;
48    ClauseId        id_counter;
49    ClauseId        root_counter;
50    ProofTraverser* trav;
51
52    vec<Lit>        clause;
53    vec<ClauseId>   chain_id;
54    vec<Var>        chain_var;
55    vec<int64>      c2fp; // c2fp[id] gives position in proof trace of clause with id 'id'
56
57public:
58    Proof();                        // Offline mode -- proof stored to a file, which can be saved, compressed, and/or traversed.
59    Proof(ClauseId goal); // Offline mode -- for pre-initialising c2fp
60    Proof(ProofTraverser& t);       // Online mode -- proof will not be stored.
61
62    void     incRootCount () { root_counter++; }
63    ClauseId addRoot   (vec<Lit>& clause, ClauseId orig_root_id = -1);
64    void     beginChain(ClauseId start);
65    void     resolve   (ClauseId next, Var x);
66    void     resolve   (ClauseId next, Lit p);
67    ClauseId endChain  ();
68    void     deleted   (ClauseId gone);
69    ClauseId last      () { assert(id_counter != ClauseId_NULL); return id_counter - 1; }
70
71#ifdef DEBUG
72    ClauseId parseRoot (vec<Lit>& clause, File& fp, uint64 tmp, std::ofstream* fout = NULL);
73    void     parseChain(vec<ClauseId>& chain_id, vec<Var>&  chain_var, File& fp, uint64 tmp, ClauseId id, std::ofstream* fout = NULL);
74#else
75    ClauseId parseRoot (vec<Lit>& clause, File& fp, uint64 tmp);
76    void     parseChain(vec<ClauseId>& chain_id, vec<Var>&  chain_var, File& fp, uint64 tmp, ClauseId id);
77#endif
78
79    void     compress  (Proof& dst, ClauseId goal = ClauseId_NULL);     // 'dst' should be a newly constructed, empty proof.
80    bool     save      (cchar* filename);
81    void   traverse  (ProofTraverser& trav, int& res_count, ClauseId goal = ClauseId_NULL) ;
82
83};
84
85
86//=================================================================================================
87#endif
88