{-# LANGUAGE FlexibleInstances         #-}
{-# LANGUAGE FlexibleContexts          #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE OverloadedStrings         #-}
{-# LANGUAGE UndecidableInstances      #-}


-- | This module contains the types defining an SMTLIB2 interface.

module Language.Fixpoint.Smt.Types (

    -- * Serialized Representation
    --    symbolBuilder

    -- * Commands
      Command  (..)

    -- * Responses
    , Response (..)

    -- * Typeclass for SMTLIB2 conversion
    , SMTLIB2 (..)
    , runSmt2

    -- * SMTLIB2 Process Context
    , Context (..)

    ) where

import           Data.ByteString.Builder (Builder)
import           Language.Fixpoint.Types
import qualified Data.Text                as T
import           Text.PrettyPrint.HughesPJ
import qualified SMTLIB.Backends

import           System.IO                (Handle)
-- import           Language.Fixpoint.Misc   (traceShow)

--------------------------------------------------------------------------------
-- | Types ---------------------------------------------------------------------
--------------------------------------------------------------------------------

-- symbolBuilder :: Symbol -> LT.Builder
-- symbolBuilder = LT.fromText . symbolSafeText

-- | Commands issued to SMT engine
data Command      = Push
                  | Pop
                  | Exit
                  | SetMbqi
                  | CheckSat
                  | DeclData ![DataDecl]
                  | Declare  T.Text [SmtSort] !SmtSort
                  | Define   !Sort
                  | DefineFunc Symbol [(Symbol, SmtSort)] !SmtSort Expr
                  | Assert   !(Maybe Int) !Expr
                  | AssertAx !(Triggered Expr)
                  | Distinct [Expr] -- {v:[Expr] | 2 <= len v}
                  | GetValue [Symbol]
                  | CMany    [Command]
                  deriving (Command -> Command -> Bool
(Command -> Command -> Bool)
-> (Command -> Command -> Bool) -> Eq Command
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Command -> Command -> Bool
== :: Command -> Command -> Bool
$c/= :: Command -> Command -> Bool
/= :: Command -> Command -> Bool
Eq, Int -> Command -> ShowS
[Command] -> ShowS
Command -> String
(Int -> Command -> ShowS)
-> (Command -> String) -> ([Command] -> ShowS) -> Show Command
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Command -> ShowS
showsPrec :: Int -> Command -> ShowS
$cshow :: Command -> String
show :: Command -> String
$cshowList :: [Command] -> ShowS
showList :: [Command] -> ShowS
Show)

instance PPrint Command where
  pprintTidy :: Tidy -> Command -> Doc
pprintTidy Tidy
_ = Command -> Doc
ppCmd

ppCmd :: Command -> Doc
ppCmd :: Command -> Doc
ppCmd Command
Exit             = String -> Doc
text String
"Exit"
ppCmd Command
SetMbqi          = String -> Doc
text String
"SetMbqi"
ppCmd Command
Push             = String -> Doc
text String
"Push"
ppCmd Command
Pop              = String -> Doc
text String
"Pop"
ppCmd Command
CheckSat         = String -> Doc
text String
"CheckSat"
ppCmd (DeclData [DataDecl]
d)     = String -> Doc
text String
"Data" Doc -> Doc -> Doc
<+> [DataDecl] -> Doc
forall a. PPrint a => a -> Doc
pprint [DataDecl]
d
ppCmd (Declare Text
x [] SmtSort
t) = String -> Doc
text String
"Declare" Doc -> Doc -> Doc
<+> String -> Doc
text (Text -> String
T.unpack Text
x) Doc -> Doc -> Doc
<+> String -> Doc
text String
":" Doc -> Doc -> Doc
<+> SmtSort -> Doc
forall a. PPrint a => a -> Doc
pprint SmtSort
t
ppCmd (Declare Text
x [SmtSort]
ts SmtSort
t) = String -> Doc
text String
"Declare" Doc -> Doc -> Doc
<+> String -> Doc
text (Text -> String
T.unpack Text
x) Doc -> Doc -> Doc
<+> String -> Doc
text String
":" Doc -> Doc -> Doc
<+> Doc -> Doc
parens ([SmtSort] -> Doc
forall a. PPrint a => a -> Doc
pprint [SmtSort]
ts) Doc -> Doc -> Doc
<+> SmtSort -> Doc
forall a. PPrint a => a -> Doc
pprint SmtSort
t
ppCmd Define {}   = String -> Doc
text String
"Define ..."
ppCmd (DefineFunc Symbol
name [(Symbol, SmtSort)]
symList SmtSort
rsort Expr
e) =
  String -> Doc
text String
"DefineFunc" Doc -> Doc -> Doc
<+> Symbol -> Doc
forall a. PPrint a => a -> Doc
pprint Symbol
name Doc -> Doc -> Doc
<+> [(Symbol, SmtSort)] -> Doc
forall a. PPrint a => a -> Doc
pprint [(Symbol, SmtSort)]
symList Doc -> Doc -> Doc
<+> SmtSort -> Doc
forall a. PPrint a => a -> Doc
pprint SmtSort
rsort Doc -> Doc -> Doc
<+> Expr -> Doc
forall a. PPrint a => a -> Doc
pprint Expr
e
ppCmd (Assert Maybe Int
_ Expr
e)  = String -> Doc
text String
"Assert" Doc -> Doc -> Doc
<+> Expr -> Doc
forall a. PPrint a => a -> Doc
pprint Expr
e
ppCmd (AssertAx Triggered Expr
_)  = String -> Doc
text String
"AssertAxiom ..."
ppCmd Distinct {} = String -> Doc
text String
"Distinct ..."
ppCmd GetValue {} = String -> Doc
text String
"GetValue ..."
ppCmd CMany {}    = String -> Doc
text String
"CMany ..."

-- | Responses received from SMT engine
data Response     = Ok
                  | Sat
                  | Unsat
                  | Unknown
                  | Values [(Symbol, T.Text)]
                  | Error !T.Text
                  deriving (Response -> Response -> Bool
(Response -> Response -> Bool)
-> (Response -> Response -> Bool) -> Eq Response
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Response -> Response -> Bool
== :: Response -> Response -> Bool
$c/= :: Response -> Response -> Bool
/= :: Response -> Response -> Bool
Eq, Int -> Response -> ShowS
[Response] -> ShowS
Response -> String
(Int -> Response -> ShowS)
-> (Response -> String) -> ([Response] -> ShowS) -> Show Response
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Response -> ShowS
showsPrec :: Int -> Response -> ShowS
$cshow :: Response -> String
show :: Response -> String
$cshowList :: [Response] -> ShowS
showList :: [Response] -> ShowS
Show)

-- | Additional information around the SMT solver backend
data Context = Ctx
  {
  -- | The high-level interface for interacting with the SMT solver backend.
    Context -> Solver
ctxSolver  :: SMTLIB.Backends.Solver
  -- | The close operation of the SMT solver backend.
  , Context -> IO ()
ctxClose   :: IO ()
  , Context -> Maybe Handle
ctxLog     :: !(Maybe Handle)
  , Context -> Bool
ctxVerbose :: !Bool
  , Context -> SymEnv
ctxSymEnv  :: !SymEnv
  }

--------------------------------------------------------------------------------
-- | AST Conversion: Types that can be serialized ------------------------------
--------------------------------------------------------------------------------

class SMTLIB2 a where
  smt2 :: SymEnv -> a -> Builder

runSmt2 :: (SMTLIB2 a) => SymEnv -> a -> Builder
runSmt2 :: forall a. SMTLIB2 a => SymEnv -> a -> Builder
runSmt2 = SymEnv -> a -> Builder
forall a. SMTLIB2 a => SymEnv -> a -> Builder
smt2