1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
--- ----------------------------------------------------------------------------
--- This module provides an abstract representation of the SMT-LIB language.
--- The implementation is based on the SMT-LIB Standard 2.6
--- (http://smtlib.cs.uiowa.edu/papers/smt-lib-reference-v2.6-r2017-07-18.pdf)
--- and covers most parts of the language description.
---
--- @author  Jan Tikovsky
--- @version December 2017
--- ----------------------------------------------------------------------------
module Language.SMTLIB.Types where

--- Representation of an SMT-LIB script
data SMTLib = SMTLib [Command]
  deriving (Eq, Show)

type Symbol  = String
type Ident   = String
type Numeral = Int

data Keyword = KW Symbol
  deriving (Eq, Show)

--- S-expressions

-- binary and hexadecimal representation not supported
data SpecConstant = Num Numeral
                  | Dec Float
                  | Str String
  deriving (Eq, Show)

data SExpr = SEConst SpecConstant
           | SESym   Symbol
           | SEKW    Keyword
           | SEList  [SExpr]
  deriving (Eq, Show)

--- Sorts

data Sort = SComb Ident [Sort]
  deriving (Eq, Ord, Show)

--- Attributes

data AttrValue = AVConst SpecConstant
               | AVSym   Symbol
               | AVSExpr [SExpr]
  deriving (Eq, Show)

data Attribute = AKW  Keyword
               | AVal Keyword AttrValue
  deriving (Eq, Show)

--- Terms

-- note: definitions of 'var_binding' and 'match_case' are inlined

data QIdent = Id Ident
            | As Ident Sort
  deriving (Eq, Show)

data SortedVar = SV Symbol Sort
  deriving (Eq, Show)

data Pattern = PComb Symbol [Symbol]
  deriving (Eq, Show)

data Term = TConst SpecConstant
          | TComb  QIdent [Term]
          | Let    [(Symbol, Term)] Term
          | Forall [SortedVar] Term
          | Exists [SortedVar] Term
          | Match  Term [(Pattern, Term)]
          | Annot  Term [Attribute]
  deriving (Eq, Show)

--- Theories

data SortSymDecl = SortSymDecl Ident Numeral [Attribute]
  deriving (Eq, Show)

data MetaSpecConstant = NUMERAL
                      | DECIMAL
                      | STRING
  deriving (Eq, Show)

data FunSymDecl = Spec  SpecConstant Sort [Attribute]
                | Meta  MetaSpecConstant Sort [Attribute]
                | Ident Ident [Sort] [Attribute]
  deriving (Eq, Show)

data ParFunSymDecl = ParFunSymDecl FunSymDecl [Symbol] Ident [Sort] [Attribute]
  deriving (Eq, Show)

data TheoryAttr = TASorts      [SortSymDecl]
                | TAFuns       [ParFunSymDecl]
                | TASortsDesc  String
                | TAFunsDesc   String
                | TADefinition String
                | TAValues     String
                | TANotes      String
                | TA           Attribute
  deriving (Eq, Show)

data Theory = Theory Symbol [TheoryAttr]
  deriving (Eq, Show)

--- Info flags

data InfoFlag = AllStatistics
              | AssertionStackLevels
              | Authors
              | ErrorBehavior
              | Name
              | ReasonUnknown
              | Version
              | IFKW Keyword
  deriving (Eq, Show)

--- Command options

data Option = DiagnosticOutput     String
            | GlobalDecls          Bool
            | Interactive          Bool
            | PrintSuccess         Bool
            | ProduceAssertions    Bool
            | ProduceAssign        Bool
            | ProduceModels        Bool
            | ProduceProofs        Bool
            | ProduceUnsatAssump   Bool
            | ProduceUnsatCores    Bool
            | RandomSeed           Numeral
            | RegularOutput        String
            | ReproducibleResLimit Numeral
            | Verbosity            Numeral
            | OptAttr              Attribute
  deriving (Eq, Show)

--- Commands

-- Note: instead of a 'selector_dec' definition the definition of 'sorted_var' is reused

data FunDec = FunDec Symbol [SortedVar] Sort
  deriving (Eq, Show)

data FunDef = FunDef Symbol [SortedVar] Sort Term
  deriving (Eq, Show)

data PropLit = Sym Symbol
             | Not Symbol
  deriving (Eq, Show)

--- sort declaration for datatypes
data SortDecl = SortDecl Symbol Numeral
  deriving (Eq, Show)

--- datatype declaration
data DTDecl = MT [ConsDecl]          -- monomorphic type
            | PT [Symbol] [ConsDecl] -- polymorphic type
  deriving (Eq, Show)

data ConsDecl = Cons Symbol [SortedVar]
  deriving (Eq, Show)

data Command = Assert              Term
             | CheckSat
             | CheckSatAssuming    [PropLit]
             | DeclareConst        Symbol Sort
             | DeclareDatatype     Symbol DTDecl
             | DeclareDatatypes    [(SortDecl, DTDecl)]
             | DeclareFun          Symbol [Sort] Sort
             | DeclareSort         Symbol Numeral
             | DefineFun           FunDef
             | DefineFunRec        FunDef
             | DefineFunsRec       [(FunDec, Term)]
             | DefineSort          Symbol [Symbol] Sort
             | Echo                String
             | Exit
             | GetAssertions
             | GetAssignment
             | GetInfo             InfoFlag
             | GetModel
             | GetOption           Option
             | GetProof
             | GetUnsatAssumptions
             | GetUnsatCore
             | GetValue            [Term]
             | Pop                 Numeral
             | Push                Numeral
             | Reset
             | ResetAssertions
             | SetInfo             Attribute
             | SetLogic            Logic
             | SetOption           Option
               -- no official command, but useful for the generation
               -- of line comments in SMT-LIB scripts
             | Comment             String
  deriving (Eq, Show)

--- Logics provided by the SMT-LIB Standard
---
--- Explanation of the naming conventions:
---
---   * QF: restriction to quantifier free formulas
---   * A or AX: theory of ArraysEx
---   * BV: theory FixedSizeBitVectors
---   * FP: theory FloatingPoint
---   * IA: theory Ints (Integer Arithmetic)
---   * RA: theory Reals (Real Arithmetic)
---   * IRA: theory Reals_Ints (mixed Integer Real Arithmetic)
---   * IDL: Integer Difference Logic
---   * RDL: Rational Difference Logic
---   * L before IA, RA or IRA: linear fragment of those arithmetics
---   * N before IA, RA or IRA: non-linear fragment of those arithmetics
---   * UF extension allowing free sort and function symbols
---
--- see http://smtlib.cs.uiowa.edu/logics.shtml for more details
data Logic = ALL
           | AUFLIA
           | AUFLIRA
           | AUFNIRA
           | LIA
           | LRA
           | QFABV
           | QFAUFBV
           | QFAUFLIA
           | QFAX
           | QFBV
           | QFIDL
           | QFLIA
           | QFLRA
           | QFNIA
           | QFNRA
           | QFRDL
           | QFUF
           | QFUFBV
           | QFUFIDL
           | QFUFLIA
           | QFUFLRA
           | QFUFNRA
           | UFLRA
           | UFNIA
  deriving (Eq, Show)

--- Command responses

data ErrorBehavior = ImmediateExit
                   | ContinuedExecution
  deriving (Eq, Show)

data ReasonUnknown = Memout
                   | Incomplete
                   | SEReason SExpr
  deriving (Eq, Show)

data ModelRsp = MRFun     FunDef
              | MRFunRec  FunDef
              | MRFunsRec [(FunDec, Term)]
  deriving (Eq, Show)

data InfoRsp = AssertionStackLevelsRsp Numeral
             | AuthorsRsp              String
             | ErrorBehaviorRsp        ErrorBehavior
             | NameRsp                 String
             | ReasonUnknownRsp        ReasonUnknown
             | VersionRsp              String
             | AttrRsp                 Attribute
  deriving (Eq, Show)

type ValuationPair  = (Term, Term)
type TValuationPair = (Symbol, Bool)

data CmdResponse = SuccessRsp
                 | UnsupportedRsp
                 | ErrorRsp               String
                 | CheckSatRsp            CheckSat
                 | EchoRsp                String
                 | GetAssertionsRsp       [Term]
                 | GetAssignmentRsp       [TValuationPair]
                 | GetInfoRsp             [InfoRsp]
                 | GetModelRsp            [ModelRsp]
                 | GetOptionRsp           AttrValue
                 | GetProofRsp            SExpr
                 | GetUnsatAssumptionsRsp [Symbol]
                 | GetUnsatCoreRsp        [Symbol]
                 | GetValueRsp            [ValuationPair]
  deriving (Eq, Show)

data CheckSat = Sat
              | Unsat
              | Unknown
  deriving (Eq, Show)