/
Expr.hs
769 lines (735 loc) · 26.4 KB
/
Expr.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
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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}
module CSPM.Evaluator.Expr (
eval,
) where
import qualified Data.ByteString.Char8 as B
import Data.List (sort)
import qualified Data.Foldable as F
import qualified Data.Map as M
import qualified Data.Set as St
import qualified Data.Traversable as T
import CSPM.Syntax.FreeVars
import CSPM.Syntax.Literals
import CSPM.Syntax.Names
import CSPM.Syntax.AST
import CSPM.Syntax.Types
import CSPM.Evaluator.AnalyserMonad
import CSPM.Evaluator.BuiltInFunctions
import CSPM.Evaluator.DeclBind
import CSPM.Evaluator.Dot
import CSPM.Evaluator.Exceptions
import CSPM.Evaluator.Monad
import CSPM.Evaluator.PatBind
import CSPM.Evaluator.PrefixExpr
import CSPM.Evaluator.Values
import qualified CSPM.Evaluator.ValueSet as S
import Util.Annotated
import Util.Exception
import Util.List
-- In order to keep lazy evaluation working properly only use pattern
-- matching when you HAVE to know the value. (Hence why we delay pattern
-- matching in BooleanBinaryOp And in case the first value is false.)
eval :: TCExp -> AnalyserMonad (EvaluationMonad Value)
eval (An _ _ (App func args)) = do
func <- eval func
args <- mapM eval args
return $! do
vs <- sequence args
VFunction _ f <- func
f vs
eval (An _ _ (BooleanBinaryOp op e1 e2)) = do
e1 <- eval e1
e2 <- eval e2
let fn = case op of
And -> \ v1 v2 ->
let
VBool b1 = v1
-- This is lazy, only pattern matches if b2 is required.
VBool b2 = v2
in b1 && b2
Or -> \ v1 v2 ->
let
VBool b1 = v1
-- This is lazy, only pattern matches if b2 is required.
VBool b2 = v2
in b1 || b2
-- TODO: optimise these calls so that compareValues takes a target comparator
Equals -> \ v1 v2 -> compareValues v1 v2 == Just EQ
NotEquals -> \ v1 v2 -> compareValues v1 v2 /= Just EQ
LessThan -> \ v1 v2 -> compareValues v1 v2 == Just LT
GreaterThan -> \ v1 v2 -> compareValues v1 v2 == Just GT
LessThanEq -> \ v1 v2 -> compareValues v1 v2 `elem` [Just LT, Just EQ]
GreaterThanEq -> \ v1 v2 -> compareValues v1 v2 `elem` [Just GT, Just EQ]
return $! do
v1 <- e1
v2 <- e2
return $! makeBoolValue $ fn v1 v2
eval (An _ _ (BooleanUnaryOp Not e)) = do
e <- eval e
return $! do
VBool b <- e
return $ makeBoolValue (not b)
eval (An _ _ (Concat e1 e2)) = do
e1 <- eval e1
e2 <- eval e2
return $! do
VList vs1 <- e1
v2 <- e2
-- If we instead wrote VList v2 <- eval e2
-- then this would force evaluation of e2 to a list immediately.
-- However, if we do the following instead this means that
-- the second argument is only evaluated if it absolutely has to
-- be (what if e2 was bottom and we were taking head(e1^e2)).
-- (To see why haskell does this consider the desugared form with
-- the do's removed. It would be:
-- ... eval e1) >>= (\ (VList vs2) -> ...)
-- and therefore the pattern match would force evaluation.)
let VList vs2 = v2
return $ VList (vs1++vs2)
eval e@(An _ _ (DotApp _ _)) = evaluateDotApplication e
eval (An _ _ (If e1 e2 e3)) = do
e1 <- eval e1
e2 <- eval e2
e3 <- eval e3
return $! do
VBool b <- e1
if b then e2 else e3
eval exp@(An loc _ (Lambda ps e)) = do
binder <- bindAll ps
outerFrameInfo <- createLambdaFrame ps e return
createLambdaFrame ps e $! \ _ -> do
e <- eval e
return $! do
fid <- instantiateFrame outerFrameInfo
createFunction fid $! \ vs ->
case binder vs of
Just binds -> addScopeAndBind binds e
Nothing -> throwError $ patternMatchesFailureMessage loc ps vs
eval (An _ _ (Let decls e)) = do
analyseRelevantVars decls $! do
decls <- bindDecls decls
e <- eval e
return $! do
nvs <- decls
addScopeAndBindM nvs e
eval (An _ _ (Lit (Int i))) = return $! return $! VInt i
eval (An _ _ (Lit (Bool b))) = return $! return $! makeBoolValue b
eval (An _ _ (Lit (Char c))) = return $! return $! VChar c
eval (An _ _ (Lit (Loc l))) = return $! return $! VLoc l
eval (An _ _ (Lit (String s))) =
let
cs = map VChar (B.unpack s)
in return $! return $! VList cs
eval (An _ _ (List es)) = do
es <- mapM eval es
return $! sequence es >>= return . VList
eval (An _ _ (ListComp es stmts)) = do
stmts <- evalStmts (\(VList xs) -> xs) stmts (map eval es)
return $ stmts >>= return . VList
eval (An _ _ (ListEnumFrom e)) = do
e <- eval e
return $! do
VInt lb <- e
return $ VList $ map VInt [lb..]
eval (An _ _ (ListEnumFromTo e1 e2)) = do
e1 <- eval e1
e2 <- eval e2
return $! do
VInt lb <- e1
VInt ub <- e2
return $ VList $ map VInt [lb..ub]
eval (An _ _ (ListEnumFromComp e1 stmts)) = do
stmts <- evalStmts (\ (VList xs) -> xs) stmts [do
e1 <- eval e1
return $ do
VInt lb <- e1
return $ map VInt [lb..]
]
return $ do
ss <- stmts
return $! VList $ concat ss
eval (An _ _ (ListEnumFromToComp e1 e2 stmts)) = do
stmts <- evalStmts (\ (VList xs) -> xs) stmts [do
e1 <- eval e1
e2 <- eval e2
return $! do
VInt lb <- e1
VInt ub <- e2
return $ map VInt [lb..ub]
]
return $ do
ss <- stmts
return $! VList $ concat ss
eval (An _ _ (ListLength e)) = do
e <- eval e
return $! do
VList xs <- e
return $ VInt (length xs)
eval (An _ _ (Map kvs)) = do
xs <- mapM (\ (k, v) -> do
k <- eval k
v <- eval v
return (k, v)) kvs
return $ do
xs <- mapM (\ (k, v) -> do
k <- k
v <- v
return (k, v)) xs
return $! VMap $ M.fromList xs
eval (An loc _ (MathsBinaryOp op e1 e2)) = do
e1 <- eval e1
e2 <- eval e2
let fn = case op of
Divide -> \ i1 i2 -> do
case i2 of
0 -> throwError $ divideByZeroMessage loc Nothing
_ -> return $ VInt (i1 `div` i2)
Minus -> \ i1 i2 -> return $ VInt (i1 - i2)
Mod -> \ i1 i2 -> return $ VInt (i1 `mod` i2)
Plus -> \ i1 i2 -> return $ VInt (i1 + i2)
Times -> \ i1 i2 -> return $ VInt (i1 * i2)
return $! do
VInt i1 <- e1
VInt i2 <- e2
fn i1 i2
eval (An _ _ (MathsUnaryOp Negate e)) = do
e <- eval e
return $! do
VInt i <- e
return $ VInt (-i)
eval (An _ _ (Paren e)) = eval e
eval (An _ _ (Set es)) = do
es <- mapM eval es
return $! sequence es >>= return . VSet . S.fromList
eval (An _ _ (SetComp es stmts)) = do
stmts <- evalStmts (\ (VSet s) -> S.toList s) stmts $! map eval es
return $! do
xs <- stmts
return $ VSet (S.fromList xs)
eval (An _ _ (SetEnum es)) = do
es <- mapM eval es
return $! do
evs <- sequence es
ss <- mapM productionsSet evs
return $ VSet (S.unions ss)
eval (An _ _ (SetEnumComp es stmts)) = do
stmts <- evalStmts (\(VSet s) -> S.toList s) stmts $ map (\ e -> do
e <- eval e
return $! e >>= productionsSet) es
return $! do
ss <- stmts
return $ VSet (S.unions ss)
eval (An _ _ (SetEnumFrom e)) = do
e <- eval e
return $! do
VInt lb <- e
return $ VSet $ S.IntSetFrom lb
eval (An _ _ (SetEnumFromTo e1 e2)) = do
e1 <- eval e1
e2 <- eval e2
return $! do
VInt lb <- e1
VInt ub <- e2
return $ VSet $ S.fromList $ map VInt [lb..ub]
eval (An _ _ (SetEnumFromComp e1 stmts)) = do
stmts <- evalStmts (\ (VSet s) -> S.toList s) stmts [do
e1 <- eval e1
return $! do
VInt lb <- e1
return $! S.IntSetFrom lb
]
return $! do
ss <- stmts
return $ VSet $ S.unions ss
eval (An _ _ (SetEnumFromToComp e1 e2 stmts)) = do
stmts <- evalStmts (\ (VSet s) -> S.toList s) stmts [do
e1 <- eval e1
e2 <- eval e2
return $! do
VInt lb <- e1
VInt ub <- e2
return $! S.fromList (map VInt [lb..ub])
]
return $! do
ss <- stmts
return $ VSet $ S.unions ss
eval (An _ _ (Tuple es)) = do
es <- mapM eval es
return $! do
vs <- sequence es
return $ tupleFromList vs
eval (An _ _ (Var n)) | isNameDataConstructor n = return $ do
(dc, _, _) <- dataTypeInfo n
return dc
eval (An _ _ (Var n)) = return $! lookupVar n
eval (an@(An _ _ (Prefix e1 fs e2))) = evalPrefix an
eval (An _ _ (TimedPrefix n e)) =
maybeTimed (panic "Timed prefix in non-timed section") $ \ tockName fnName -> do
createFunctionFrame n [] e $! \frameInfo -> do
e <- eval e
return $ do
VProc p <- e
pn <- makeProcessName frameInfo
VFunction _ eventFunc <- lookupVar fnName
let addTocker (POp PExternalChoice ps) = do
ps' <- T.mapM addTocker ps
return $! POp PExternalChoice ps'
addTocker (PUnaryOp (PPrefixEventSet evs) p) =
let ps = fmap (\ev -> PUnaryOp (PPrefix ev) p) (F.toList evs)
in addTocker (POp PExternalChoice ps)
addTocker (PUnaryOp (PPrefix ev) p) = do
let UserEvent ev' = ev
VInt tockCount <- eventFunc [ev']
return $! PUnaryOp (PPrefix ev) (makeTocker tockName tockCount p)
addTocker p = return p
p' <- addTocker p
let
tocker = PUnaryOp (PPrefix (tock tockName)) procCall
mainProc = POp PExternalChoice [tocker, p']
procCall = PProcCall pn mainProc
return $ VProc procCall
eval (An _ _ (AlphaParallel e1 e2 e3 e4)) = do
e1 <- eval e1
e2 <- timedCSPSyncSet $ eval e2
e3 <- timedCSPSyncSet $ eval e3
e4 <- eval e4
return $! do
VProc p1 <- e1
VProc p2 <- e4
VSet a1 <- e2
VSet a2 <- e3
return $ VProc $ POp (PAlphaParallel
[S.valueSetToEventSet a1, S.valueSetToEventSet a2])
[p1, p2]
eval (An _ _ (Exception e1 e2 e3)) = do
e1 <- eval e1
e2 <- eval e2
e3 <- eval e3
return $! do
VProc p1 <- e1
VSet a <- e2
VProc p2 <- e3
return $ VProc $ PBinaryOp (PException (S.valueSetToEventSet a)) p1 p2
eval (An _ _ (ExternalChoice e1 e2)) = do
e1 <- eval e1
e2 <- eval e2
op <- maybeTimed
(return $ VProc . POp PExternalChoice)
(\ tn _ -> return $ VProc . POp (PSynchronisingExternalChoice (tockSet tn)))
return $! do
VProc p1 <- e1
VProc p2 <- e2
return $! op [p1, p2]
eval (An _ _ (GenParallel e1 e2 e3)) = do
e1 <- eval e1
e2 <- timedCSPSyncSet $ eval e2
e3 <- eval e3
return $! do
VProc p1 <- e1
VSet a <- e2
VProc p2 <- e3
return $ VProc $ POp (PGenParallel (S.valueSetToEventSet a)) [p1, p2]
eval (An _ _ (GuardedExp guard proc)) = do
guard <- eval guard
proc <- eval proc
stop <- maybeTimed
(return $! lookupVar (builtInName "STOP"))
(\ tn _ -> return $! do
VFunction _ fn <- lookupVar (builtInName "TSTOP")
fn [tockValue tn])
return $! do
VBool b <- guard
if b then proc
else stop
eval (An _ _ (Hiding e1 e2)) = do
e1 <- eval e1
e2 <- eval e2
return $! do
VProc p <- e1
VSet s <- e2
if S.empty s then return $ VProc p
else return $ VProc $ PUnaryOp (PHide (S.valueSetToEventSet s)) p
eval (An _ _ (InternalChoice e1 e2)) = do
e1 <- eval e1
e2 <- eval e2
return $! do
VProc p1 <- e1
VProc p2 <- e2
return $ VProc $ POp PInternalChoice [p1, p2]
eval (An _ _ (Interrupt e1 e2)) = do
e1 <- eval e1
e2 <- eval e2
op <- maybeTimed
(return PInterrupt)
(\ tn _ -> return $ PSynchronisingInterrupt (tockSet tn))
return $! do
VProc p1 <- e1
VProc p2 <- e2
return $! VProc $ PBinaryOp op p1 p2
eval (An _ _ (Interleave e1 e2)) = do
e1 <- eval e1
e2 <- eval e2
op <- maybeTimed
(return PInterleave)
(\ tn _ -> return $ PGenParallel (tockSet tn))
return $! do
VProc p1 <- e1
VProc p2 <- e2
return $ VProc $ POp op [p1, p2]
eval (An _ _ (LinkParallel e1 ties stmts e2)) = do
e1 <- eval e1
ties <- evalTies stmts ties
e2 <- eval e2
return $! do
VProc p1 <- e1
VProc p2 <- e2
ts <- ties
return $ VProc $ PBinaryOp (PLinkParallel (removeDuplicateTies ts)) p1 p2
eval (An _ _ (Project e1 e2)) = do
e1 <- eval e1
e2 <- eval e2
return $! do
VProc p <- e1
VSet s <- e2
return $ VProc $ PUnaryOp (PProject (S.valueSetToEventSet s)) p
eval (An _ _ (Rename e1 ties stmts)) = do
e1 <- eval e1
ties <- evalTies stmts ties
return $! do
VProc p1 <- e1
ts <- ties
case ts of
[] -> return $! VProc p1
_ -> return $! VProc $! PUnaryOp (PRename (removeDuplicateTies ts)) p1
eval (An _ _ (SequentialComp e1 e2)) = do
e1 <- eval e1
e2 <- eval e2
return $! do
VProc p1 <- e1
VProc p2 <- e2
return $ VProc $ PBinaryOp PSequentialComp p1 p2
eval (An _ _ (SlidingChoice e1 e2)) = do
e1 <- eval e1
e2 <- eval e2
return $! do
VProc p1 <- e1
VProc p2 <- e2
return $ VProc $ PBinaryOp PSlidingChoice p1 p2
eval (An _ _ (SynchronisingExternalChoice e1 e2 e3)) = do
e1 <- eval e1
e2 <- timedCSPSyncSet $ eval e2
e3 <- eval e3
return $! do
VProc p1 <- e1
VSet a <- e2
VProc p2 <- e3
return $ VProc $ POp
(PSynchronisingExternalChoice (S.valueSetToEventSet a))
[p1, p2]
eval (An _ _ (SynchronisingInterrupt e1 e2 e3)) = do
e1 <- eval e1
e2 <- timedCSPSyncSet $ eval e2
e3 <- eval e3
return $! do
VProc p1 <- e1
VSet a <- e2
VProc p2 <- e3
return $ VProc $
PBinaryOp (PSynchronisingInterrupt (S.valueSetToEventSet a)) p1 p2
eval (An _ _ (ReplicatedAlphaParallel stmts e1 e2)) = do
stmts <- evalStmts (\(VSet s) -> S.toList s) stmts [do
e1 <- timedCSPSyncSet $ eval e1
e2 <- eval e2
return $! do
VSet s <- e1
VProc p <- e2
return (S.valueSetToEventSet s, p)]
tstop <- maybeTSTOP
return $! do
aps <- stmts
let (as, ps) = unzip aps
tstop ps $! return $! VProc $! POp (PAlphaParallel as) ps
eval (An _ _ (ReplicatedExternalChoice stmts e)) = do
stmts <- evalStmts (\(VSet s) -> S.toList s) stmts [evalProc e]
tstop <- maybeTSTOP
op <- maybeTimed
(return PExternalChoice)
(\ tn _ -> return $ PSynchronisingExternalChoice (tockSet tn))
return $! do
ps <- stmts
tstop ps (return $! VProc $! POp op $! ps)
eval (An _ _ (ReplicatedInterleave stmts e)) = do
stmts <- evalStmts (\(VSet s) -> S.toList s) stmts [evalProc e]
tstop <- maybeTSTOP
op <- maybeTimed
(return PInterleave)
(\tn _ -> return $ PGenParallel (tockSet tn))
return $! do
ps <- stmts
tstop ps (return $! VProc $! POp op $! ps)
eval (e'@(An _ _ (ReplicatedInternalChoice stmts e))) = do
stmts <- evalStmts (\(VSet s) -> S.toList s) stmts [evalProc e]
return $! do
ps <- stmts
case ps of
[] -> throwError' $ replicatedInternalChoiceOverEmptySetMessage e'
_ ->return $! VProc $! POp PInternalChoice $! ps
eval (An loc _ e'@(ReplicatedLinkParallel ties tiesStmts stmts e)) = do
stmts <- evalStmts (\(VList vs) -> vs) stmts [do
ties <- evalTies tiesStmts ties
e <- evalProc e
return $! do
ts <- ties
p <- e
return (ts, p)
]
let mkLinkPar [(_, p)] = p
mkLinkPar ((ts, p1):ps) =
PBinaryOp (PLinkParallel (removeDuplicateTies ts)) p1 (mkLinkPar ps)
return $! do
tsps <- stmts
case tsps of
[] -> throwError' $ replicatedLinkParallelOverEmptySeqMessage e' loc
_ -> return $! VProc $! mkLinkPar tsps
eval (An _ _ (ReplicatedParallel e1 stmts e2)) = do
e1 <- timedCSPSyncSet $ eval e1
stmts <- evalStmts (\(VSet s) -> S.toList s) stmts [evalProc e2]
tstop <- maybeTSTOP
return $! do
VSet s <- e1
ps <- stmts
tstop ps $ return $ VProc $ POp (PGenParallel (S.valueSetToEventSet s))
(ps)
eval (An _ _ (ReplicatedSequentialComp stmts e)) = do
stmts <- evalStmts (\(VList vs) -> vs) stmts [evalProc e]
skip <- maybeTimed
(return $ lookupVar (builtInName "SKIP"))
(\tn _ -> return $ tSKIP tn)
return $! do
ps <- stmts
case ps of
[] -> skip
_ -> return $ VProc $ foldr1 (PBinaryOp PSequentialComp) ps
eval (An _ _ (ReplicatedSynchronisingExternalChoice e1 stmts e2)) = do
e1 <- timedCSPSyncSet $ eval e1
stmts <- evalStmts (\(VSet s) -> S.toList s) stmts [evalProc e2]
tstop <- maybeTSTOP
return $! do
VSet a <- e1
ps <- stmts
tstop ps $ return $ VProc $ POp
(PSynchronisingExternalChoice (S.valueSetToEventSet a))
(ps)
eval e = panic ("No clause to eval "++show e)
evalProc :: TCExp -> AnalyserMonad (EvaluationMonad Proc)
evalProc e = do
prog <- eval e
return $! do
VProc p <- prog
return p
removeDuplicateTies :: [(Event, Event)] -> [(Event, Event)]
removeDuplicateTies = sortedNub . sort
evalTies :: [TCStmt] -> [(TCExp, TCExp)] ->
AnalyserMonad (EvaluationMonad [(Event, Event)])
evalTies stmts ties = do
tss <- evalStmts (\(VSet s) -> S.toList s) stmts (map evalTie ties)
return $ do
vss <- tss
return $! concat vss
where
extendTie :: SrcSpan -> SrcSpan -> (Value, Value) -> Value ->
EvaluationMonad (Event, Event)
extendTie loc1 loc2 (evOld, evNew) ex = do
ev1 <- combineDots loc1 evOld ex
ev2 <- combineDots loc2 evNew ex
return (valueEventToEvent ev1, valueEventToEvent ev2)
evalTie :: (TCExp, TCExp) ->
AnalyserMonad (EvaluationMonad [(Event, Event)])
evalTie (eOld, eNew) = do
eOld' <- eval eOld
eNew' <- eval eNew
return $! do
evOld <- eOld'
evNew <- eNew'
-- Obviously evOld and evNew could be channels, or prefixes
-- of events so we compute the extensions.
exsOld <- extensions evOld
mapM (\ex -> extendTie (loc eOld) (loc eNew) (evOld, evNew) ex) exsOld
-- TODO: modify so that if the statement generators are independent, only
-- compute each set once, rather than once for each value.
-- | Evaluates the statements, evaluating each prog in progs for each possible
-- assingment to the generators that satisfies the qualifiers.
evalStmts :: (Value -> [Value]) -> [TCStmt] ->
[AnalyserMonad (EvaluationMonad a)] ->
AnalyserMonad (EvaluationMonad [a])
evalStmts extract stmts progs =
let
-- | Progressively generates new values lazily
evStmts [] = do
progs <- sequence progs
return $! sequence progs
evStmts (An _ _ (Qualifier e):stmts) = do
e <- eval e
rest <- evStmts stmts
return $! do
VBool b <- e
if b then rest else return []
evStmts (An _ _ (Generator p e):stmts) = do
e <- eval e
binder <- bind p
rest <- createVariableFrame' p $ evStmts stmts
return $! do
v <- e
vss <- mapM (\v -> do
case binder v of
Just binds -> addScopeAndBind binds rest
Nothing -> return []) (extract v)
return $ concat vss
isGenerator (An _ _ (Generator _ _)) = True
isGenerator _ = False
generators = filter isGenerator stmts
generatorFvs = St.fromList (freeVars generators)
generatorsIndependent = and $!
map (not . flip St.member generatorFvs) (boundNames generators)
evGeneratorSets :: [TCStmt] -> AnalyserMonad (EvaluationMonad [Value])
evGeneratorSets [] = return $! return []
evGeneratorSets (An _ _ (Qualifier _):stmts) = evGeneratorSets stmts
evGeneratorSets (An _ _ (Generator _ e):stmts) = do
e <- eval e
rest <- evGeneratorSets stmts
return $! do
v <- e
vs <- rest
return $! v : vs
evBinders [] = do
progs <- sequence progs
return $! \ _ -> sequence progs
evBinders (An _ _ (Qualifier e):stmts) = do
e <- eval e
rest <- evBinders stmts
return $! \ sets -> do
VBool b <- e
if b then rest sets else return []
evBinders (An _ _ (Generator p _):stmts) = do
binder <- bind p
rest <- createVariableFrame' p $ evBinders stmts
return $! \ (set:sets) -> do
vss <- mapM (\v -> do
case binder v of
Just binds -> addScopeAndBind binds (rest sets)
Nothing -> return []) (extract set)
return $ concat vss
in
-- If there are multiple generators that are independent, split the
-- evaluation into two stages: firstly compute the sets, and then take
-- the cartesian product and do the binding etc. This has the advantage
-- of only computing the sets once each, rather than computing the
-- subsequent sets for *each* value of the first generator.
if generatorsIndependent && length generators > 1 then do
setGenerator <- evGeneratorSets stmts
binderGenerator <- evBinders stmts
return $! do
sets <- setGenerator
binderGenerator sets
else
evStmts stmts
timedCSPSyncSet ::
AnalyserMonad (EvaluationMonad Value) ->
AnalyserMonad (EvaluationMonad Value)
timedCSPSyncSet prog = do
maybeTimed prog $! \ tn _ -> do
prog <- prog
let set = S.fromList [tockValue tn]
return $! do
VSet a <- prog
return $ VSet (S.union set a)
tSTOP :: Name -> EvaluationMonad Value
tSTOP tockName = do
VFunction _ tstop <- lookupVar (builtInName "TSTOP")
tstop [tockValue tockName]
tSKIP :: Name -> EvaluationMonad Value
tSKIP tockName = do
VFunction _ tskip <- lookupVar (builtInName "TSKIP")
tskip [tockValue tockName]
maybeTSTOP :: AnalyserMonad
([a] -> EvaluationMonad Value -> EvaluationMonad Value)
maybeTSTOP =
maybeTimed
(return $ \ _ prog -> prog)
(\ tn _ -> return $ \ xs p1 ->
case xs of
[] -> tSTOP tn
_ -> p1)
tock :: Name -> Event
tock tn = UserEvent (tockValue tn)
tockValue :: Name -> Value
tockValue tn = VDot [VChannel tn]
tockSet :: Name -> EventSet
tockSet tn = [tock tn]
makeTocker :: Name -> Int -> Proc -> Proc
makeTocker tn 0 p = p
makeTocker tn tocks p =
PUnaryOp (PPrefix (tock tn)) (makeTocker tn (tocks-1) p)
dataTypeTypeName :: Type -> Name
dataTypeTypeName (TDatatype n) = n
dataTypeTypeName TEvent = builtInName "Events"
dataTypeTypeName t = panic $ show t ++ " is not a datatype type name."
-- | Evaluates a dot application, attempting to optimise it.
evaluateDotApplication :: TCExp -> AnalyserMonad (EvaluationMonad Value)
evaluateDotApplication (exp@(An loc _ (DotApp left right))) = do
let
leftMostDot (An _ _ (DotApp l r)) = (leftMost, args ++ [r])
where (leftMost, args) = leftMostDot l
leftMostDot x = (x, [])
(leftMostConstructor, arguments) = leftMostDot exp
findFields :: [Type] -> [TCExp] -> Maybe [TCExp]
findFields [] _ = panic "Empty type list for find fields"
findFields [t] [exp] | getType exp == t = Just [exp]
findFields (t:ts) (exp:exps) | t == getType exp =
case findFields ts exps of
Just fs -> Just $ exp : fs
Nothing -> Nothing
findFields (t:ts) (An _ typ (DotApp l r) : exps) =
-- We know t != typ, so we try splitting
findFields (t:ts) (l:r:exps)
findFields _ _ = Nothing
isDotable (TDotable _ _) = True
isDotable (TExtendable _ _) = True
isDotable _ = False
catVDots v v'@(VDot (VDataType _ : vs)) = VDot [v, v']
catVDots v v'@(VDot (VChannel _ : vs)) = VDot [v, v']
catVDots v (VDot vs) = VDot (v:vs)
catVDots v v' = VDot [v, v']
fallback = do
e1 <- eval left
e2 <- eval right
if not (isDotable (getType left)) then
return $! do
v1 <- e1
v2 <- e2
return $! catVDots v1 v2
else return $! do
v1 <- e1
v2 <- e2
combineDots loc v1 v2
if isDataTypeOrEvent (getType exp) then
case leftMostConstructor of
An _ _ (Var n) -> do
dataType <- dataTypeForName (dataTypeTypeName (getType exp))
case M.lookup n (dataTypeConstructors dataType) of
Just clause | constructorFieldCount clause > 0
&& and (constructorFieldSetIsTrivial clause) ->
case findFields (constructorFieldTypes clause) arguments of
Just fs -> do
computeFields <- mapM eval fs
let constructor =
case getType exp of
TEvent -> VChannel n
TDatatype _ -> VDataType n
return $! do
fs <- sequence computeFields
return $! VDot $! constructor : fs
_ -> fallback
_ -> fallback
_ -> fallback
else fallback