-
Notifications
You must be signed in to change notification settings - Fork 0
/
BDD.hs
176 lines (152 loc) · 5.83 KB
/
BDD.hs
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
module BDD where
import Data.List
import Data.Map as M hiding (map)
import Data.Maybe as Y
type Index = Int
data BExp = Prim Bool | IdRef Index | Not BExp | And BExp BExp | Or BExp BExp
deriving (Eq, Ord, Show)
type Env = [(Index, Bool)]
type NodeId = Int
type BDDNode = (NodeId, (Index, NodeId, NodeId))
type BDD = (NodeId, [BDDNode])
------------------------------------------------------
-- PART I
-- Pre: The item is in the given table
lookUp :: Eq a => a -> [(a, b)] -> b
lookUp x
= snd . head . dropWhile (\p -> (fst p) /= x)
checkSat :: BDD -> Env -> Bool
checkSat (root, nodes) env
= check root where
check 0 = False
check 1 = True
check nodeid = check (choose node) where
node = lookUp nodeid nodes
choose (index, f, t) = if (lookUp index env) then t else f
sat :: BDD -> [[(Index, Bool)]]
sat (root, nodes)
= sat' root where
sat' 0 = []
sat' 1 = [[]]
sat' nodeid = map ((index, False) :) (sat' f) ++ map ((index, True) :) (sat' t) where
(index, f, t) = lookUp nodeid nodes
------------------------------------------------------
-- PART II
simplify :: BExp -> BExp
simplify (Not (Prim b))
= Prim (not b)
simplify (And (Prim b) (Prim b2))
= Prim (b && b2)
simplify (Or (Prim b) (Prim b2))
= Prim (b || b2)
simplify bexp = bexp
restrict :: BExp -> Index -> Bool -> BExp
restrict bexp index b
= restrict' bexp where
restrict' (IdRef i)
| i == index = Prim b
| otherwise = IdRef i
restrict' (Not bexp) = simplify (Not (restrict' bexp))
restrict' (And bexp bexp2) = simplify (And (restrict' bexp) (restrict' bexp2))
restrict' (Or bexp bexp2) = simplify (Or (restrict' bexp) (restrict' bexp2))
restrict' bexp = bexp
------------------------------------------------------
-- PART III
-- Pre: Each variable index in the BExp appears exactly once
-- in the Index list; there are no other elements
-- The question suggests the following definition (in terms of buildBDD')
-- but you are free to implement the function differently if you wish.
buildBDD :: BExp -> [Index] -> BDD
buildBDD (Prim b) _
= (if b then 1 else 0, [])
buildBDD bexp indexes
= buildBDD' bexp 2 indexes
-- Potential helper function for buildBDD which you are free
-- to define/modify/ignore/delete/embed as you see fit.
buildBDD' :: BExp -> NodeId -> [Index] -> BDD
buildBDD' bexp root indexes
= (root, buildBDD'' bexp root indexes) where
buildBDD'' _ _ [] = []
buildBDD'' bexp nodeid (index : indexes) = (nodeid, (index, left, right))
: (buildBDD'' lexp left indexes
++ buildBDD'' rexp right indexes) where
left = choose lexp (2 * nodeid)
right = choose rexp (2 * nodeid + 1)
lexp = restrict bexp index False
rexp = restrict bexp index True
choose (Prim b) _ = if b then 1 else 0
choose _ n = n
------------------------------------------------------
-- PART IV
-- Pre: Each variable index in the BExp appears exactly once
-- in the Index list; there are no other elements
buildROBDD :: BExp -> [Index] -> BDD
buildROBDD bexp indexes = applyEliminate (applyShare (buildBDD bexp indexes))
applyEliminate (root, allNodes) = (root, applyEliminate' root) where
applyEliminate' 0 = []
applyEliminate' 1 = []
applyEliminate' nodeid =
let
(nodeid', node) = eliminate nodeid allNodes
(index, left, right) = fromMaybe (lookUp nodeid allNodes) node
leftNodes = applyEliminate' left
rightNodes = applyEliminate' right
in
((if nodeid /= root then nodeid' else root, (index, left, right)) : (leftNodes ++ rightNodes))
eliminate nodeid nodes
| nodeid == 0 = (0, Nothing)
| nodeid == 1 = (1, Nothing)
| otherwise =
let
(leftChild', leftTriple) = eliminate leftChild nodes
(rightChild', rightTriple) = eliminate rightChild nodes
(index, leftChild, rightChild) = lookUp nodeid nodes
in
if leftChild' == rightChild' then
(leftChild', leftTriple)
else
(nodeid, Just (index, leftChild', rightChild'))
applyShare (root, allNodes) = (root, (fst (applyShare' M.empty root))) where
subs = subtrees allNodes
applyShare' memo 0 = ([], memo)
applyShare' memo 1 = ([], memo)
applyShare' memo nodeid =
let
(index, leftChild, rightChild) = lookUp nodeid allNodes
(children, memo') = subtreeMemo memo nodeid allNodes subs
(leftChild', rightChild') = fromMaybe (leftChild, rightChild) children
(left, memo'') = applyShare' memo' leftChild'
(right, memo''') = applyShare' memo'' rightChild'
in
(((nodeid, (index, leftChild', rightChild')) : (left ++ right)), memo''')
subtreeMemo memo nodeid nodes subs
| nodeid == 0 = (Nothing, memo)
| nodeid == 1 = (Nothing, memo)
| otherwise =
let
(index, leftChild, rightChild) = lookUp nodeid nodes
(leftChild', memo')
| leftChild == 0 || leftChild == 1 = (Nothing, memo)
| otherwise = M.insertLookupWithKey (\k n o -> o) leftTree leftChild memo where
leftTree = lookUp leftChild subs
(rightChild', memo'')
| rightChild == 0 || rightChild == 1 = (Nothing, memo')
| otherwise = M.insertLookupWithKey (\k n o -> o) rightTree rightChild memo' where
rightTree = lookUp rightChild subs
in
(Just (fromMaybe leftChild leftChild', fromMaybe rightChild rightChild'), memo'')
subtrees :: [BDDNode] -> [BDD]
subtrees nodes = map subtree nodes where
subtree (nodeid, (index, left, right)) = (nodeid, subtree' nodeid 2)
subtree' 0 _ = []
subtree' 1 _ = []
subtree' nodeid nodeid' = (nodeid', (index, left', right')) : (subtree' left left' ++ subtree' right right') where
left'
| left == 0 = left
| left == 1 = left
| otherwise = 2 * nodeid'
right'
| right == 0 = right
| right == 1 = right
| otherwise = 2 * nodeid' + 1
(index, left, right) = lookUp nodeid nodes