packages feed

hsmt-0.1.0.0: src/HSMT/Core/Term.hs

-- | Hash-consed term DAG.
--
-- Interning maps structurally equal expressions to the same t'TermId', so
-- that decision procedures can compare terms in constant time and share
-- work between occurrences of the same sub-term.
module HSMT.Core.Term
  ( TermId (..)
  , TermNode (..)
  , TermStore
  , emptyStore
  , storeSize
  , intern
  , internNode
  , lookupNode
  , termSort
  , termOp
  , termArgs
  , toExpr
  ) where

import Data.IntMap.Strict (IntMap)
import qualified Data.IntMap.Strict as IntMap
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map

import HSMT.Core.Expr
import HSMT.Core.Op
import HSMT.Core.Sort
import HSMT.Core.Typing

-- | Identifier of an interned term, valid only in the t'TermStore' that
-- created it.
newtype TermId = TermId {unTermId :: Int}
  deriving (Eq, Ord, Show)

-- | A node of the DAG: an operator applied to interned arguments.
data TermNode = TermNode
  { nodeOp :: !Op
  -- ^ The operator.
  , nodeArgs :: ![TermId]
  -- ^ The arguments, in order.
  , nodeSort :: !Sort
  -- ^ The sort of the term, inferred when it was interned.
  }
  deriving (Eq, Show)

-- | The hash-consing table: the nodes indexed by t'TermId' and the reverse
-- index from structure to identifier.
data TermStore = TermStore
  { tsNodes :: !(IntMap TermNode)
  , tsIndex :: !(Map (Op, [TermId]) TermId)
  , tsNext :: !Int
  }

-- | A store with no terms.
emptyStore :: TermStore
emptyStore = TermStore IntMap.empty Map.empty 0

-- | Number of distinct terms in the store.
storeSize :: TermStore -> Int
storeSize = tsNext

-- | Intern an expression, sort-checking every new node.
intern :: Expr -> TermStore -> Either SortError (TermId, TermStore)
intern (App op args) st0 = do
  (ids, st1) <- internAll args st0
  internNode op ids st1
  where
    internAll [] st = Right ([], st)
    internAll (e : es) st = do
      (i, st') <- intern e st
      (is, st'') <- internAll es st'
      pure (i : is, st'')

-- | Intern an operator applied to already interned arguments.
internNode :: Op -> [TermId] -> TermStore -> Either SortError (TermId, TermStore)
internNode op ids st =
  case Map.lookup (op, ids) (tsIndex st) of
    Just tid -> Right (tid, st)
    Nothing -> do
      s <- inferSort op (map (termSort st) ids)
      let tid = TermId (tsNext st)
          node = TermNode op ids s
      Right
        ( tid
        , st
            { tsNodes = IntMap.insert (unTermId tid) node (tsNodes st)
            , tsIndex = Map.insert (op, ids) tid (tsIndex st)
            , tsNext = tsNext st + 1
            }
        )

-- | The node of a term. Calls 'error' if the identifier is not in the store.
lookupNode :: TermStore -> TermId -> TermNode
lookupNode st (TermId i) =
  case IntMap.lookup i (tsNodes st) of
    Just n -> n
    Nothing -> error ("HSMT.Core.Term.lookupNode: unknown term " <> show i)

-- | The sort of a term.
termSort :: TermStore -> TermId -> Sort
termSort st = nodeSort . lookupNode st

-- | The operator at the root of a term.
termOp :: TermStore -> TermId -> Op
termOp st = nodeOp . lookupNode st

-- | The arguments of a term.
termArgs :: TermStore -> TermId -> [TermId]
termArgs st = nodeArgs . lookupNode st

-- | Expand a term back into a tree. This can be exponentially larger than
-- the DAG and is meant for debugging and printing small terms.
toExpr :: TermStore -> TermId -> Expr
toExpr st tid =
  let TermNode op args _ = lookupNode st tid
   in App op (map (toExpr st) args)