packages feed

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

-- | Operators of the core term language.
--
-- Every term in the core is an operator applied to a (possibly empty) list
-- of arguments. Constants and declared symbols are nullary operators.
module HSMT.Core.Op
  ( Op (..)
  , Symbol (..)
  , opName
  ) where

import Data.Text (Text)
import qualified Data.Text as T

import HSMT.Core.Float (RoundingMode, rmName)
import HSMT.Core.Sort

-- | A user-declared symbol: a constant (no arguments) or an uninterpreted
-- function.
data Symbol = Symbol
  { symName :: !Text
  , symArgs :: ![Sort]
  , symResult :: !Sort
  }
  deriving (Eq, Ord, Show)

-- | An operator of the core language: an uninterpreted symbol, a literal
-- constant, or a built-in function of one of the supported theories.
data Op
  = -- Uninterpreted symbols
    Uninterpreted !Symbol
  | -- Core (Boolean) theory
    BoolConst !Bool
  | Not
  | And
  | Or
  | Xor
  | Implies
  | Ite
  | Eq
  | Distinct
  | -- Integers
    IntConst !Integer
  | Add
  | Sub
  | Neg
  | Mul
  | IntDiv
    -- ^ Euclidean division, as in SMT-LIB.
  | IntMod
    -- ^ Euclidean remainder, always non-negative.
  | Abs
  | Le
  | Lt
  | Ge
  | Gt
  | -- Bit-vectors
    BVConst !Int !Integer
    -- ^ Width and unsigned value, with @0 <= value < 2^width@.
  | BVNot
  | BVAnd
  | BVOr
  | BVXor
  | BVNeg
  | BVAdd
  | BVSub
  | BVMul
  | BVUDiv
  | BVURem
  | BVSDiv
  | BVSRem
  | BVSMod
  | BVShl
  | BVLShr
  | BVAShr
  | BVConcat
  | BVExtract !Int !Int
    -- ^ @BVExtract hi lo@ selects bits @hi@ down to @lo@ (inclusive).
  | BVZeroExtend !Int
  | BVSignExtend !Int
  | BVULt
  | BVULe
  | BVUGt
  | BVUGe
  | BVSLt
  | BVSLe
  | BVSGt
  | BVSGe
  | -- Arrays
    Select
  | Store
  | ConstArray !Sort
    -- ^ Constant array; the argument is the index sort.
  | -- Strings
    StrConst !Text
  | StrConcat
  | StrLen
  | StrAt
  | StrSubstr
  | StrContains
  | StrPrefixOf
  | StrSuffixOf
  | StrIndexOf
  | StrReplace
  | StrToInt
  | StrFromInt
  | StrLt
  | StrLe
  | -- Regular expressions
    StrToRe
    -- ^ @str.to_re@: the language of one string.
  | StrInRe
    -- ^ @str.in_re@: membership of a string in a language.
  | ReNone
  | ReAll
  | ReAllChar
  | ReConcat
  | ReUnion
  | ReInter
  | ReStar
  | RePlus
  | ReOpt
  | ReRange
  | ReComp
  | ReDiff
  | ReLoop !Int !Int
    -- ^ @(_ re.loop i j)@: between @i@ and @j@ repetitions.
  | RePower !Int
    -- ^ @(_ re.^ n)@: exactly @n@ repetitions.
  | -- Records
    RecordMk ![Text]
    -- ^ Record construction; the field names, in order.
  | RecordGet !Text
  | RecordSet !Text
  | -- Floating point
    FPConst !Int !Int !Integer
    -- ^ Format and bit pattern (NaN is always the canonical pattern, see
    -- 'HSMT.Core.Float.canonicalNaN').
  | RMConst !RoundingMode
  | FPFromFields
    -- ^ The @fp@ constructor: sign, biased exponent and trailing
    -- significand bit-vectors.
  | FPAbs
  | FPNeg
  | FPAdd
  | FPSub
  | FPMul
  | FPDiv
  | FPFma
  | FPSqrt
  | FPRem
  | FPRoundToIntegral
  | FPMin
  | FPMax
  | FPLeq
  | FPLt
  | FPGeq
  | FPGt
  | FPEq
  | FPIsNormal
  | FPIsSubnormal
  | FPIsZero
  | FPIsInfinite
  | FPIsNaN
  | FPIsNegative
  | FPIsPositive
  | ToFP !Int !Int
    -- ^ @(_ to_fp eb sb)@: from a bit pattern (one bit-vector argument),
    -- from another format or from a signed bit-vector (a rounding mode
    -- and the value).
  | ToFPUnsigned !Int !Int
  | FPToUBV !Int
  | FPToSBV !Int
  deriving (Eq, Ord, Show)

-- | SMT-LIB-like name of an operator, used for diagnostics and printing.
opName :: Op -> Text
opName = \case
  Uninterpreted s -> symName s
  BoolConst b -> if b then "true" else "false"
  Not -> "not"
  And -> "and"
  Or -> "or"
  Xor -> "xor"
  Implies -> "=>"
  Ite -> "ite"
  Eq -> "="
  Distinct -> "distinct"
  IntConst n -> T.pack (show n)
  Add -> "+"
  Sub -> "-"
  Neg -> "-"
  Mul -> "*"
  IntDiv -> "div"
  IntMod -> "mod"
  Abs -> "abs"
  Le -> "<="
  Lt -> "<"
  Ge -> ">="
  Gt -> ">"
  BVConst w v -> "(_ bv" <> T.pack (show v) <> " " <> T.pack (show w) <> ")"
  BVNot -> "bvnot"
  BVAnd -> "bvand"
  BVOr -> "bvor"
  BVXor -> "bvxor"
  BVNeg -> "bvneg"
  BVAdd -> "bvadd"
  BVSub -> "bvsub"
  BVMul -> "bvmul"
  BVUDiv -> "bvudiv"
  BVURem -> "bvurem"
  BVSDiv -> "bvsdiv"
  BVSRem -> "bvsrem"
  BVSMod -> "bvsmod"
  BVShl -> "bvshl"
  BVLShr -> "bvlshr"
  BVAShr -> "bvashr"
  BVConcat -> "concat"
  BVExtract h l -> "(_ extract " <> T.pack (show h) <> " " <> T.pack (show l) <> ")"
  BVZeroExtend k -> "(_ zero_extend " <> T.pack (show k) <> ")"
  BVSignExtend k -> "(_ sign_extend " <> T.pack (show k) <> ")"
  BVULt -> "bvult"
  BVULe -> "bvule"
  BVUGt -> "bvugt"
  BVUGe -> "bvuge"
  BVSLt -> "bvslt"
  BVSLe -> "bvsle"
  BVSGt -> "bvsgt"
  BVSGe -> "bvsge"
  Select -> "select"
  Store -> "store"
  ConstArray _ -> "const"
  StrConst s -> T.pack (show s)
  StrConcat -> "str.++"
  StrLen -> "str.len"
  StrAt -> "str.at"
  StrSubstr -> "str.substr"
  StrContains -> "str.contains"
  StrPrefixOf -> "str.prefixof"
  StrSuffixOf -> "str.suffixof"
  StrIndexOf -> "str.indexof"
  StrReplace -> "str.replace"
  StrToInt -> "str.to_int"
  StrFromInt -> "str.from_int"
  StrLt -> "str.<"
  StrLe -> "str.<="
  StrToRe -> "str.to_re"
  StrInRe -> "str.in_re"
  ReNone -> "re.none"
  ReAll -> "re.all"
  ReAllChar -> "re.allchar"
  ReConcat -> "re.++"
  ReUnion -> "re.union"
  ReInter -> "re.inter"
  ReStar -> "re.*"
  RePlus -> "re.+"
  ReOpt -> "re.opt"
  ReRange -> "re.range"
  ReComp -> "re.comp"
  ReDiff -> "re.diff"
  ReLoop i j -> "(_ re.loop " <> T.pack (show i) <> " " <> T.pack (show j) <> ")"
  RePower n -> "(_ re.^ " <> T.pack (show n) <> ")"
  RecordMk _ -> "record"
  RecordGet f -> "get." <> f
  RecordSet f -> "set." <> f
  FPConst eb sb x -> "(fp " <> T.pack (show (eb, sb, x)) <> ")"
  RMConst rm -> rmName rm
  FPFromFields -> "fp"
  FPAbs -> "fp.abs"
  FPNeg -> "fp.neg"
  FPAdd -> "fp.add"
  FPSub -> "fp.sub"
  FPMul -> "fp.mul"
  FPDiv -> "fp.div"
  FPFma -> "fp.fma"
  FPSqrt -> "fp.sqrt"
  FPRem -> "fp.rem"
  FPRoundToIntegral -> "fp.roundToIntegral"
  FPMin -> "fp.min"
  FPMax -> "fp.max"
  FPLeq -> "fp.leq"
  FPLt -> "fp.lt"
  FPGeq -> "fp.geq"
  FPGt -> "fp.gt"
  FPEq -> "fp.eq"
  FPIsNormal -> "fp.isNormal"
  FPIsSubnormal -> "fp.isSubnormal"
  FPIsZero -> "fp.isZero"
  FPIsInfinite -> "fp.isInfinite"
  FPIsNaN -> "fp.isNaN"
  FPIsNegative -> "fp.isNegative"
  FPIsPositive -> "fp.isPositive"
  ToFP eb sb -> "(_ to_fp " <> T.pack (show eb) <> " " <> T.pack (show sb) <> ")"
  ToFPUnsigned eb sb -> "(_ to_fp_unsigned " <> T.pack (show eb) <> " " <> T.pack (show sb) <> ")"
  FPToUBV w -> "(_ fp.to_ubv " <> T.pack (show w) <> ")"
  FPToSBV w -> "(_ fp.to_sbv " <> T.pack (show w) <> ")"