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
module BoolExp
  ( BoolExp(..), bTrue, bFalse, bNot, bEqu, bEquVar, bindingBE, letBinding
  , forallBinding, existsBinding, assertSMT, allSymbolsOfBE, simpBE
  , showBoolExp, smtBE, asLisp, prettyBE, withBracket ) where

import qualified SetFunctions
import qualified Char
import qualified List

data BoolExp = BVar Int
             | BTerm String [BoolExp]
             | Conj [BoolExp]
             | Disj [BoolExp]
             | Not BoolExp
             | Binding String [(Int,BoolExp)] BoolExp

bTrue :: BoolExp
bTrue = BTerm "true" []

bFalse :: BoolExp
bFalse = BTerm "false" []

bNot :: BoolExp -> BoolExp
bNot = Not

bEqu :: BoolExp -> BoolExp -> BoolExp
bEqu b1 b2 = BTerm "=" [b1,b2]

bEquVar :: Int -> BoolExp -> BoolExp
bEquVar v bexp = BTerm "=" [BVar v,bexp]

bindingBE :: String -> [(Int,BoolExp)] -> BoolExp -> BoolExp
bindingBE bkind bs exp
  | null bs = exp
  | otherwise = Binding bkind bs exp

letBinding :: [(Int,BoolExp)] -> BoolExp -> BoolExp
letBinding = bindingBE "let"

forallBinding :: [(Int,BoolExp)] -> BoolExp -> BoolExp
forallBinding = bindingBE "forall"

existsBinding :: [(Int,BoolExp)] -> BoolExp -> BoolExp
existsBinding = bindingBE "exists"

assertSMT :: BoolExp -> BoolExp
assertSMT be = BTerm "assert" [be]

allSymbolsOfBE :: BoolExp -> [String]
allSymbolsOfBE (BVar _) = []
allSymbolsOfBE (BTerm s args) = foldr List.union [s] (map allSymbolsOfBE args)
allSymbolsOfBE (Conj args) = foldr List.union [] (map allSymbolsOfBE args)
allSymbolsOfBE (Disj args) = foldr List.union [] (map allSymbolsOfBE args)
allSymbolsOfBE (Not arg) = allSymbolsOfBE arg
allSymbolsOfBE (Binding _ bs e) =
  foldr List.union [] (map allSymbolsOfBE (e : map snd bs))

simpBE :: BoolExp -> BoolExp
simpBE x1 = SetFunctions.selectValue (SetFunctions.set1 simpBE_ORGNDFUN x1)

simpBE_ORGNDFUN_ORGRULES :: BoolExp -> BoolExp
simpBE_ORGNDFUN_ORGRULES (Conj (bs1 ++ ([BTerm "true" []] ++ bs2))) =
  simpBE (Conj (bs1 ++ bs2))
simpBE_ORGNDFUN_ORGRULES (Conj (_ ++ ([BTerm "false" []] ++ _))) = bFalse
simpBE_ORGNDFUN_ORGRULES (Conj (bs1 ++ ([Conj bs2] ++ bs3))) =
  simpBE (Conj (bs1 ++ (bs2 ++ bs3)))
simpBE_ORGNDFUN_ORGRULES (Conj bs) =
  if null bs then bTrue else Conj (map simpBE bs)
simpBE_ORGNDFUN_ORGRULES (Disj (bs1 ++ ([BTerm "false" []] ++ bs2))) =
  simpBE (Disj (bs1 ++ bs2))
simpBE_ORGNDFUN_ORGRULES (Disj (_ ++ ([BTerm "true" []] ++ _))) = bTrue
simpBE_ORGNDFUN_ORGRULES (Disj (bs1 ++ ([Disj bs2] ++ bs3))) =
  simpBE (Disj (bs1 ++ (bs2 ++ bs3)))
simpBE_ORGNDFUN_ORGRULES (Disj bs) =
  if null bs then bFalse else Disj (map simpBE bs)
simpBE_ORGNDFUN_ORGRULES (Not (Not b)) = b
simpBE_ORGNDFUN_ORGRULES (Binding _ [] e) = e
simpBE_ORGNDFUN_ORGRULES (BTerm f args) = BTerm f (map simpBE args)

simpBE_ORGNDFUN_APPLICABLE :: BoolExp -> ()
simpBE_ORGNDFUN_APPLICABLE (Conj (_ ++ ([BTerm "true" []] ++ _))) = ()
simpBE_ORGNDFUN_APPLICABLE (Conj (_ ++ ([BTerm "false" []] ++ _))) = ()
simpBE_ORGNDFUN_APPLICABLE (Conj (_ ++ ([Conj _] ++ _))) = ()
simpBE_ORGNDFUN_APPLICABLE (Conj _) = ()
simpBE_ORGNDFUN_APPLICABLE (Disj (_ ++ ([BTerm "false" []] ++ _))) = ()
simpBE_ORGNDFUN_APPLICABLE (Disj (_ ++ ([BTerm "true" []] ++ _))) = ()
simpBE_ORGNDFUN_APPLICABLE (Disj (_ ++ ([Disj _] ++ _))) = ()
simpBE_ORGNDFUN_APPLICABLE (Disj _) = ()
simpBE_ORGNDFUN_APPLICABLE (Not (Not _)) = ()
simpBE_ORGNDFUN_APPLICABLE (Binding _ [] _) = ()
simpBE_ORGNDFUN_APPLICABLE (BTerm _ _) = ()

simpBE_ORGNDFUN_DEFAULT :: BoolExp -> BoolExp
simpBE_ORGNDFUN_DEFAULT be
  | SetFunctions.isEmpty (SetFunctions.set1 simpBE_ORGNDFUN_APPLICABLE be)
  = be

simpBE_ORGNDFUN :: BoolExp -> BoolExp
simpBE_ORGNDFUN x1 = simpBE_ORGNDFUN_ORGRULES x1 ? simpBE_ORGNDFUN_DEFAULT x1

showBoolExp :: BoolExp -> String
showBoolExp = smtBE . simpBE

smtBE :: BoolExp -> String
smtBE (BVar i) = 'x' : show i
smtBE (BTerm f args)
  | f == "=" = asLisp ["=",smtBE (head args),smtBE (args !! 1)]
  | f == "/=" = smtBE (Not (BTerm "=" args))
  | f == "let"
  = asLisp
     ["let"
     ,asLisp (map (\(BTerm _ [v,e]) -> asLisp [smtBE v,smtBE e]) (tail args))
     ,smtBE (head args)]
  | otherwise = if null args then f else asLisp $ (f : map smtBE args)
smtBE (Conj bs) =
  case bs of
    [] -> "true"
    [b] -> smtBE b
    _ -> asLisp $ ("and" : map smtBE bs)
smtBE (Disj bs) =
  case bs of
    [] -> "false"
    [b] -> smtBE b
    _ -> asLisp $ ("or" : map smtBE bs)
smtBE (Not b) = asLisp ["not",smtBE b]
smtBE (Binding s bs e) =
  asLisp
   [s,asLisp (map (\(v,b) -> asLisp [smtBE (BVar v),smtBE b]) bs),smtBE e]

asLisp :: [String] -> String
asLisp = withBracket . unwords

prettyBE :: BoolExp -> String
prettyBE (BVar i) = 'x' : show i
prettyBE (BTerm f args)
  | f == "=" = prettyBE (head args) ++ (" = " ++ prettyBE (args !! 1))
  | f == "/=" = prettyBE (Not (BTerm "=" args))
  | null args = f
  | not (Char.isAlpha (head f)) && (length args == 2)
  = prettyBE (args !! 0) ++ (f ++ prettyBE (args !! 1))
  | otherwise = f ++ withBracket (List.intercalate "," (map prettyBE args))
prettyBE (Conj bs) =
  case bs of
    [] -> "true"
    [b] -> prettyBE b
    _ -> withBracket (List.intercalate " && " (map prettyBE bs))
prettyBE (Disj bs) =
  case bs of
    [] -> "false"
    [b] -> prettyBE b
    _ -> withBracket (List.intercalate " || " (map prettyBE bs))
prettyBE (Not b) = withBracket ("not " ++ prettyBE b)
prettyBE (Binding s bs e) =
  withBracket
   $ (unwords
       $ [s
         ,"{"
         ,List.intercalate ";"
           (map (\(v,b) -> unwords [prettyBE (BVar v),"=",prettyBE b]) bs)
         ,"}"
         ,prettyBE e])

withBracket :: String -> String
withBracket s = '(' : (s ++ ")")