packages feed

hsmt-0.1.0.0: src/HSMT/SAT/Types.hs

-- | Basic types shared by the SAT solver and its clients.
module HSMT.SAT.Types
  ( Var
  , Lit (..)
  , mkLit
  , posLit
  , negLit
  , litVar
  , litSign
  , litNeg
  , LBool (..)
  , SatResult (..)
  , TheoryResult (..)
  , Lemma (..)
  , removable
  , permanent
  , TheoryHooks (..)
  , noTheory
  ) where

-- | Propositional variables are numbered from 0.
type Var = Int

-- | A literal is encoded as @2 * var + sign@, where sign 1 means negated.
newtype Lit = Lit {unLit :: Int}
  deriving (Eq, Ord)

instance Show Lit where
  show l
    | litSign l = "-" <> show (litVar l + 1)
    | otherwise = show (litVar l + 1)

-- | @mkLit v True@ is the negative literal of @v@.
mkLit :: Var -> Bool -> Lit
mkLit v neg = Lit (2 * v + fromEnum neg)
{-# INLINE mkLit #-}

-- | The positive literal of a variable.
posLit :: Var -> Lit
posLit v = Lit (2 * v)
{-# INLINE posLit #-}

-- | The negative literal of a variable.
negLit :: Var -> Lit
negLit v = Lit (2 * v + 1)
{-# INLINE negLit #-}

-- | The variable of a literal.
litVar :: Lit -> Var
litVar (Lit l) = l `div` 2
{-# INLINE litVar #-}

-- | True for negative literals.
litSign :: Lit -> Bool
litSign (Lit l) = odd l
{-# INLINE litSign #-}

-- | The complementary literal (same variable, opposite sign).
litNeg :: Lit -> Lit
litNeg (Lit l) = Lit (if even l then l + 1 else l - 1)
{-# INLINE litNeg #-}

-- | Three-valued Booleans.
data LBool = LTrue | LFalse | LUndef
  deriving (Eq, Show)

-- | Outcome of a call to the SAT solver.
data SatResult
  = Sat
    -- ^ A satisfying assignment was found.
  | Unsat
    -- ^ The clauses are unsatisfiable (under the given assumptions).
  | Unknown
    -- ^ The resource budget was exhausted.
  deriving (Eq, Show)

-- | Answer of a theory solver to the SAT engine.
data TheoryResult
  = TheoryOk
  | TheoryConflict [Lit]
    -- ^ A set of currently true literals that is inconsistent with the
    -- theory. The engine learns the clause made of their negations.
  | TheoryLemmas [Lemma]
    -- ^ Clauses implied by the theory. They may mention fresh variables and
    -- may be unit or conflicting under the current assignment; implied
    -- (propagated) literals must come first in their clause.
  deriving (Eq, Show)

-- | A clause produced by a theory.
data Lemma = Lemma
  { lemmaPermanent :: !Bool
    -- ^ Permanent lemmas are never deleted. Use them for clauses the theory
    -- will not produce again (for instance because they introduce new
    -- atoms); explanations that can be recomputed should be removable.
  , lemmaClause :: [Lit]
    -- ^ The literals of the clause.
  }
  deriving (Eq, Show)

-- | Build a removable or a permanent lemma from a clause.
removable, permanent :: [Lit] -> Lemma
removable = Lemma False
permanent = Lemma True

-- | Callbacks that connect a theory solver to the CDCL engine (DPLL(T)).
--
-- The engine delivers assigned literals in batches, in trail order, through
-- 'thAssert' right before calling 'thPropagate' or 'thFinalCheck'. Every
-- literal delivered after the last 'thNewLevel' belongs to the current
-- decision level.
data TheoryHooks = TheoryHooks
  { thNewLevel :: IO ()
    -- ^ A new decision level was opened.
  , thBacktrack :: Int -> IO ()
    -- ^ Undo everything above the given decision level.
  , thAssert :: Lit -> IO ()
    -- ^ A literal became true.
  , thPropagate :: IO TheoryResult
    -- ^ Cheap consistency check after Boolean propagation.
  , thFinalCheck :: IO TheoryResult
    -- ^ Complete check once every variable is assigned.
  }

-- | Hooks for pure propositional solving.
noTheory :: TheoryHooks
noTheory =
  TheoryHooks
    { thNewLevel = pure ()
    , thBacktrack = \_ -> pure ()
    , thAssert = \_ -> pure ()
    , thPropagate = pure TheoryOk
    , thFinalCheck = pure TheoryOk
    }