-
Notifications
You must be signed in to change notification settings - Fork 1
/
Circ.hs
321 lines (264 loc) · 10.1 KB
/
Circ.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
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
{-# LANGUAGE GADTs, KindSignatures, DataKinds, TypeOperators, PolyKinds,
StandaloneDeriving, ScopedTypeVariables #-}
module Circ where
import Data.List
import Data.List.Extra
import Data.Function
import Data.Traversable
import Control.Applicative
import Control.Arrow
import Control.Monad
import BigArray
data Tuply :: * -> * where
Oney :: Tuply ()
Booly :: Tuply Bool
(:*:) :: Tuply s -> Tuply t -> Tuply (s, t)
deriving instance Show (Tuply t)
deriving instance Eq (Tuply t)
infixr 4 :*:
class (Show i, Ord i) => TUPLY i where
tuply :: Tuply i
instance TUPLY () where
tuply = Oney
instance TUPLY Bool where
tuply = Booly
instance (TUPLY s, TUPLY t) => TUPLY (s, t) where
tuply = tuply :*: tuply
tuples :: Tuply t -> [t]
tuples Oney = [()]
tuples Booly = [False, True]
tuples (s :*: t) = (,) <$> tuples s <*> tuples t
data Table :: * -> * -> * where
Ret :: x -> Table () x
Cond :: x -> x -> Table Bool x
Curry :: Table s (Table t x) -> Table (s, t) x
deriving instance Show x => Show (Table t x)
deriving instance Eq x => Eq (Table t x)
instance Ord x => Ord (Table t x) where
compare (Ret x) (Ret y) = compare x y
compare (Cond x0 y0) (Cond x1 y1) = compare (x0, y0) (x1, y1)
compare (Curry f) (Curry g) = compare f g
($$) :: Table s x -> s -> x
Ret x $$ () = x
Cond f t $$ False = f
Cond f t $$ True = t
Curry f $$ (s, t) = f $$ s $$ t
infixl 3 $$
instance Traversable (Table i) where
traverse f (Ret s) = Ret <$> f s
traverse f (Cond s0 s1) = Cond <$> f s0 <*> f s1
traverse f (Curry st) = Curry <$> traverse (traverse f) st
instance Foldable (Table i) where foldMap = foldMapDefault
instance Functor (Table i) where fmap = fmapDefault
lambda :: TUPLY s => (s -> x) -> Table s x
lambda = lambdaEx tuply
lambdaEx :: Tuply s -> (s -> x) -> Table s x
lambdaEx Oney f = Ret (f ())
lambdaEx Booly f = Cond (f False) (f True)
lambdaEx (s :*: t) f = Curry (lambdaEx s $ \ u -> lambdaEx t $ \ v -> f (u, v))
instance TUPLY i => Applicative (Table i) where
pure x = lambda $ \ _ -> x
f <*> s = lambda $ \ i -> (f $$ i) (s $$ i)
type Component s i o = Table (s, i) (s, o)
comb :: TUPLY i => (i -> o) -> Component () i o
comb f = lambda $ \ ((), i) -> ((), f i)
(-$) :: Component () i o -> i -> o
f -$ i = snd (f $$ ((), i))
gNAND :: Component () (Bool, Bool) Bool
gNAND = comb $ (not . uncurry (&&))
gDFF :: Component Bool Bool Bool
gDFF = lambda $ \ (q, d) -> (d, q)
gNOT :: Component () Bool Bool
gNOT = comb $ \ b -> gNAND -$ (b, b)
gAND :: Component () (Bool, Bool) Bool
gAND = comb $ \ x -> case x of
(a, b) -> z where
c = gNAND -$ (a, b)
z = gNOT -$ c
gOR :: Component () (Bool, Bool) Bool
gOR = comb $ \ x -> case x of
(a, b) -> z where
a' = gNOT -$ a
b' = gNOT -$ b
z = gNAND -$ (a', b')
gMUX :: Component () (Bool, (Bool, Bool)) Bool
gMUX = comb $ \ x -> case x of
(c, (b0, b1)) -> z where
c' = gNOT -$ c
x = gAND -$ (c', b0)
y = gAND -$ (c, b1)
z = gOR -$ (x, y)
gJKFF :: Component Bool (Bool, Bool) Bool
gJKFF = lambda $ \ x -> case x of
(q, (j, k)) -> (q', d') where
(q', d') = gDFF $$ (q, d)
k' = gNOT -$ k
d = gMUX -$ (q, (j, k'))
gXOR :: Component () (Bool, Bool) Bool
gXOR = comb $ \ x -> case x of
(a, b) -> z where
z = gMUX -$ (a, (b, b'))
b' = gNOT -$ b
gTFF0 :: Component Bool Bool Bool
gTFF0 = lambda $ \ x -> case x of
(q, t) -> (q', d') where
(q', d') = gDFF $$ (q, d)
d = gXOR -$ (q, t)
gTFF1 :: Component Bool Bool Bool
gTFF1 = lambda $ \ x -> case x of
(q, t) -> (q', jk') where
(q', jk') = gJKFF $$ (q, (t, t))
gCOUNT2 :: Component (Bool, Bool) () (Bool, Bool)
gCOUNT2 = lambda $ \ x -> case x of
((q2, q1), ()) -> ((q2', q1'), (t2', t1')) where
(q1', t1') = gTFF0 $$ (q1, True)
(q2', t2') = gTFF0 $$ (q2, t1')
gCOUNT3 :: Component (Bool, (Bool, Bool)) () (Bool, (Bool, Bool))
gCOUNT3 = lambda $ \ x -> case x of
((q4, (q2, q1)), ()) -> ((q4', (q2', q1')), (t4', (t2', t1'))) where
(q1', t1') = gTFF0 $$ (q1, True)
(q2', t2') = gTFF0 $$ (q2, t1')
(q4', t4') = gTFF0 $$ (q4, t4)
t4 = gAND -$ (t2', t1')
gCOUNT2' :: Component (Bool, (Bool, Bool)) () (Bool, Bool)
gCOUNT2' = lambda $ \ x -> case x of
sv -> (s, t21') where
(s, (_, t21')) = gCOUNT3 $$ sv
data PART = P | PART :+: PART deriving (Show, Eq)
{-
data PARTy :: PART -> * where
Py :: PARTy P
(:++:) :: PARTy p -> PARTy q -> PARTy (p :+: q)
deriving instance Show (PARTy p)
deriving instance Eq (PARTy p)
-}
data Parted :: PART -> * -> * where
Part :: [x] -> Parted P x
Sep :: Parted p x -> Parted q x -> Parted (p :+: q) x
deriving instance Show x => Show (Parted p x)
deriving instance Eq x => Eq (Parted p x)
similar :: Parted p x -> Parted q y -> Bool
similar (Part _) (Part _) = True
similar (Sep a b) (Sep c d) = similar a c && similar b d
similar _ _ = False
data Pos :: PART -> * where
X :: Pos P
L :: Pos p -> Pos (p :+: q)
R :: Pos q -> Pos (p :+: q)
deriving instance Show (Pos p)
deriving instance Eq (Pos p)
instance Ord (Pos p) where
compare X X = EQ
compare (L x) (L y) = compare x y
compare (L x) (R y) = LT
compare (R x) (L y) = GT
compare (R x) (R y) = compare x y
unL :: Pos (p :+: q) -> Maybe (Pos p)
unL (L x) = Just x
unL (R y) = Nothing
unR :: Pos (p :+: q) -> Maybe (Pos q)
unR (L y) = Nothing
unR (R x) = Just x
part :: Parted p x -> Pos p -> [x]
part (Part xs) X = xs
part (Sep ps qs) (L x) = part ps x
part (Sep ps qs) (R x) = part qs x
data Partition :: * -> * where
Partition :: Parted p t -> Table t (Pos p) -> Partition t
-- invariant: Partition ts f satisfies
-- elem t (part ts x) = True <=> f $$ t = x
deriving instance Show t => Show (Partition t)
allInOne :: TUPLY t => Partition t
allInOne = allInOneEx tuply
allInOneEx :: Tuply t -> Partition t
allInOneEx ty = Partition (Part (tuples ty)) (lambdaEx ty $ \ _ -> X)
refine :: forall t x. (Eq t, Ord x, TUPLY t) => Partition t -> (t -> x) -> Partition t
refine (Partition p f) ob = case refineSub (SubPartition p (Just . (f $$))) ob of
SubPartition q g -> Partition q (lambda $ \ t -> case g t of Just x -> x)
data SubPartition :: * -> * where
SubPartition :: Parted p t -> (t -> Maybe (Pos p)) -> SubPartition t
-- invariant: SubPartition ts f satisfies
-- elem t (part ts x) = True <=> f t = Just x
pairSub :: SubPartition t -> SubPartition t -> SubPartition t
pairSub (SubPartition p f) (SubPartition q g) = SubPartition (Sep p q) $ \ t ->
L <$> f t <|> R <$> g t
refineSub :: forall t x. (Eq t, Ord x) => SubPartition t -> (t -> x) -> SubPartition t
refineSub (SubPartition (Part ts) f) ob = subPartition (groupSortOn ob ts)
refineSub (SubPartition (Sep p q) f) ob = pairSub
(refineSub (SubPartition p (unL <=< f)) ob)
(refineSub (SubPartition q (unR <=< f)) ob)
deal :: [x] -> ([x], [x])
deal [] = ([], [])
deal (x : xs) = (x : zs, ys) where (ys, zs) = deal xs
subPartition :: Eq t => [[t]] -> SubPartition t
subPartition [ts] = SubPartition (Part ts) $ \ t -> if elem t ts then Just X else Nothing
subPartition tss = uncurry pairSub . (subPartition *** subPartition) $ deal tss
analyse :: forall s i o. (Eq s, Ord o, TUPLY s, TUPLY i) => Component s i o -> Partition s
analyse c = iter start where
start :: Partition s
start = refine allInOne $ \ s -> lambda $ \ i -> snd (c $$ (s, i))
iter :: Partition s -> Partition s
iter x@(Partition p f) = case refine x next of
y@(Partition q _)
| similar p q -> y
| otherwise -> iter y
where next s = lambda $ \ i -> f $$ (fst (c $$ (s, i)))
data Machine :: * -> * -> * -> * where
Machine :: TUPLY i =>
Parted p s -> [(Table i o, [(Pos p, Table i (Pos p))])] -> Machine s i o
instance (Show s, Show o) => Show (Machine s i o) where
show (Machine p a) = concat ["Machine (", show p, ") (", show a, ")"]
machine :: forall s i o. (Eq s, Ord o, TUPLY s, TUPLY i) =>
Component s i o -> Machine s i o
machine c = case analyse c of
Partition p f -> Machine p (foldMapArr (:[]) (go p f))
where
go :: Parted p s -> Table s (Pos q) -> Arr (Table i o) [(Pos p, Table i (Pos q))]
go (Part (s : _)) f = single ( lambda $ \ i -> snd (c $$ (s, i))
, [(X, lambda $ \ i -> f $$ fst (c $$ (s, i)))]
)
go (Sep p q) f = fmap (fmap (L *** id)) (go p f) <> fmap (fmap (R *** id)) (go q f)
picks :: [x] -> [(x, [x])]
picks [] = []
picks (x : xs) = (x, xs) : [(y, x : ys) | (y, ys) <- picks xs]
mkBisim :: forall b s t i. (Eq b, Ord s, Eq t, TUPLY i) =>
Arr s t -> [(b, [(s, Table i s)])] -> [(b, [(t, Table i t)])] -> [Arr s t]
mkBisim r [] [] = return r
mkBisim r ((b, sfs) : bsfss) ((b', tgs) : btgss) | b == b' = do
(r, sfs, tgs) <- precook r sfs tgs
r <- guess r sfs tgs
mkBisim r bsfss btgss
where
precook r [] tgs = [(r, [], tgs)]
precook r (sf@(s, f) : sfs) tgs = case findArr s r of
Nothing -> do
(r, sfs, tgs) <- precook r sfs tgs
return (r, sf : sfs, tgs)
Just t -> case [(g, tgs') | ((t', g), tgs') <- picks tgs] of
[(g, tgs)] -> do
r <- foldr ((<=<) . match) return ((,) <$> f <*> g) r
precook r sfs tgs
_ -> []
match :: (s, t) -> Arr s t -> [Arr s t]
match (s, t) r = case findArr s r of
Just t' -> if t == t' then return r else []
Nothing -> return (insertArr (s, t) r)
guess r [] [] = return r
guess r ((s, f) : sfs) tgs = do
((t, g), tgs) <- picks tgs
r <- foldr ((<=<) . match) return ((,) <$> f <*> g) r
(r, sfs, tgs) <- precook (insertArr (s, t) r) sfs tgs
guess r sfs tgs
mkBisim r _ _ = []
data Bisims :: * -> * -> * -> * -> * where
Bisims :: (Parted p s, [(Table i o, [(Pos p, Table i (Pos p))])]) ->
(Parted q t, [(Table i o, [(Pos q, Table i (Pos q))])]) ->
[Arr (Pos p) (Pos q)] ->
Bisims s t i o
instance (Show s, Show t, Show o) => Show (Bisims s t i o) where
show (Bisims lm rm bs) = concat
["Bisims (", show lm, ") (", show rm, ") (", show bs, ")"]
bisimulations :: (TUPLY s, TUPLY t, TUPLY i, Ord o) =>
Component s i o -> Component t i o -> Bisims s t i o
bisimulations c d = case (machine c, machine d) of
(Machine p lm, Machine q rm) -> Bisims (p, lm) (q, rm) (mkBisim emptyArr lm rm)