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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
|
module ESMT where
import List ( (\\), intercalate, isPrefixOf, union )
import Data.FiniteMap
import Text.Pretty
data SMTLib = SMTLib [Command]
deriving (Eq, Show)
type SVar = Int
type Ident = String
data Sort = SComb Ident [Sort]
deriving (Eq, Show)
showSort :: Sort -> String
showSort (SComb s ss) = s ++ intercalate "_" (map showSort ss)
isTypeParameter :: Sort -> Bool
isTypeParameter (SComb s ss) = null ss && "TVar" `isPrefixOf` s && length s > 4
data TLiteral = TInt Int
| TFloat Float
| TString String
deriving (Eq, Show)
data QIdent = Id Ident
| As Ident Sort
deriving (Eq, Show)
qidName :: QIdent -> Ident
qidName (Id n ) = n
qidName (As n _) = n
data SortedVar = SV SVar Sort
deriving (Eq, Show)
data Term = TConst TLiteral
| TSVar SVar
| TComb QIdent [Term]
| Let [(SVar, Term)] Term
| Forall [SortedVar] Term
| Exists [SortedVar] Term
deriving (Eq, Show)
tComb :: Ident -> [Term] -> Term
tComb f ts = TComb (Id f) ts
tConj :: [Term] -> Term
tConj = tComb "and"
tDisj :: [Term] -> Term
tDisj = tComb "or"
tNot :: Term -> Term
tNot t = tComb "not" [t]
tTrue :: Term
tTrue = tComb "true" []
tFalse :: Term
tFalse = tComb "false" []
tEqu :: Term -> Term -> Term
tEqu t1 t2 = tComb "=" [t1, t2]
tEquVar :: SVar -> Term -> Term
tEquVar v t = tEqu (TSVar v) t
sortedConst :: Ident -> Sort -> Term
sortedConst c s = TComb (As c s) []
data DTDecl = DT [Ident] [DTCons]
deriving (Eq, Show)
data DTCons = DCons Ident [(Ident,Sort)]
deriving (Eq, Show)
data FunSig = FunSig Ident [Sort] Sort
deriving (Eq, Show)
data FunDec = FunDec Ident [SortedVar] Sort
deriving (Eq, Show)
type FunSigTerm = ([Ident], FunSig, Term)
data Command = Assert Term
| CheckSat
| String
| DeclareVar SortedVar
| DeclareDatatypes [(Ident, Int, DTDecl)]
| DeclareFun Ident [Sort] Sort
| DeclareSort Ident Int
| DefineFunsRec [(FunDec, Term)]
| DefineSigsRec [FunSigTerm]
| EmptyLine
deriving (Eq, Show)
sAssert :: Term -> Command
sAssert = Assert . simpTerm
allQIdsOfTerm :: Term -> [QIdent]
allQIdsOfTerm (TConst _) = []
allQIdsOfTerm (TSVar _) = []
allQIdsOfTerm (TComb f args) = foldr union [f] (map allQIdsOfTerm args)
allQIdsOfTerm (Forall _ arg) = allQIdsOfTerm arg
allQIdsOfTerm (Exists _ arg) = allQIdsOfTerm arg
allQIdsOfTerm (Let bs e) =
foldr union [] (map allQIdsOfTerm (e : map snd bs))
allQIdsOfSigs :: [FunSigTerm] -> [QIdent]
allQIdsOfSigs = foldr union [] . map allQIdsOfSig
where allQIdsOfSig (_,_,t) = allQIdsOfTerm t
allQIdsOfAsserts :: [Command] -> [QIdent]
allQIdsOfAsserts = foldr union [] . map allQIdsOfAssert
allQIdsOfAssert :: Command -> [QIdent]
allQIdsOfAssert cmd = case cmd of Assert t -> allQIdsOfTerm t
_ -> []
typeParamsOfSort :: Sort -> [Ident]
typeParamsOfSort s@(SComb sn ss) =
if isTypeParameter s then [sn]
else foldr union [] (map typeParamsOfSort ss)
typeParamsOfTerm :: Term -> [Ident]
typeParamsOfTerm (TConst _) = []
typeParamsOfTerm (TSVar _) = []
typeParamsOfTerm (TComb f args) = foldr union (typeParamsOfQId f)
(map typeParamsOfTerm args)
typeParamsOfTerm (Forall svs arg) =
foldr union (typeParamsOfTerm arg) (map typeParamsOfSV svs)
typeParamsOfTerm (Exists svs arg) =
foldr union (typeParamsOfTerm arg) (map typeParamsOfSV svs)
typeParamsOfTerm (Let bs e) =
foldr union [] (map typeParamsOfTerm (e : map snd bs))
typeParamsOfQId :: QIdent -> [Ident]
typeParamsOfQId (Id _ ) = []
typeParamsOfQId (As _ s) = typeParamsOfSort s
typeParamsOfSV :: SortedVar -> [Ident]
typeParamsOfSV (SV _ s) = typeParamsOfSort s
typeParamsOfFunSig :: FunSig -> [Ident]
typeParamsOfFunSig (FunSig _ ss s) =
foldr union [] (map typeParamsOfSort (ss++[s]))
type TPSubst = FM Ident Sort
emptyTPSubst :: TPSubst
emptyTPSubst = emptyFM (<)
matchSort :: Sort -> Sort -> Maybe TPSubst
matchSort s1@(SComb sn1 ss1) s2@(SComb sn2 ss2)
| isTypeParameter s1
= Just $ if s1 == s2 then emptyTPSubst
else addToFM emptyTPSubst (head (typeParamsOfSort s1)) s2
| otherwise
= if sn1 == sn2 then matchSorts ss1 ss2 else Nothing
matchSorts :: [Sort] -> [Sort] -> Maybe TPSubst
matchSorts [] [] = Just emptyTPSubst
matchSorts [] (_:_) = Nothing
matchSorts (_:_) [] = Nothing
matchSorts (t1:ts1) (t2:ts2) = do
s <- matchSort t1 t2
t <- matchSorts (map (substSort s) ts1)(map (substSort s) ts2)
return (plusFM s t)
substSort :: TPSubst -> Sort -> Sort
substSort sub (SComb sn ss) =
maybe (SComb sn (map (substSort sub) ss)) id (lookupFM sub sn)
substTerm :: TPSubst -> Term -> Term
substTerm sub term = case term of
TConst _ -> term
TSVar _ -> term
TComb f args -> TComb (substQId sub f) (map (substTerm sub) args)
Forall svs arg -> Forall (map (substSV sub) svs) (substTerm sub arg)
Exists svs arg -> Forall (map (substSV sub) svs) (substTerm sub arg)
Let bs e -> Let (map (\ (v,s) -> (v, substTerm sub s)) bs) (substTerm sub e)
substQId :: TPSubst -> QIdent -> QIdent
substQId _ qid@(Id _) = qid
substQId sub (As n s) = As n (substSort sub s)
substSV :: TPSubst -> SortedVar -> SortedVar
substSV sub (SV v s) = SV v (substSort sub s)
substFunSig :: TPSubst -> FunSig -> FunSig
substFunSig sub (FunSig fn ss s) =
FunSig fn (map (substSort sub) ss) (substSort sub s)
substDefSig :: TPSubst -> FunSigTerm -> FunSigTerm
substDefSig tsub (ps, fsig, term) =
(ps \\ keysFM tsub, substFunSig tsub fsig, substTerm tsub term)
rnmTerm :: (Ident -> Ident) -> Term -> Term
rnmTerm rnm term = case term of
TConst _ -> term
TSVar _ -> term
TComb f args -> TComb (rnmQId rnm f) (map (rnmTerm rnm) args)
Forall svs arg -> Forall svs (rnmTerm rnm arg)
Exists svs arg -> Forall svs (rnmTerm rnm arg)
Let bs e -> Let (map (\ (v,s) -> (v, rnmTerm rnm s)) bs) (rnmTerm rnm e)
rnmQId :: (Ident -> Ident) -> QIdent -> QIdent
rnmQId rnm (Id n) = Id (rnm n)
rnmQId rnm (As n s) = As (rnm n) s
rnmFunSig :: (Ident -> Ident) -> FunSig -> FunSig
rnmFunSig rnm (FunSig fn ss s) = FunSig (rnm fn) ss s
rnmDefSig :: (Ident -> Ident) -> ([Ident],FunSig,Term) -> ([Ident],FunSig,Term)
rnmDefSig rnm (ps, fsig, term) =
(ps, rnmFunSig rnm fsig, rnmTerm rnm term)
simpTerm :: Term -> Term
simpTerm (TConst l) = TConst l
simpTerm (TSVar v) = TSVar v
simpTerm (Let bs t) = if null bs then t'
else Let bs' t'
where bs' = map (\ (v,tm) -> (v, simpTerm tm)) bs
t' = simpTerm t
simpTerm (Forall vs t) = if null vs then t' else Forall vs t'
where t' = simpTerm t
simpTerm (Exists vs t) = if null vs then t' else Exists vs t'
where t' = simpTerm t
simpTerm (TComb f ts)
| qidName f == "/=" && length ts == 2
= simpTerm (TComb (Id "not") [TComb (Id "=") ts])
| f == Id "apply" && not (null ts')
= case head ts' of TComb s' ts0 -> TComb s' (ts0 ++ tail ts')
_ -> fts
| f == Id "not"
= case ts' of [TComb s' [ts0]] -> if s' == f then ts0 else fts
_ -> fts
| f == Id "and"
= case filter (/= tTrue) ts' of
[] -> tTrue
cjs -> if tFalse `elem` cjs
then tFalse
else TComb f (concatMap joinSame cjs)
| f == Id "or"
= case filter (/= tFalse) ts' of
[] -> tFalse
djs -> if tTrue `elem` djs
then tTrue
else TComb f (concatMap joinSame djs)
| otherwise = fts
where
ts' = map simpTerm ts
fts = TComb f ts'
joinSame arg = case arg of TComb f' args | f==f' -> args
_ -> [arg]
reduceAsInTerm :: Term -> Term
reduceAsInTerm (TConst l) = TConst l
reduceAsInTerm (TSVar v) = TSVar v
reduceAsInTerm (Let bs t) = Let (map (\ (v,tm) -> (v, reduceAsInTerm tm)) bs)
(reduceAsInTerm t)
reduceAsInTerm (Forall vs t) = Forall vs (reduceAsInTerm t)
reduceAsInTerm (Exists vs t) = Exists vs (reduceAsInTerm t)
reduceAsInTerm (TComb f ts) = TComb (simpAs f) (map reduceAsInTerm ts)
where
simpAs qid = case qid of As n (SComb s _) | s == "Func" -> Id n
_ -> qid
sortIdsOfSort :: Sort -> [Ident]
sortIdsOfSort (SComb s ss) = s : concatMap sortIdsOfSort ss
sortsOfTerm :: Term -> [Sort]
sortsOfTerm (TConst _) = []
sortsOfTerm (TSVar _) = []
sortsOfTerm (Let bs t) = concatMap (sortsOfTerm . snd) bs ++ sortsOfTerm t
sortsOfTerm (Forall vs t) = map sortOfSortedVar vs ++ sortsOfTerm t
sortsOfTerm (Exists vs t) = map sortOfSortedVar vs ++ sortsOfTerm t
sortsOfTerm (TComb f ts) = sortsOfQIdent f ++ concatMap sortsOfTerm ts
where
sortsOfQIdent (Id _) = []
sortsOfQIdent (As _ s) = [s]
sortOfSortedVar :: SortedVar -> Sort
sortOfSortedVar (SV _ s) = s
unpoly :: [Command] -> [Command]
unpoly commands =
let allsigs = map sigNameSort (allSigs commands)
in map (unpolyCmd allsigs) . reverse . addSigs [] . reverse $ commands
where
addSigs _ [] = []
addSigs qids (cmd:cmds) = case cmd of
DefineSigsRec fts ->
let (qids1,ftss) = addAllInstancesOfSigs (union (allQIdsOfSigs fts) qids)
fts
in DefineSigsRec ftss : addSigs qids1 cmds
_ -> cmd : addSigs (union (allQIdsOfAssert cmd) qids) cmds
unpolyCmd sigs cmd = case cmd of
DefineSigsRec fts -> DefineSigsRec $ map rnmTermInSig
(filter (\ (ps,_,_) -> null ps) fts)
Assert term -> Assert (rnmQIdWithTInstTerm sigs term)
_ -> cmd
where
rnmTermInSig (ps,sig,term) = (ps, sig, rnmQIdWithTInstTerm sigs term)
addAllInstancesOfSigs :: [QIdent] -> [FunSigTerm] -> ([QIdent], [FunSigTerm])
addAllInstancesOfSigs allqids = addAllInsts allqids
where
addAllInsts qids fts =
let (qids1,fts1) = addInstancesOfSigs qids fts
in if null fts1
then (qids1,fts)
else let (qids2,fts2) = addAllInsts
(union qids1 (allQIdsOfSigs fts1 \\ allqids))
(fts ++ fts1)
in (qids2, fts2)
addInstancesOfSigs :: [QIdent] -> [FunSigTerm] -> ([QIdent], [FunSigTerm])
addInstancesOfSigs qids allsigs = addInstsOfSigs qids allsigs
where
addInstsOfSigs qids0 [] = (qids0,[])
addInstsOfSigs qids0 (fts:ftss) =
let (qids1,fts1) = addInstancesOfSig qids0 allsigs fts
(qids2,fts2) = addInstsOfSigs qids1 ftss
in (qids2, fts1 ++ fts2)
addInstancesOfSig :: [QIdent] -> [FunSigTerm] -> FunSigTerm
-> ([QIdent], [FunSigTerm])
addInstancesOfSig allqids allsigs fts@(ps, (FunSig fn ss rs), _) =
addSigInsts allqids
where
addSigInsts [] = ([],[])
addSigInsts (qid:qids) =
let (qids1,sigs1) = addSigInsts qids
in case qid of
As n s | n==fn -> (qids1, sigInstForType s ++ sigs1)
_ -> (qid : qids1, sigs1)
sigInstForType s =
maybe []
(\tsub -> let rnm = toTInstName fn ps tsub
in if rnm fn `elem` map nameOfSig allsigs
then []
else [(rnmDefSig rnm (substDefSig tsub fts))])
(matchSort (sigTypeAsSort ss rs) s)
rnmQIdWithTInst :: [(Ident, ([Ident],Sort))] -> QIdent -> QIdent
rnmQIdWithTInst _ (Id n) = Id n
rnmQIdWithTInst sigs qid@(As n s) =
maybe qid
(\ (ps,psort) -> maybe qid
(\tsub -> As (addTInstName ps tsub n) s)
(matchSort psort s))
(lookup n sigs)
rnmQIdWithTInstTerm :: [(Ident, ([Ident],Sort))] -> Term -> Term
rnmQIdWithTInstTerm sigs term = case term of
TConst _ -> term
TSVar _ -> term
TComb f args -> TComb (rnmQIdWithTInst sigs f)
(map (rnmQIdWithTInstTerm sigs) args)
Forall svs arg -> Forall svs (rnmQIdWithTInstTerm sigs arg)
Exists svs arg -> Forall svs (rnmQIdWithTInstTerm sigs arg)
Let bs e -> Let (map (\ (v,s) -> (v, rnmQIdWithTInstTerm sigs s)) bs)
(rnmQIdWithTInstTerm sigs e)
toTInstName :: Ident -> [Ident] -> TPSubst -> Ident -> Ident
toTInstName fn ps tsub n | fn == n = addTInstName ps tsub n
| otherwise = n
addTInstName :: [Ident] -> TPSubst -> Ident -> Ident
addTInstName tps tsub n =
n ++ concatMap (\p -> maybe "" (('_':) . showSort) (lookupFM tsub p)) tps
allSigs :: [Command] -> [FunSigTerm]
allSigs = concatMap sigOfCmd
where sigOfCmd cmd = case cmd of DefineSigsRec fts -> fts
_ -> []
nameOfSig :: FunSigTerm -> Ident
nameOfSig (_, FunSig n _ _, _) = n
sigNameSort :: FunSigTerm -> (Ident, ([Ident],Sort))
sigNameSort (ps, FunSig n ss s, _) = (n, (ps, sigTypeAsSort ss s))
sigTypeAsSort :: [Sort] -> Sort -> Sort
sigTypeAsSort [] s = s
sigTypeAsSort (t:ts) s = SComb "Func" [t, sigTypeAsSort ts s]
showSMT :: [Command] -> String
showSMT cmds = pPrint (pretty (SMTLib (unpoly cmds))) ++ "\n"
showSMTRaw :: [Command] -> String
showSMTRaw cmds = pPrint (pretty (SMTLib cmds)) ++ "\n"
instance Pretty SMTLib where
pretty (SMTLib cmds) = vsep (map pretty cmds)
instance Pretty Sort where
pretty (SComb i ss) = parensIf (not $ null ss) $
text i <+> (hsep (map pretty ss))
instance Pretty TLiteral where
pretty (TInt n) = int n
pretty (TFloat f) = float f
pretty (TString s) = text s
instance Pretty QIdent where
pretty (Id i ) = text i
pretty (As i s) = parent [text "as", text i, pretty s]
instance Pretty SortedVar where
pretty (SV v s) = parent [prettyVar v, pretty s]
instance Pretty Term where
pretty (TConst c) = pretty c
pretty (TSVar v) = prettyVar v
pretty (TComb qi ts) = parensIf (not $ null ts) $
pretty qi <+> (hsep (map pretty ts))
pretty (Let bs t) = parent [text "let", parent (map ppBind bs), pretty t]
where ppBind (v, t') = parent [prettyVar v, pretty t']
pretty (Forall svs t) = parent [ text "forall"
, parent (map pretty svs)
, pretty t
]
pretty (Exists svs t) = parent [ text "exists"
, parent (map pretty svs)
, pretty t
]
instance Pretty DTDecl where
pretty (DT tys cs) = if null tys
then parent (map pretty cs)
else parent [ text "par"
, parent (map text tys)
, parent (map pretty cs)
]
instance Pretty DTCons where
pretty (DCons sym sels) = parent [text sym, (hsep (map prettySel sels))]
where
prettySel (n,s) = parent [text n, pretty s]
instance Pretty FunSig where
pretty (FunSig fn ss s) = parent (ppCmd (DeclareFun fn ss s))
instance Pretty FunDec where
pretty (FunDec fn svs s) = parent [ text fn, parent (map pretty svs)
, pretty s
]
instance Pretty Command where
pretty cmd = case cmd of
Comment cmt -> semi <+> text cmt
EmptyLine -> text ""
DefineSigsRec fts -> vsep $ map (pretty . (\ (_,t,_) -> t)) fts ++
map ppSigBody fts
_ -> parent $ ppCmd cmd
ppSigBody :: ([Ident],FunSig,Term) -> Doc
ppSigBody (_, FunSig fn _ _, term) = vsep $ map pretty
[ EmptyLine, Comment $ "Axiomatization of function '" ++ fn ++ "'"
, sAssert term ]
ppCmd :: Command -> [Doc]
ppCmd cmd = case cmd of
Assert t -> [text "assert", pretty (reduceAsInTerm t)]
CheckSat -> [text "check-sat"]
DeclareVar (SV v s) -> [text "declare-const", prettyVar v, pretty s]
DeclareDatatypes sds ->
if length sds /= 1
then error "Datatype declaration with more than one type!"
else let (tc, _, DT tvs cs) = head sds
in [ text "declare-datatypes"
, parent (map text tvs)
, parent [parent (text tc : map pretty cs)]
]
DeclareFun fn ss s -> [ text "declare-fun"
, text fn
, parent (map pretty ss)
, pretty s
]
DefineFunsRec fts -> let (fs, ts) = unzip fts in
[ text "define-funs-rec"
, parent (map pretty fs)
, parent (map pretty ts)
]
DeclareSort sym n -> [text "declare-sort", text sym, int n]
_ -> error $ "ppCmd: command '" ++ show cmd ++ "' not reachable!"
prettyVar :: SVar -> Doc
prettyVar v = text ('x' : show v)
parent :: [Doc] -> Doc
parent = encloseSep lparen rparen space
|