-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathElaboration.hs
More file actions
1270 lines (1158 loc) · 48.2 KB
/
Elaboration.hs
File metadata and controls
1270 lines (1158 loc) · 48.2 KB
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
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module Elaboration where
import Control.Monad.Except
import Control.Monad.Reader
import Control.Monad.State
import Control.Monad.Writer
import Data.Function
import Data.List (isPrefixOf)
import qualified Data.List as List
import qualified Data.Map as Map
import qualified Data.Set as Set
import Data.Maybe
import Data.Bitraversable
import Actor
import Bwd
import Concrete.Base
import Format
import Hide
import Scope
import Syntax
( SyntaxCat,
SyntaxDesc, syntaxDesc)
import Thin
import Utils
import Info
import Machine.Matching
import Elaboration.Monad
import Term.Base
import Term.Substitution
import Pattern as P hiding (match)
import Location
import Data.List.NonEmpty (fromList)
import Pattern.Coverage (Covering'(..), combine, shrinkBy, missing)
import Control.Applicative ((<|>))
import Operator
import Operator.Eval
import Semantics
import Data.Bifunctor (bimap)
import GHC.Stack.Types (HasCallStack)
-- import Debug.Trace (traceShow, traceShowId, trace)
type CPattern = PATTERN Concrete
type APattern = PATTERN Abstract
dmesg = const id
isSubject :: EScrutinee -> IsSubject' ()
isSubject SubjectVar{} = IsSubject ()
isSubject _ = IsNotSubject
-- must be used in definition mode only
checkSendableSubject :: Raw -> Elab (Maybe ActorVar)
checkSendableSubject tm = do
localVars <- asks (getObjVars . objVars)
go (fmap objVarName localVars) tm
where
-- TODO: move this check to *after* elaboration? Might be easier.
go :: Bwd String -> Raw -> Elab (Maybe ActorVar)
go localVars x = case x of
Var r v -> resolve v >>= \case
Just (ADeclaration (ActVar (IsSubject {}) _)) -> pure . Just $ getVariable v
_ -> Nothing <$ raiseWarning tm (SentSubjectNotASubjectVar tm)
Sbst r sg x -> do
case isInvertible localVars sg of
Nothing -> Nothing <$ raiseWarning tm (SentSubjectNotASubjectVar tm)
Just localVars -> go localVars x
_ -> Nothing <$ raiseWarning tm (SentSubjectNotASubjectVar tm)
isInvertible :: Bwd String -> Bwd Assign -> Maybe (Bwd String)
isInvertible lvz B0 = pure lvz
{-
isInvertible (lvz :< w) (sz :< Keep _ v) | getVariable v == w
= (:< w) <$> isInvertible lvz sz
isInvertible (lvz :< w) (sz :< Drop _ v) | getVariable v == w
= isInvertible lvz sz
-}
isInvertible lvz (sz :< Assign _ v (Var _ w)) | Just (lz, x, ls) <- focus (getVariable w) lvz
= (:< getVariable v) <$> isInvertible (lz <>< ls) sz
isInvertible _ _ = Nothing
escrutinee :: EScrutinee -> ASemanticsDesc
escrutinee = \case
Pair _ p q -> Semantics.contract (Semantics.VCons (escrutinee p) (escrutinee q))
SubjectVar _ desc -> desc
Lookup _ desc _ -> desc
-- TODO : do we need to pass in the scope?
Compare _ t1 t2 -> Semantics.contract (Semantics.VEnumOrTag 0 ["LT", "EQ", "GT"] [])
Term _ desc -> desc
dual :: PROTOCOL ph -> PROTOCOL ph
dual (Protocol ps) = Protocol $ flip map ps $ \case
(Input, c) -> (Output, c)
(Subject x, c) -> (Subject x, c)
(Output, c) -> (Input, c)
data Comm = SEND | RECV
deriving (Eq, Show)
whatComm :: Mode a -> Direction -> Comm
whatComm m d = case m of
Input -> RECV
Subject _ -> case d of
Rootwards -> RECV
Leafwards -> SEND
Output -> SEND
isFresh :: Variable -> Elab String
isFresh x = do
res <- resolve x
whenJust res $ \ _ -> throwComplaint x (VariableShadowing x)
pure (getVariable x)
spassport :: Usage -> IsSubject -> Passport
spassport u IsSubject{} | isBeingScrutinised u = ASubject
spassport _ _ = ACitizen
smeta :: Range
-> Usage
-> ActorMeta {- eps -}
-> ACTSbst {- delta -} {- gamma -}
-> Telescopic ASemanticsDesc {- delta -} {- eps -}
-> Elab ({- T :: -} ASemanticsDesc {- gamma -}, ACTm {- gamma -} {- T -})
smeta r usage am sg (Stop desc) = pure (desc //^ sg, am $: sg)
smeta r usage am sg (Tele desc (Scope (Hide x) tel)) = do
t <- stm usage (desc //^ sg) (Var r $ Variable r x)
smeta r usage am (sbstT sg ((Hide x :=) $^ t)) tel
svar :: Usage
-> Maybe ASemanticsDesc
-> Variable
-> Elab (IsSubject, ASemanticsDesc, ACTm)
svar usage mdesc' x = do
let r = getRange x
ovs <- asks objVars
res <- resolve x
dmesg ("Looking at " ++ show x ++ ", objvars: " ++ show ovs) $ case res of
Just (ADeclaration k) -> case k of
ActVar isSub (sc :=> desc) -> do
logUsage (getVariable x) usage
let tel = discharge sc desc
let am = ActorMeta (spassport usage isSub) (getVariable x)
(desc, tm) <- smeta r usage am (sbst0 $ scopeSize ovs) tel
desc <- fmap (fromMaybe desc) $ flip traverse mdesc' $ \desc' -> do
i <- compatibleInfos r (Known desc') (Known desc)
fromInfo r i -- cannot possibly fail
pure desc
pure (isSub, desc, tm)
_ -> throwComplaint x (NotAValidTermVariable x k)
Just (AnObjVar desc i) -> do
desc <- fmap (fromMaybe desc) $ flip traverse mdesc' $ \desc' -> do
i <- compatibleInfos r (Known desc') (Known desc)
fromInfo r i -- cannot possibly fail
pure (IsNotSubject, desc, var i (scopeSize ovs))
Just (AMacro t) -> do
(desc, t) <- case mdesc' of
Nothing -> itm usage t
Just desc -> (desc,) <$> stm usage desc t
pure (IsNotSubject, desc, t)
Nothing -> throwComplaint x (OutOfScope x)
spop :: Range -> Elab (ObjVars, (Variable, ASemanticsDesc))
spop r = do
ovs <- asks objVars
case getObjVars ovs of
B0 -> throwComplaint r EmptyContext
(xz :< ObjVar x cat) -> pure (ObjVars xz, (Variable r x, cat))
ssyntaxdesc :: [SyntaxCat] -> Raw -> Elab SyntaxDesc
ssyntaxdesc syndecls syn = do
desc <- satom "Syntax"
syn <- withSyntax (syntaxDesc syndecls) $ stm DontLog desc syn
case isMetaFree syn of
Nothing -> error "Impossible in ssyntaxdesc" -- this should be impossible, since parsed in empty context
Just syn0 -> pure syn0
smacro :: Bwd String -> Raw -> Elab ()
smacro xz (Var r v) = do
unless (getVariable v `elem` xz) $ do
x <- resolve v
whenNothing x $ throwComplaint r (OutOfScope v)
smacro xz (At r a) = pure ()
smacro xz (Cons r t u) = do
smacro xz t
smacro xz u
smacro xz (Lam r (Scope (Hide x) sc)) = do
xz <- case x of
Unused _ -> pure xz
Used x -> do x <- isFresh x
pure (xz :< x)
smacro xz sc
smacro xz (Sbst r sg t) = do
xz <- smacros xz (sg <>> [])
smacro xz t
smacro xz (Op r obj opps) = do
smacro xz obj
smacro xz opps
smacro xz (Guarded r t) = smacro xz t
smacros :: Bwd String -> [Assign] -> Elab (Bwd String)
smacros xz [] = pure xz
smacros xz (Assign r x t : asss) = do
x <- isFresh x
smacro xz t
smacros (xz :< x) asss
ssbst :: [Assign] -> Elab Macros
ssbst [] = asks macros
ssbst (Assign r x t : asss) = do
x <- isFresh x
smacro B0 t
local (declareMacro (x, t)) $ ssbst asss
{-usage B0 = do
ovs <- asks objVars
pure (sbstI (scopeSize ovs), ovs)
ssbst usage (sg :< sgc) = case sgc of
Assign r v t -> do
(sg, ovs) <- ssbst usage sg
-- ovs better be a valid scope (without Drop, we know it will be)
local (setObjVars' ovs) $ do
v <- isFresh v
(desc, t) <- itm usage t
pure (sbstT sg ((Hide v :=) $^ t), ovs <: ObjVar v desc)
_ -> undefined
-}
{-
ssbst :: Usage -> Bwd Assign -> Elab (ACTSbst, ObjVars)
ssbst usage B0 = do
ovs <- asks objVars
pure (sbstI (scopeSize ovs), ovs)
ssbst usage (sg :< sgc) = case sgc of
Keep r v -> do
(xz, (w, cat)) <- spop r
when (v /= w) $ throwComplaint r (NotTopVariable v w)
(sg, ovs) <- local (setObjVars xz) (ssbst usage sg)
pure (sbstW sg (ones 1), ovs <: ObjVar (getVariable w) cat)
-- TODO : worry about dropped things ocurring in types
Drop r v -> do
(xz, (w, cat)) <- spop r
when (v /= w) $ throwComplaint r (NotTopVariable v w)
(sg, ovs) <- local (setObjVars xz) (ssbst usage sg)
pure (weak sg, ovs)
Assign r v t -> do
(sg, ovs) <- ssbst usage sg
local (setObjVars ovs) $ do
(desc, t) <- itm usage t
v <- isFresh v
pure (sbstT sg ((Hide v :=) $^ t), ovs <: ObjVar v (Known desc))
-}
sth :: Restriction -> (Bwd Variable, ThDirective) -> Elab Th
sth (Restriction ovs th) (xz, b) = do
whenLeft (isAll ((`elem` ovs) . getVariable) (xz <>> [])) $ \ x ->
throwComplaint x (OutOfScope x)
let th = which (`elem` (getVariable <$> xz)) ovs
pure $ case b of
ThKeep -> th
ThDrop -> comp th
stms :: Usage -> [ASemanticsDesc] -> Raw -> Elab ACTm
stms usage [] (At r "") = atom "" <$> asks (scopeSize . objVars)
stms usage [] (At r a) = throwComplaint r (ExpectedNilGot a)
stms usage [] t = throwComplaint t (ExpectedANilGot t)
stms usage (d:ds) (Cons r p q) = (%) <$> stm usage d p <*> stms usage ds q
stms usage _ t = throwComplaint t (ExpectedAConsGot t)
sscrutinee :: CScrutinee -> Elab (EScrutinee, AScrutinee)
sscrutinee (SubjectVar r v) = do
-- TODO: shouldn't this svar return a syntax desc?
-- We're in subject analysis mode
(isSub, desc, actm) <- svar (Scrutinised r) Nothing v
case (isSub, actm) of
(IsSubject{}, CdB (m :$ sg) _) -> pure (SubjectVar r desc, SubjectVar r actm)
_ -> throwComplaint r (NotAValidSubjectVar v)
sscrutinee (Pair r sc1 sc2) = do
(esc1, asc1) <- sscrutinee sc1
(esc2, asc2) <- sscrutinee sc2
pure (Pair r esc1 esc2, Pair r asc1 asc2)
sscrutinee (Lookup r stk v) = do
(stk, stkTy) <- isContextStack stk
t <- during (LookupVarElaboration v) $ do
let desc = asSemantics (keyDesc stkTy)
(isSub, desc, t) <- svar (LookedUp r) (Just desc) v
pure t
let vdesc = valueDesc stkTy
desc = Semantics.contract (VEnumOrTag (scope vdesc) ["Nothing"] [("Just", [vdesc])])
pure (Lookup r desc (getVariable v), Lookup r stk t)
sscrutinee (Compare r s t) = do
infoS <- guessDesc False s
infoT <- guessDesc False t
desc <- during (CompareSyntaxCatGuess s t) $
fromInfo r =<< compatibleInfos r infoS infoT
s <- during (CompareTermElaboration s) $ stm (Compared (getRange s)) desc s
t <- during (CompareTermElaboration t) $ stm (Compared (getRange t)) desc t
pure (Compare r () (), Compare r s t)
sscrutinee (Term r t) = during (ScrutineeTermElaboration t) $ do
desc <- fromInfo r =<< guessDesc False t
t <- stm (MatchedOn r) desc t
pure (Term r desc, Term r t)
sty :: CSemanticsDesc -> Elab ASemanticsDesc
sty t = do
sem <- satom "Semantics"
stm DontLog sem t
ssot :: CSOT -> Elab ASOT
ssot (CSOT [] ty) = (:=>) <$> asks objVars <*> sty ty
ssot (CSOT ((desc, x) : xs) ty) = do
desc <- sty desc
x <- isFresh x
local (declareObjVar (x, desc)) $ ssot (CSOT xs ty)
sparamdescs :: [(Binder Variable, CSOT)]
-> Elab ([(Binder ActorMeta, ASOT)], Decls)
sparamdescs [] = ([],) <$> asks declarations
sparamdescs ((bd , sot):ps) = do
sot <- ssot sot
binder <- traverse isFresh bd
let bd = ActorMeta ACitizen <$> binder
(ps, ds) <- local (declare binder (ActVar IsNotSubject sot)) $ sparamdescs ps
pure ((bd , sot):ps, ds)
spatSemantics0 :: ASemanticsDesc -> CPattern -> Elab (APattern, Decls, ACTm)
spatSemantics0 desc p = do
ovs <- asks objVars
spatSemantics desc (initRestriction ovs) p
data ConsDesc
= ConsCell ASemanticsDesc ASemanticsDesc
| ConsEnum [(String, [ASemanticsDesc])]
| ConsUniverse
vconsDesc :: Range -> ASemanticsDesc -> RawP -- for the error message
-> VSemanticsDesc -> Elab ConsDesc
vconsDesc r desc rp vdesc = case vdesc of
VNilOrCons d1 d2 -> pure (ConsCell d1 d2)
VCons d1 d2 -> pure (ConsCell d1 d2)
VWildcard _ -> pure (ConsCell desc desc)
VEnumOrTag _ _ ds -> pure (ConsEnum ds)
VUniverse _ -> pure ConsUniverse
_ -> throwComplaint r =<< syntaxPError desc rp
spatSemantics :: ASemanticsDesc {- gamma -}
-> {- r :: -} Restriction {- gamma -}
-> CPattern {- should fit in r.support -}
-> Elab (APattern {- gamma -}, Decls, ACTm {- gamma -})
spatSemantics desc rest (Irrefutable r p) = do
raiseWarning r (IgnoredIrrefutable p) -- TODO
spatSemantics desc rest p
spatSemantics desc rest (AsP r v p) = do
v <- isFresh v
ds <- asks declarations
(ovs, asot) <- thickenedASOT r (restriction rest) desc
(p, ds, t) <-
local (setDecls (ds :< (v, ActVar IsNotSubject asot))) $ spatSemantics desc rest p
pure (AT (ActorMeta ACitizen v) p, ds, t)
spatSemantics desc rest (ThP r ph p) = do
ph <- sth rest ph
spatSemantics desc (ph ^? rest) p
spatSemantics desc rest (UnderscoreP r) = do
ds <- asks declarations
let hack = Variable r ("_" ++ show (length ds))
spatSemantics desc rest (VarP r hack)
spatSemantics desc rest (VarP r v) = during (PatternVariableElaboration v) $ do
ds <- asks declarations
res <- resolve v
let th = restriction rest
case res of
Just (AnObjVar desc' i) -> do
-- TODO: do we need to check whether desc' is thickenable?
whenNothing (thickx th i) $ throwComplaint r (OutOfScope v)
compatibleInfos (getRange v) (Known desc) (Known desc')
pure (VP i, ds, var i (bigEnd th))
Just mk -> throwComplaint r (NotAValidPatternVariable v mk)
Nothing -> do
(ovs, asot) <- thickenedASOT r th desc
v <- isFresh v
let pat = MP (ActorMeta ACitizen v) th
pure (pat, ds :< (v, ActVar IsNotSubject asot), ActorMeta ACitizen v $: sbstW (sbstI 0) th)
spatSemantics desc rest rp = do
table <- gets syntaxCats
dat <- asks headUpData
ds <- asks declarations
case Semantics.expand table dat desc of
Nothing -> throwComplaint rp . InvalidSemanticsDesc =<< withVarNames desc
Just vdesc -> case rp of
AtP r a -> do
case vdesc of
VAtom _ -> pure ()
VAtomBar _ as -> when (a `elem` as) $ throwComplaint r (GotBarredAtom a as)
VNil _ -> unless (a == "") $ throwComplaint r (ExpectedNilGot a)
VNilOrCons{} -> unless (a == "") $ throwComplaint r (ExpectedNilGot a)
VEnumOrTag sc es _ -> unless (a `elem` es) $ throwComplaint r (ExpectedEnumGot es a)
VWildcard sc -> pure ()
VUniverse _ ->
unless (a `elem` ("Atom" : "Nil" : "Wildcard" : "Syntax" : "Semantics" : Map.keys table)) $
throwComplaint r (ExpectedASemanticsGot (At r a))
_ -> throwComplaint r =<< syntaxPError desc rp
pure (AP a, ds, atom a (bigEnd (restriction rest)))
ConsP r p1 p2 -> do
-- take vdesc apart and decide what needs to be checked
descs <- vconsDesc r desc rp vdesc
case descs of
ConsCell d1 d2 -> do
(p1, ds, t1) <- spatSemantics d1 rest p1
(p2, ds, t2) <- local (setDecls ds) (spatSemantics d2 rest p2)
pure (PP p1 p2, ds, t1 % t2)
ConsEnum ds -> case p1 of
AtP r a -> case lookup a ds of
Nothing -> throwComplaint r (ExpectedTagGot (fst <$> ds) a)
Just descs -> do
at <- satom "Atom"
(p1, ds, t1) <- spatSemantics at rest p1
(p2, ds, t2) <- local (setDecls ds) (spatSemanticss descs rest p2)
pure (PP p1 p2, ds, t1 % t2)
_ -> throwComplaint r =<< syntaxPError desc rp
ConsUniverse -> case (p1 , p2) of
(AtP _ "Pi", ConsP _ s (ConsP _ (LamP _ (Scope (Hide x) t)) (AtP _ ""))) -> do
(ps, ds, ts) <- spatSemantics desc rest s
(pt, ds, tt) <-
local (setDecls ds) $ elabUnder (x, ts) $
-- local (addHint (getVariable <$> x) (Known desc)) $
spatSemantics (weak desc) (extend rest (getVariable <$> x)) t
pure (PP (AP "Pi") (PP ps (PP pt (AP "")))
, ds
, "Pi" #%+ [ts,tt])
_ -> throwComplaint r (ExpectedASemanticsPGot rp)
LamP r (Scope v@(Hide x) p) -> do
(s, desc) <- case vdesc of
VWildcard _ -> pure (desc, weak desc)
VBind cat desc -> (, weak desc) <$> satom cat
VPi s (y, t) -> throwComplaint r =<< CantMatchOnPi <$> withVarNames desc <*> pure rp
_ -> throwComplaint r =<< syntaxPError desc rp
elabUnder (x, s) $
-- local (addHint (getVariable <$> x) (Known s)) $
spatSemantics desc (extend rest (getVariable <$> x)) p
spatSemanticss :: [ASemanticsDesc]
-> Restriction
-> RawP
-> Elab (Pat, Decls, ACTm)
spatSemanticss [] rest (AtP r "") = (AP "",, atom "" (weeEnd (restriction rest))) <$> asks declarations
spatSemanticss [] rest (AtP r a) = throwComplaint r (ExpectedNilGot a)
spatSemanticss [] rest t = throwComplaint t (ExpectedANilPGot t)
spatSemanticss (d:ds) rest (ConsP r p ps) = do
(p, decls, t) <- spatSemantics d rest p
(ps, decls, ts) <- local (setDecls decls) $ spatSemanticss ds rest ps
pure (PP p ps, decls, t % ts)
spatSemanticss _ rest t = throwComplaint t (ExpectedAConsPGot t)
isList :: Raw -> Elab [Raw]
isList (At r "") = pure []
isList (At r a) = throwComplaint r (ExpectedNilGot a)
isList (Cons r p q) = (p:) <$> isList q
isList t = throwComplaint t (ExpectedAConsGot t)
mkList :: [ACTm] -> Elab ACTm
mkList ts = do
snil <- satom ""
pure (foldr (%) snil ts)
-- Input: fully applied operator ready to operate
-- Output: (abstract operator, raw parameters)
sop :: Raw -> Elab (AAnOperator, [Raw])
sop (At ra a) = do
op <- isOperator ra a
pure (op, [])
sop (Cons rp (At ra a) ps) = do
op <- isOperator ra a
es <- isList ps
pure (op, es)
sop ro = throwComplaint ro (ExpectedAnOperator ro)
matchObjType :: Range
-> (Binder ActorMeta, Pat) -- (p : ['Sig S \x.T]) -'snd
-> (ACTm, ASemanticsDesc) -- ['MkSig a b] : ['Sig A \y.B]
-> Elab (HeadUpData' ActorMeta) -- environment extended by: (S = A, \x.T = \y.B, p = ['MkSig a b])
matchObjType r (mb , oty) (ob, obDesc) = do
dat <- asks headUpData
let hnf = headUp dat
env <- case snd $ match hnf initMatching (Problem B0 oty obDesc) of
Left e -> throwComplaint r =<< InferredDescMismatch <$> withVarNames oty <*> withVarNames obDesc
Right m -> pure $ matchingToEnv m (huEnv dat)
env <- case mb of
Unused _ -> pure env
Used v -> pure $ newActorVar v (localScope env <>> [], ob) env
pure dat{huEnv = env}
itm :: Usage -> Raw -> Elab (ASemanticsDesc, ACTm)
itm usage (Var r v) = do
(_, desc, v) <- svar usage Nothing v
pure (desc, v)
-- rob -rop
itm usage (Op r rob rop) = do
(obDesc, ob) <- itm usage rob
(AnOperator{..}, rps) <- sop rop
dat <- matchObjType r objDesc (ob, obDesc)
local (setHeadUpData dat) $ do
(desc, ps) <- itms r (getOperator opName) usage paramsDesc rps retDesc
pure (desc, rad ob obDesc -% (getOperator opName, ps))
-- TODO?: annotated terms?
itm _ t = throwComplaint t $ DontKnowHowToInferDesc t
itms :: Range -> String -> Usage
-- Parameters types e.g. (_ : 'Nat\n. {m = n}p\ih. {m = ['Succ n]}p)
-> [(Binder ActorMeta, ASOT)]
-- Raw parameters
-> [Raw]
-- Return type
-> ASemanticsDesc
--
-> Elab (ASemanticsDesc -- Instantiated return type
, [ACTm]) -- Elaborated parameters
itms r op usage [] [] rdesc = (, []) <$> (instantiateDesc r rdesc)
itms r op usage ((binder, sot):bs) (rp:rps) rdesc = do
(ovs :=> desc) <- instantiateSOT (getRange rp) sot
(p, dat) <- sparam usage binder B0 (discharge ovs desc) rp
local (setHeadUpData dat) $
fmap (p:) <$> itms r op usage bs rps rdesc
itms r op usage bs rps rdesc = throwComplaint r $ ArityMismatchInOperator op ((length bs) - (length rps))
sparam :: Usage
-> Binder ActorMeta -- Name of parameter
-> Bwd String -- Names of formal parameters of the parameter
-> Telescopic ASemanticsDesc -- Type of the parameter
-> Raw -- Raw term naming the actual parameters
-> Elab (ACTm, HeadUpData' ActorMeta) -- Elaborated term,
-- headupdata with the parameter defined
sparam usage binder namez (Stop pdesc) rp = do
p <- stm usage pdesc rp
dat <- do
dat <- asks headUpData
pure $ case binder of
Unused _ -> dat
Used v ->
let env = huEnv dat
env' = newActorVar v (namez <>> [], p) env
in dat {huEnv = env'}
pure (p, dat)
sparam usage binder namez (Tele desc (Scope (Hide name) tele)) (Lam r (Scope (Hide x) rp)) =
elabUnder (x, desc) $ sparam usage binder (namez :< name) tele rp
sparam _ _ _ _ rp = throwComplaint rp $ ExpectedParameterBinding rp
instantiateSOT :: Range -> ASOT -> Elab ASOT
instantiateSOT r (ovs :=> desc)
= (:=>) <$> traverse (instantiateDesc r) ovs <*> instantiateDesc r desc
instantiateDesc :: Range -> ASemanticsDesc -> Elab ASemanticsDesc
instantiateDesc r desc = do
dat <- asks headUpData
-- The object acted upon and the parameters appearing before the
-- one currently being elaborated need to be substituted into the desc
case mangleActors (huOptions dat) (huEnv dat) desc of
Nothing -> throwComplaint r $ SchematicVariableNotInstantiated
Just v -> pure v
{-
sp is only for Concrete p to Abstract p
sasot :: Range -> ASOT -> Elab ASemanticsDesc
sasot r (objVars :=> desc) = do
dat <- asks headUpData
-- The object acted upon and the parameters appearing before the
-- one currently being elaborated need to be substituted into the SOT
case mangleActors (huOptions dat) (huEnv dat) desc of
Nothing -> throwComplaint r $ SchematicVariableNotInstantiated r
Just v -> pure v -- TODO: foldr (\ (x,t) v => ['Bind t \x.v]) id v
-}
stm :: Usage -> ASemanticsDesc -> Raw -> Elab ACTm
stm usage desc (Var r v) = during (TermVariableElaboration v) $ do
(_, _, t) <- svar usage (Just desc) v
pure t
stm usage desc (Thicken r th t) = do
ovs <- asks objVars
let rest = initRestriction ovs
th <- sth rest th
desc <- case thickenCdB th desc of
Nothing -> throwComplaint r . NotAValidDescriptionRestriction th =<< withVarNames desc
Just desc -> pure desc
ovs <- case thickenObjVars th ovs of
Nothing -> throwComplaint r (NotAValidContextRestriction th ovs)
Just ovs -> pure ovs
fmap (*^ th) $ local (setObjVars' ovs) $ stm usage desc t
stm usage desc (Sbst r sg t) = do
ms <- during (SubstitutionElaboration sg) $ ssbst (sg <>> [])
local (setMacros ms) (stm usage desc t)
stm usage desc rt = do
table <- gets syntaxCats
dat <- asks headUpData
case Semantics.expand table dat desc of
Nothing -> throwComplaint rt =<< InvalidSemanticsDesc <$> withVarNames desc
Just vdesc -> case rt of
At r a -> do
case vdesc of
VAtom _ -> pure ()
VAtomBar _ as -> when (a `elem` as) $ throwComplaint r (GotBarredAtom a as)
VNil _ -> unless (a == "") $ throwComplaint r (ExpectedNilGot a)
VNilOrCons{} -> unless (a == "") $ throwComplaint r (ExpectedNilGot a)
VEnumOrTag _ es _ -> unless (a `elem` es) $ throwComplaint r (ExpectedEnumGot es a)
VWildcard _ -> pure ()
VUniverse _ -> unless (a `elem` ("Atom" : "Nil" : "Wildcard" : "Syntax" : "Semantics" : Map.keys table)) $ throwComplaint r (ExpectedASemanticsGot rt)
_ -> throwComplaint r =<< SemanticsError <$> withVarNames desc <*> pure rt
satom a
Cons r p q -> case vdesc of
VNilOrCons d1 d2 -> (%) <$> stm usage d1 p <*> stm usage d2 q
VCons d1 d2 -> (%) <$> stm usage d1 p <*> stm usage d2 q
VWildcard _ -> (%) <$> stm usage desc p <*> stm usage desc q
VEnumOrTag _ _ ds -> case p of
At r a -> case lookup a ds of
Nothing -> throwComplaint r (ExpectedTagGot (fst <$> ds) a)
Just descs -> do
adesc <- satom "Atom"
(%) <$> stm usage adesc p <*> stms usage descs q
_ -> throwComplaint r =<< syntaxError desc rt
VUniverse _ -> case (p , q) of
(At _ "Pi", Cons _ s (Cons _ (Lam _ (Scope (Hide x) t)) (At _ ""))) -> do
s <- sty s
t <- elabUnder (x, s) $ sty t
pure ("Pi" #%+ [s, t])
(At _ a, Cons _ s (Cons _ t (At _ ""))) | a `elem` ["Cons", "NilOrCons", "Bind"] -> do
s <- sty s
t <- sty t
pure (a #%+ [s, t])
(At _ a, Cons _ es nil@(At _ "")) | a `elem` ["Enum", "AtomBar"] ->
senumortag r a es nil
(At _ "Tag", Cons _ tds nil@(At _ "")) ->
senumortag r "Tag" nil tds
(At _ "EnumOrTag", Cons _ es (Cons _ tds nil@(At _ ""))) ->
senumortag r "EnumOrTag" es tds
(At _ "Fix", Cons _ f (At _ "")) -> do
f <- stm usage (Semantics.contract (VBind "Semantics" desc)) f
pure ("Fix" #%+ [f])
_ -> throwComplaint r (ExpectedASemanticsGot rt)
_ -> throwComplaint r =<< syntaxError desc rt
Lam r (Scope (Hide x) sc) -> do
(s, desc) <- case vdesc of
VWildcard i -> pure (desc, weak desc)
VBind cat desc -> (,weak desc) <$> satom cat
VPi s (y, t) -> pure (s, t)
_ -> throwComplaint r =<< syntaxError desc rt
elabUnder (x, s) $ stm usage desc sc
Op{} -> do
(tdesc, t) <- itm usage rt
compatibleInfos (getRange rt) (Known desc) (Known tdesc)
pure t
senumortag :: Range -> String -> Raw -> Raw -> Elab ACTm
senumortag r a es tds = do
-- elaborate enums
es <- isList es
es <- forM es $ \case
(At _ a) -> do
e <- satom a
pure (a, e)
x -> do
adesc <- satom "Atom"
throwComplaint x =<< syntaxError adesc x
(as, es) <- do pure (unzip es)
whenLeft (allUnique as) $ \ a -> throwComplaint r (DuplicatedTag a)
es <- mkList es
-- elaborate tags
tds <- isList tds
tds <- forM tds $ \case
(Cons _ (At _ ra) args) -> do
args <- isList args
args <- traverse sty args
a <- satom ra
as <- mkList (a:args)
pure (ra, as)
x -> throwComplaint x (ExpectedAConsGot x)
(ts, tds) <- do pure (unzip tds)
whenLeft (allUnique ts) $ \ a -> throwComplaint r (DuplicatedTag a)
tds <- mkList tds
-- put things back together
case a of
"AtomBar" -> do
a <- satom a
mkList [a,es]
_ -> do
hd <- satom "EnumOrTag"
mkList [hd, es, tds]
elabUnder :: HasCallStack => Show a => Dischargeable a => (Binder Variable, ASemanticsDesc) -> Elab a -> Elab a
elabUnder (x, desc) ma = do
scp <- asks (scopeSize . objVars)
unless (scp == scope desc) $ do
st <- asks stackTrace
error ("The IMPOSSIBLE has happened when binding " ++ show x ++ show st)
x <- case x of
Used x -> isFresh x
Unused _ -> pure "_"
(x \\) {-. (\ x -> traceShow x x) -} <$> local (declareObjVar (x, desc)) ma
spats :: IsSubject -> [ASemanticsDesc] -> Restriction -> RawP -> Elab (Maybe Range, Pat, Decls, Hints)
spats _ [] rest (AtP r "") = (Nothing, AP "",,) <$> asks declarations <*> asks binderHints
spats _ [] rest (AtP r a) = throwComplaint r (ExpectedNilGot a)
spats _ [] rest t = throwComplaint t (ExpectedANilPGot t)
spats isSub (d:ds) rest (ConsP r p q) = do
(mr1, p, decls, hints) <- spatBase isSub d rest p
(mr2, q, decls, hints) <- local (setDecls decls . setHints hints) $ spats isSub ds rest q
pure (mr1 <|> mr2, PP p q, decls, hints)
spats _ _ rest t = throwComplaint t (ExpectedAConsPGot t)
-- Inputs:
-- 0. Elaborated scrutinee -- description of how the scrutinee we are
-- matching on was formed
-- 1. Pair of local variables and thinning describing what we are
-- allowed to depend on
-- 2. Raw pattern to elaborate
-- Returns:
-- 0. Whether a subject pattern was thrown away
-- 1. Elaborated pattern
-- 2. Bound variables (together with their syntactic categories)
-- 3. Binder hints introduced by \x. patterns
spat :: EScrutinee -> Restriction -> RawP -> Elab (Maybe Range, Pat, Decls, Hints)
spat esc rest rp@(AsP r v p) = do
unless (isSubjectFree esc) $
throwComplaint r (AsPatternCannotHaveSubjects rp)
let desc = escrutinee esc
v <- isFresh v
ds <- asks declarations
(ovs, asot) <- thickenedASOT r (restriction rest) desc
(mr, p, ds, hs) <- local (setDecls (ds :< (v, ActVar IsNotSubject asot))) $ spat esc rest p
pure (mr, AT (ActorMeta ACitizen v) p, ds, hs)
spat esc rest p@VarP{} = spatBase (Pattern <$ isSubject esc) (escrutinee esc) rest p
spat esc rest (ThP r ph p) = do
ph <- sth rest ph
(mr, p, ds, hs) <- spat esc (ph ^? rest) p
pure (mr, p, ds, hs)
spat esc rest p@(UnderscoreP r) = do
(_, p, ds, hs) <- spatBase (Pattern <$ isSubject esc) (escrutinee esc) rest p
let mr = r <$ guard (not (isSubjectFree esc))
pure (mr, p, ds, hs)
spat esc@(Pair r esc1 esc2) rest rp = case rp of
ConsP r p q -> do
(mr1, p, ds, hs) <- spat esc1 rest p
(mr2, q, ds, hs) <- local (setDecls ds . setHints hs) (spat esc2 rest q)
pure (mr1 <|> mr2, PP p q, ds, hs)
_ -> throwComplaint rp =<< syntaxPError (escrutinee esc) rp
spat (SubjectVar r desc) rest rp = spatBase (IsSubject Pattern) desc rest rp
spat esc@(Lookup _ _ av) rest rp@(ConsP r (AtP _ "Just") (ConsP _ _ (AtP _ ""))) = do
logUsage av (SuccessfullyLookedUp r)
spatBase IsNotSubject (escrutinee esc) rest rp
spat esc@(Lookup _ _ av) rest rp = spatBase IsNotSubject (escrutinee esc) rest rp
spat esc@(Compare{}) rest rp = spatBase IsNotSubject (escrutinee esc) rest rp
spat esc@(Term{}) rest rp = spatBase IsNotSubject (escrutinee esc) rest rp
thickenedASOT :: Range -> Th -> ASemanticsDesc -> Elab (ObjVars, ASOT)
thickenedASOT r th desc = do
ovs <- asks objVars
ovs <- case thickenObjVars th ovs of
Nothing -> throwComplaint r (NotAValidContextRestriction th ovs)
Just ovs -> pure ovs
desc <- case thickenCdB th desc of
Nothing -> throwComplaint r . NotAValidDescriptionRestriction th =<< withVarNames desc
Just desc -> pure desc
pure (ovs, ovs :=> desc)
spatBase :: IsSubject
-> ASemanticsDesc
-> Restriction
-> RawP
-> Elab (Maybe Range, Pat, Decls, Hints)
spatBase isSub desc rest rp@(AsP r v p) = do
unless (isSub == IsNotSubject) $
throwComplaint r (AsPatternCannotHaveSubjects rp)
v <- isFresh v
ds <- asks declarations
(ovs, asot) <- thickenedASOT r (restriction rest) desc
(mr, p, ds, hs) <- local (setDecls (ds :< (v, ActVar isSub asot))) $ spatBase isSub desc rest p
pure (mr, AT (ActorMeta ACitizen v) p, ds, hs)
spatBase isSub desc rest (ThP r ph p) = do
ph <- sth rest ph
(mr, p, ds, hs) <- spatBase isSub desc (ph ^? rest) p
pure (mr, p, ds, hs)
spatBase isSub desc rest (VarP r v) = during (PatternVariableElaboration v) $ do
ds <- asks declarations
hs <- asks binderHints
res <- resolve v
let th = restriction rest
case res of
Just (AnObjVar desc' i) -> do
-- TODO: do we need to check whether desc' is thickenable?
whenNothing (thickx th i) $ throwComplaint r (OutOfScope v)
compatibleInfos (getRange v) (Known desc) (Known desc')
pure (Nothing, VP i, ds, hs)
Just mk -> throwComplaint r (NotAValidPatternVariable v mk)
Nothing -> do
(ovs, asot) <- thickenedASOT r th desc
v <- isFresh v
let pat = MP (ActorMeta (spassport (Scrutinised unknown) isSub) v) th
pure (Nothing, pat, ds :< (v, ActVar isSub asot), hs)
spatBase isSub desc rest (UnderscoreP r) = do
let mr = case isSub of
IsSubject{} -> Just r
IsNotSubject -> Nothing
(mr,HP,,) <$> asks declarations <*> asks binderHints
spatBase isSub desc rest rp = do
table <- gets syntaxCats
dat <- asks headUpData
case Semantics.expand table dat desc of
Nothing -> throwComplaint rp . InvalidSemanticsDesc =<< withVarNames desc
Just vdesc -> case rp of
AtP r a -> do
case vdesc of
VAtom _ -> pure ()
VAtomBar _ as -> when (a `elem` as) $ throwComplaint r (GotBarredAtom a as)
VNil _ -> unless (a == "") $ throwComplaint r (ExpectedNilGot a)
VNilOrCons{} -> unless (a == "") $ throwComplaint r (ExpectedNilGot a)
VEnumOrTag sc es _ -> unless (a `elem` es) $ throwComplaint r (ExpectedEnumGot es a)
VWildcard sc -> pure ()
VUniverse _ -> throwComplaint r (CantMatchOnSemantics rp)
_ -> throwComplaint r =<< syntaxPError desc rp
(Nothing, AP a,,) <$> asks declarations <*> asks binderHints
ConsP r p q -> do
-- take vdesc apart and decide what needs to be checked
descs <- vconsDesc r desc rp vdesc
case descs of
ConsCell d1 d2 -> do
(mr1, p, ds, hs) <- spatBase isSub d1 rest p
(mr2, q, ds, hs) <- local (setDecls ds . setHints hs) (spatBase isSub d2 rest q)
pure (mr1 <|> mr2, PP p q, ds, hs)
ConsEnum ds -> case p of
AtP r a -> case lookup a ds of
Nothing -> throwComplaint r (ExpectedTagGot (fst <$> ds) a)
Just descs -> do
(mr1, p, ds, hs) <- spatBase isSub (atom "Atom" 0) rest p
(mr2, q, ds, hs) <- local (setDecls ds . setHints hs) (spats isSub descs rest q)
pure (mr1 <|> mr2, PP p q, ds, hs)
_ -> throwComplaint r =<< syntaxPError desc rp
ConsUniverse -> throwComplaint r (CantMatchOnSemantics rp)
LamP r (Scope v@(Hide x) p) -> do
(s, desc) <- case vdesc of
VWildcard _ -> pure (desc, weak desc)
VBind cat desc -> (, weak desc) <$> satom cat
VPi s (y, t) -> throwComplaint r =<< CantMatchOnPi <$> withVarNames desc <*> pure rp
_ -> throwComplaint r =<< syntaxPError desc rp
elabUnder (x, s) $
-- local (addHint (getVariable <$> x) (Known s)) $
spatBase isSub desc (extend rest (getVariable <$> x)) p
isObjVar :: Variable -> Elab (ASemanticsDesc, DB)
isObjVar p = resolve p >>= \case
Just (AnObjVar desc i) -> pure (desc, i)
Just mk -> throwComplaint p $ NotAValidPatternVariable p mk
Nothing -> throwComplaint p $ OutOfScope p
isChannel :: Variable -> Elab Channel
isChannel ch = resolve ch >>= \case
Just (ADeclaration (AChannel sc)) -> pure (Channel $ getVariable ch)
Just mk -> throwComplaint ch (NotAValidChannel ch mk)
Nothing -> throwComplaint ch (OutOfScope ch)
isOperator :: Range -> String -> Elab AAnOperator
isOperator r nm = do
ops <- asks operators
case Map.lookup nm ops of
Just res -> pure res
Nothing -> throwComplaint r (NotAValidOperator nm)
data IsJudgement = IsJudgement
{ judgementExtract :: ExtractMode
, judgementName :: JudgementName
, judgementProtocol :: AProtocol
}
isJudgement :: Variable -> Elab IsJudgement
isJudgement jd = resolve jd >>= \case
Just (ADeclaration (AJudgement em p)) -> pure (IsJudgement em (getVariable jd) p)
Just mk -> throwComplaint jd (NotAValidJudgement jd mk)
Nothing -> throwComplaint jd (OutOfScope jd)
isContextStack :: Variable -> Elab (Stack, AContextStack)
isContextStack stk = resolve stk >>= \case
Just (ADeclaration (AStack stkTy)) -> do
scp <- asks (scopeSize . objVars)
pure (Stack (getVariable stk), bimap (weaks scp) (weaks scp) stkTy)
Just mk -> throwComplaint stk (NotAValidStack stk mk)
Nothing -> throwComplaint stk (OutOfScope stk)
channelScope :: Channel -> Elab ObjVars
channelScope (Channel ch) = do
ds <- asks declarations
case fromJust (focusBy (\ (y, k) -> k <$ guard (ch == y)) ds) of
(_, AChannel sc, _) -> pure sc
steppingChannel :: Range -> Channel
-> (Direction -> [AProtocolEntry] -> Elab (a, [AProtocolEntry]))
-> Elab a
steppingChannel r ch step = do
nm <- getName
(dir, pnm, p) <- gets (fromJust . channelLookup ch)
unless (pnm `isPrefixOf` nm) $ throwComplaint r (NonLinearChannelUse ch)
(a, p) <- step dir p
modify (channelInsert ch (dir, nm, p))
pure a
open :: Direction -> Channel -> AProtocol -> Elab ()
open dir ch (Protocol p) = do
nm <- getName
modify (channelInsert ch (dir, nm, p))
close :: Bool -> Range -> Channel -> Elab ()
close b r ch = do
-- make sure the protocol was run all the way
gets (channelLookup ch) >>= \case
Just (_, _, ps) -> case ps of
[] -> pure ()
_ -> when b $
-- if we cannot win, we don't care
throwComplaint r (UnfinishedProtocol ch (Protocol ps))
modify (channelDelete ch)
withChannel :: Range -> Direction -> Channel -> AProtocol -> Elab a -> Elab a
withChannel r dir ch@(Channel rch) p ma = do
open dir ch p
-- run the actor in the extended context
ovs <- asks objVars
(a, All b) <- local (declare (Used rch) (AChannel ovs)) $ listen ma
close b r ch
pure a
guessDesc :: Bool -> -- is this in tail position?
Raw -> Elab (Info ASemanticsDesc)
guessDesc b (Var _ v) = resolve v >>= \case
Just (AnObjVar desc i) -> pure (Known desc)
Just (ADeclaration (ActVar isSub (ObjVars B0 :=> desc))) -> do
scp <- asks (scopeSize . objVars)
pure $ Known (weaks scp desc)
_ -> pure Unknown
guessDesc b (Cons _ p q) = do
dp <- guessDesc False p
dq <- guessDesc True q
case (dp, dq) of
(Known d1, Known d2) -> pure (Known $ Semantics.contract (Semantics.VCons d1 d2))
_ -> pure Unknown
-- might need better guess for the scope than 0
guessDesc True (At _ "") = Known <$> satom "Nil"
guessDesc _ _ = pure Unknown
compatibleChannels :: Range
-> (Direction, [AProtocolEntry])
-> Ordering
-> (Direction, [AProtocolEntry])
-> Elab Int
compatibleChannels r (dp, []) dir (dq, []) = pure 0
compatibleChannels r (dp, p@(m, s) : ps) dir (dq, q@(n, t) : qs) = do
unless (s == t) $ throwComplaint r =<< incompatibleSemanticsDescs s t
let (cp , cq) = (whatComm m dp, whatComm n dq)
when (cp == cq) $ throwComplaint r (IncompatibleModes p q)
case (cp, dir) of
(RECV, LT) -> throwComplaint r (WrongDirection p dir q)
(SEND, GT) -> throwComplaint r (WrongDirection p dir q)
_ -> pure ()
(+1) <$> compatibleChannels r (dp, ps) dir (dq , qs)
compatibleChannels r (_,ps) _ (_,qs) = throwComplaint r (ProtocolsNotDual (Protocol ps) (Protocol qs))
sirrefutable :: String -> IsSubject -> RawP -> Elab (Binder String, Maybe (CScrutinee, RawP))
sirrefutable nm isSub = \case
VarP _ v -> (, Nothing) . Used <$> isFresh v
UnderscoreP r -> pure (Unused r, Nothing)
p -> do ctxt <- ask
-- this should be a unique name & is not user-writable
let r = getRange p
let av = "&" ++ nm ++ show (scopeSize (objVars ctxt) + length (declarations ctxt))
let var = Variable r av
let sc = case isSub of
IsSubject{} -> SubjectVar r var
IsNotSubject -> Term r (Var r var)
pure (Used av, Just (sc, p))
checkScrutinised :: Binder String -> Elab Bool
checkScrutinised (Unused _) = pure False
checkScrutinised (Used nm) = do
avs <- gets actvarStates
b <- case Map.lookup nm avs of
Just logs | wasScrutinised logs -> pure True
_ -> pure False
modify (\ st -> st { actvarStates = Map.delete nm (actvarStates st) })
pure b
sact :: CActor -> Elab AActor
sact = \case
Win r -> pure (Win r)
Constrain r s t -> do
infoS <- guessDesc False s
infoT <- guessDesc False t