hsmt-0.1.0.0: src/HSMT/Core/Sort.hs
-- | Run-time representation of sorts used by the untyped core.
module HSMT.Core.Sort
( Sort (..)
, prettySort
, finiteCardinality
) where
import Data.Text (Text)
import qualified Data.Text as T
-- | Sorts of the core language.
data Sort
= BoolSort
| IntSort
| StringSort
| BVSort !Int
-- ^ Fixed-width bit-vectors; the width is always positive.
| ArraySort Sort Sort
-- ^ @ArraySort index element@.
| RecordSort [(Text, Sort)]
-- ^ Records with named fields, in declaration order.
| UninterpretedSort Text
-- ^ User-declared uninterpreted sort.
| FPSort !Int !Int
-- ^ Floating-point numbers with the given exponent and significand
-- widths (the significand width counts the hidden bit); both are at
-- least 2.
| RoundingModeSort
| RegLanSort
-- ^ Regular languages (SMT-LIB @RegLan@).
deriving (Eq, Ord, Show)
-- | Render a sort using SMT-LIB 2 syntax. Records have no native SMT-LIB
-- syntax, so they are rendered as a quoted symbol describing their fields.
prettySort :: Sort -> Text
prettySort = \case
BoolSort -> "Bool"
IntSort -> "Int"
StringSort -> "String"
BVSort n -> "(_ BitVec " <> tshow n <> ")"
ArraySort i e -> "(Array " <> prettySort i <> " " <> prettySort e <> ")"
RecordSort fs ->
"|Record{" <> T.intercalate "," [f <> ":" <> prettySort s | (f, s) <- fs] <> "}|"
UninterpretedSort n -> n
FPSort eb sb -> "(_ FloatingPoint " <> tshow eb <> " " <> tshow sb <> ")"
RoundingModeSort -> "RoundingMode"
RegLanSort -> "RegLan"
where
tshow = T.pack . show
-- | Number of elements of a sort, when it is finite.
--
-- Uninterpreted sorts are treated as infinite: the solver never assumes a
-- bound on their size.
finiteCardinality :: Sort -> Maybe Integer
finiteCardinality = \case
BoolSort -> Just 2
BVSort n -> Just (2 ^ n)
-- Every NaN pattern stands for the single NaN.
FPSort eb sb -> Just (2 ^ (eb + sb) - 2 * (2 ^ (sb - 1) - 1) + 1)
RoundingModeSort -> Just 5
RecordSort fs -> product <$> traverse (finiteCardinality . snd) fs
ArraySort i e -> do
ci <- finiteCardinality i
ce <- finiteCardinality e
-- Guard against astronomically large exponents.
if ci <= 64 then Just (ce ^ ci) else Nothing
_ -> Nothing