Cross Reference: /hets/Static/AnalysisStructured.hs
AnalysisStructured.hs revision 7bf4436b6f9987b070033a323757b206c898c1be
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
{- |
Module : $Header$
Copyright : (c) Till Mossakowski and Uni Bremen 2003-2005
License : similar to LGPL, see HetCATS/LICENSE.txt or LIZENZ.txt
Maintainer : till@tzi.de
Stability : provisional
Portability : non-portable
Analysis of structured specifications
Follows the verfication semantic rules in Chap. IV:4.7
of the CASL Reference Manual.
-}
{-
Todo:
analysis of basic specs: use union of sigs
analysis of instantiations: if necessary, translate body along inclusion comorphism
Improve efficiency by storing local signature fragments only
Name views in devgraphs?
spec <name> not found: line number!
Issue warning for unions of non-disjoint signatures
(and offer tool for renaming?)
Check that translations and reductions do not effect local env
(Implement new semantics for revealing here)
Should we use institution independent analysis over the Grothendieck logic?
abstract syntax + devgraph would have to be changed to homogeneous case
logic translations are symbol maps in the Grothendieck logic
Problem with this approach: symbol functor goes into rel,
and induced_from_morphism gets difficult to implement
Unions need inclusion morphisms. Should these play a special role?
At least we need a function delivering the inclusion morphism
between two (sub)signatures.
In most logics, inclusions could be represented specially, such
that composition for them becomes fast.
Should we even identify an inclusion subcategory?
Then inclusions are represented by pair of signatures
(Non-inclusions could be specially displayed in the DG)
No optimization of heterogeneous unions!
(or use heterogeneous compInclusion in Proofs/Proof.hs)
(or perhaps abandon optimized unions at all and
replace compHomInclusion with comp Grothendieck? is this more efficient?)
Treatment of translations and reductions along logic translations
(see WADT 02 paper).
Open question:
may local env be translated, and even reduced, along logic translations?
if yes: how is local env linked to signature of resulting spec?
(important e.g. for checking that local env is not being renamed?)
does signature+comorphism suffice, such that c(local env)\subseteq sig?
if no: this means that only closed specs may be translated
Revealings without translations: just one arrow
Pushouts: only admissible within one logic?
Instantiations with formal parameter: add no new internal nodes
call extend_morphism
Optimizations:
Union nodes can be extended by a basic spec directly (no new node needed)
no new nodes for trivial translations
use foldM
Also: free, cofree nodes
Basic specs: if local env node is otherwise unused, overwrite it with
local sig+axioms
Ensure that just_struct option leads to disabling of various dg operations
(show sig, show mor, proving)
local: better error message for the following
spec GenCardinality [sort Subject,Object;
pred predicate : Subject * Object] =
%%Nat then
local {
Set [sort Object fit sort Elem |-> Object]
reveal Set[Object], #__, __eps__,
Nat,0,1,2,3,4,5,6,7,8,9,__@@__,__>=__,__<=__
then
op toSet : Subject -> Set [Object]
forall x : Subject; y : Object
. predicate (x,y) <=> y eps toSet(x)
} within
pred minCardinality(s: Subject;n: Nat) <=>
# toSet(s) >= n;
maxCardinality(s: Subject;n: Nat) <=>
# toSet(s) <= n;
cardinality(s: Subject;n: Nat) <=>
# toSet(s) = n
%%} hide Pos,toSet,Set[Object],#__,__eps__,__<=__,__>=__
end
-}
module Static.AnalysisStructured (ana_SPEC, ana_GENERICITY,
ana_VIEW_TYPE, ana_err, isStructured,
ana_RENAMING, ana_RESTRICTION,
homogenizeGM, extendMorphism)
where
import Driver.Options
import Data.Maybe
import Logic.Logic
import Logic.Coerce
import Logic.Comorphism
import Logic.Grothendieck
import Static.DevGraph
import Syntax.AS_Structured
import Common.AS_Annotation
import Common.Result
import Common.Id
import Data.Graph.Inductive.Graph
import qualified Common.Lib.Set as Set
import qualified Common.Lib.Map as Map
import qualified Common.Lib.Rel as Rel(image, setInsert)
import Data.List hiding (union)
import Common.PrettyPrint
import Common.Lib.Pretty
import Control.Monad
insEdgeNub :: LEdge DGLinkLab -> DGraph -> DGraph
insEdgeNub (v, w, l) g =
if (l, w) `elem` s then g
else (p, v, l', (l, w) : s) & g'
where (Just (p, _, l', s), g') = match v g
-- | analyze a SPEC
-- Parameters: global context, local environment,
-- the SIMPLE_ID may be a name if the specification shall be named,
-- options: here we need the info: shall only the structure be analysed?
ana_SPEC :: LogicGraph -> GlobalContext -> MaybeNode -> NODE_NAME ->
HetcatsOpts -> SPEC -> Result (SPEC,NodeSig,DGraph)
ana_SPEC lg gctx@(gannos,genv,dg) nsig name opts sp =
case sp of
Basic_spec (G_basic_spec lid bspec) ->
do G_sign lid' sigma' <- return (getMaybeSig nsig)
sigma <- coerceSign lid' lid "Analysis of basic spec" sigma'
(bspec', _sigma_local, sigma_complete, ax) <-
if isStructured opts
then return (bspec,empty_signature lid, empty_signature lid,[])
else do b <- maybeToMonad
("no basic analysis for logic "
++language_name lid)
(basic_analysis lid)
b (bspec,sigma,gannos)
incl <- ginclusion lg
(G_sign lid sigma) (G_sign lid sigma_complete)
let node_contents = DGNode {
dgn_name = name,
dgn_theory = G_theory lid sigma_complete $ toThSens ax,
-- no, not only the delta
dgn_nf = Nothing,
dgn_sigma = Nothing,
dgn_origin = DGBasic }
node = getNewNode dg
dg' = insNode (node,node_contents) dg
link = DGLink {
dgl_morphism = incl,
dgl_type = GlobalDef,
dgl_origin = DGExtension }
dg'' = case nsig of
EmptyNode _ -> dg'
JustNode (NodeSig n _) -> insEdgeNub (n,node,link) dg'
return (Basic_spec (G_basic_spec lid bspec'),
NodeSig node $ G_sign lid sigma_complete,
dg'')
Translation asp ren ->
do let sp1 = item asp
(sp1', NodeSig n' gsigma, dg') <-
ana_SPEC lg gctx nsig (inc name) opts sp1
mor <- ana_RENAMING lg gsigma opts ren
-- ??? check that mor is identity on local env
let gsigma' = cod Grothendieck mor
-- ??? too simplistic for non-comorphism inter-logic translations
G_sign lid' gsig <- return gsigma'
let node_contents = DGNode {
dgn_name = name,
dgn_theory = G_theory lid' gsig noSens,
dgn_nf = Nothing,
dgn_sigma = Nothing,
dgn_origin = DGTranslation }
node = getNewNode dg'
link = (n',node,DGLink {
dgl_morphism = mor,
dgl_type = GlobalDef,
dgl_origin = DGTranslation })
return (Translation (replaceAnnoted sp1' asp) ren,
NodeSig node gsigma',
insEdgeNub link $
insNode (node,node_contents) dg')
Reduction asp restr ->
do let sp1 = item asp
(sp1', NodeSig n' gsigma',dg') <-
ana_SPEC lg gctx nsig (inc name) opts sp1
let gsigma = getMaybeSig nsig
(hmor,tmor) <- ana_RESTRICTION dg gsigma gsigma' opts restr
-- we treat hiding and revealing differently
-- in order to keep the dg as simple as possible
case tmor of
Nothing ->
do let gsigma'' = dom Grothendieck hmor
-- ??? too simplistic for non-comorphism inter-logic reductions
G_sign lid' gsig <- return gsigma''
let node_contents = DGNode {
dgn_name = name,
dgn_theory = G_theory lid' gsig noSens,
dgn_nf = Nothing,
dgn_sigma = Nothing,
dgn_origin = DGHiding }
node = getNewNode dg'
link = (n',node,DGLink {
dgl_morphism = hmor,
dgl_type = HidingDef,
dgl_origin = DGHiding })
return (Reduction (replaceAnnoted sp1' asp) restr,
NodeSig node gsigma'',
insEdgeNub link $
insNode (node,node_contents) dg')
Just tmor' -> do
let gsigma1 = dom Grothendieck tmor'
gsigma'' = cod Grothendieck tmor'
-- ??? too simplistic for non-comorphism inter-logic reductions
G_sign lid1 gsig <- return gsigma1
G_sign lid'' gsig'' <- return gsigma''
-- the case with identity translation leads to a simpler dg
if tmor' == ide Grothendieck (dom Grothendieck tmor')
then do
let node1 = getNewNode dg'
node_contents1 = DGNode {
dgn_name = name,
dgn_theory = G_theory lid1 gsig noSens,
dgn_nf = Nothing,
dgn_sigma = Nothing,
dgn_origin = DGRevealing }
link1 = (n',node1,DGLink {
dgl_morphism = hmor,
dgl_type = HidingDef,
dgl_origin = DGRevealing })
return (Reduction (replaceAnnoted sp1' asp) restr,
NodeSig node1 gsigma1,
insEdgeNub link1 $
insNode (node1,node_contents1) dg')
else do
let [node1,node''] = newNodes 2 dg'
node_contents1 = DGNode {
dgn_name = extName "T" name,
dgn_theory = G_theory lid1 gsig noSens,
dgn_nf = Nothing,
dgn_sigma = Nothing,
dgn_origin = DGRevealing }
link1 = (n',node1,DGLink {
dgl_morphism = hmor,
dgl_type = HidingDef,
dgl_origin = DGRevealing })
node_contents'' = DGNode {
dgn_name = name,
dgn_theory = G_theory lid'' gsig'' noSens,
dgn_nf = Nothing,
dgn_sigma = Nothing,
dgn_origin = DGRevealTranslation }
link'' = (node1,node'',DGLink {
dgl_morphism = tmor',
dgl_type = GlobalDef,
dgl_origin = DGRevealTranslation })
return (Reduction (replaceAnnoted sp1' asp) restr,
NodeSig node'' gsigma'',
insEdgeNub link'' $
insNode (node'',node_contents'') $
insEdgeNub link1 $
insNode (node1,node_contents1) dg')
Union [] pos -> adjustPos pos $ fail $ "empty union"
Union asps pos ->
do let sps = map item asps
(sps',nsigs,dg',_) <-
let ana r sp' = do
(sps1,nsigs,dg',n) <- r
(sp1,nsig',dg1) <- ana_SPEC lg (gannos,genv,dg')
nsig n opts sp'
return (sp1:sps1,nsig':nsigs,dg1,inc n)
in foldl ana (return ([],[],dg,extName "U" name)) sps
let nsigs' = reverse nsigs
adj = adjustPos pos
gbigSigma <- adj $ gsigManyUnion lg (map getSig nsigs')
G_sign lid' gsig <- return gbigSigma
let node_contents = DGNode {
dgn_name = name,
dgn_theory = G_theory lid' gsig noSens,
dgn_nf = Nothing,
dgn_sigma = Nothing,
dgn_origin = DGUnion }
node = getNewNode dg'
insE dgres (NodeSig n gsigma) = do
dg1 <- dgres
incl <- adj $ ginclusion lg gsigma gbigSigma
let link = DGLink {
dgl_morphism = incl,
dgl_type = GlobalDef,
dgl_origin = DGUnion }
return (insEdgeNub (n,node,link) dg1)
dg'' <- foldl insE (return (insNode (node,node_contents) dg')) nsigs'
return (Union (map (uncurry replaceAnnoted)
(zip (reverse sps') asps))
pos,
NodeSig node gbigSigma,
dg'')
Extension asps pos -> do
(sps',nsig1',dg1) <- foldl ana (return ([],nsig,dg)) namedSps
case nsig1' of
EmptyNode _ -> fail "empty extension"
JustNode nsig1 -> return (Extension (map (uncurry replaceAnnoted)
(zip (reverse sps') asps))
pos, nsig1,dg1)
where
namedSps = zip (reverse (name: tail (take (length asps)
(iterate inc (extName "E" name)))))
asps
ana res (name',asp') = do
(sps', nsig', dg') <- res
(sp1', nsig1@(NodeSig n1 sig1), dg1) <-
ana_SPEC lg (gannos,genv,dg') nsig' name' opts (item asp')
-- insert theorem link for semantic annotations
-- take the first semantic annotation
let anno = find isSemanticAnno $ l_annos asp'
-- is the extension going between real nodes?
dg2 <- case (anno, nsig') of
(Just anno0@(Semantic_anno anno1 _), JustNode (NodeSig n' sig')) -> do
-- any other semantic annotation? that's an error
when (any (\an -> isSemanticAnno an && an/=anno0) $ l_annos asp')
(pplain_error () (ptext "Conflicting semantic annotations")
pos)
-- %implied should not occur here
when (anno1==SA_implied)
(pplain_error () (ptext
"Annotation %implied should come after a BASIC-ITEM")
pos)
if anno1==SA_implies then do
when (not (is_subgsign sig1 sig')) (pplain_error ()
(ptext "Signature must not be extended in presence of %implies")
pos)
-- insert a theorem link according to p. 319 of the CASL Reference Manual
return $ insEdgeNub (n1,n',DGLink {
dgl_morphism = ide Grothendieck sig1,
dgl_type = GlobalThm LeftOpen None LeftOpen,
dgl_origin = DGExtension }) dg1
else do
let anno2 = case anno1 of
SA_cons -> Cons
SA_def -> Def
SA_mono -> Mono
_ -> error "Static.AnalysisStructured: this cannot happen"
-- insert a theorem link according to p. 319 of the CASL Reference Manual
-- the theorem link is trivally proved by the parallel definition link,
-- but for clarity, we leave it open here
-- the interesting open proof obligation is anno2, of course
incl <- ginclusion lg sig' sig1
return $ insEdgeNub (n',n1,DGLink {
dgl_morphism = incl,
dgl_type = GlobalThm LeftOpen anno2 LeftOpen,
dgl_origin = DGExtension }) dg1
_ -> return dg1
return (sp1':sps', JustNode nsig1, dg2)
Free_spec asp poss ->
do let sp1 = item asp
(sp', NodeSig n' gsigma'@(G_sign lid' gsig), dg') <-
ana_SPEC lg gctx nsig (inc name) opts sp1
let pos = poss
incl <- adjustPos pos $ ginclusion lg (getMaybeSig nsig) gsigma'
let node_contents = DGNode {
dgn_name = name,
dgn_theory = G_theory lid' gsig noSens, -- delta is empty
dgn_nf = Nothing,
dgn_sigma = Nothing,
dgn_origin = DGFree }
node = getNewNode dg'
link = (n',node,DGLink {
dgl_morphism = incl,
dgl_type = FreeDef nsig,
dgl_origin = DGFree })
return (Free_spec (replaceAnnoted sp' asp) poss,
NodeSig node gsigma',
insEdgeNub link $
insNode (node,node_contents) dg')
Cofree_spec asp poss ->
do let sp1 = item asp
(sp', NodeSig n' gsigma'@(G_sign lid' gsig), dg') <-
ana_SPEC lg gctx nsig (inc name) opts sp1
let pos = poss
incl <- adjustPos pos $ ginclusion lg (getMaybeSig nsig) gsigma'
let node_contents = DGNode {
dgn_name = name,
dgn_theory = G_theory lid' gsig noSens, -- delta is empty
dgn_nf = Nothing,
dgn_sigma = Nothing,
dgn_origin = DGCofree }
node = getNewNode dg'
link = (n',node,DGLink {
dgl_morphism = incl,
dgl_type = CofreeDef nsig,
dgl_origin = DGCofree })
return (Cofree_spec (replaceAnnoted sp' asp) poss,
NodeSig node gsigma',
insEdgeNub link $
insNode (node,node_contents) dg')
Local_spec asp asp' poss ->
do let sp1 = item asp
sp1' = item asp'
(sp2, nsig'@(NodeSig _ (G_sign lid' sigma')), dg') <-
ana_SPEC lg gctx nsig (extName "L" name) opts sp1
(sp2', NodeSig n'' (G_sign lid'' sigma''), dg'') <-
ana_SPEC lg (gannos,genv,dg') (JustNode nsig') (inc name) opts sp1'
let gsigma = getMaybeSig nsig
G_sign lid sigma <- return gsigma
sigma1 <- coerceSign lid' lid "Analysis of local spec" sigma'
sigma2 <- coerceSign lid'' lid "Analysis of local spec" sigma''
let sys = sym_of lid sigma
sys1 = sym_of lid sigma1
sys2 = sym_of lid sigma2
pos = poss
mor3 <- if isStructured opts then return (ide lid sigma2)
else adjustPos pos $ cogenerated_sign lid
(sys1 `Set.difference` sys) sigma2
let sigma3 = dom lid mor3
-- gsigma2 = G_sign lid sigma2
gsigma3 = G_sign lid sigma3
sys3 = sym_of lid sigma3
when (not( isStructured opts ||
sys2 `Set.difference` sys1 `Set.isSubsetOf` sys3))
$ pplain_error () (text
"attempt to hide the following symbols from the local environment"
$$ printText ((sys2 `Set.difference` sys1) `Set.difference` sys3))
pos
let node_contents = DGNode {
dgn_name = name,
dgn_theory = G_theory lid sigma3 noSens,
dgn_nf = Nothing,
dgn_sigma = Nothing,
dgn_origin = DGLocal }
node = getNewNode dg''
link = (n'',node,DGLink {
dgl_morphism = gEmbed (G_morphism lid mor3),
dgl_type = HidingDef,
dgl_origin = DGLocal })
return (Local_spec (replaceAnnoted sp2 asp)
(replaceAnnoted sp2' asp')
poss,
NodeSig node gsigma3,
insEdgeNub link $
insNode (node,node_contents) dg'')
Closed_spec asp pos ->
do let sp1 = item asp
l = getLogic nsig
-- analyse spec with empty local env
(sp', NodeSig n' gsigma', dg') <-
ana_SPEC lg gctx (EmptyNode l) (inc name) opts sp1
let gsigma = getMaybeSig nsig
adj = adjustPos pos
gsigma'' <- adj $ gsigUnion lg gsigma gsigma'
G_sign lid'' gsig'' <- return gsigma''
incl1 <- adj $ ginclusion lg gsigma gsigma''
incl2 <- adj $ ginclusion lg gsigma' gsigma''
let node = getNewNode dg'
node_contents = DGNode {
dgn_name = name,
dgn_theory = G_theory lid'' gsig'' noSens,
dgn_nf = Nothing,
dgn_sigma = Nothing,
dgn_origin = DGClosed }
link1 = DGLink {
dgl_morphism = incl1,
dgl_type = GlobalDef,
dgl_origin = DGClosedLenv }
link2 = (n',node,DGLink {
dgl_morphism = incl2,
dgl_type = GlobalDef,
dgl_origin = DGClosed })
insLink1 = case nsig of
EmptyNode _ -> id
JustNode (NodeSig n _) -> insEdgeNub (n, node, link1)
return (Closed_spec (replaceAnnoted sp' asp) pos,
NodeSig node gsigma'',
insLink1 $
insEdgeNub link2 $
insNode (node,node_contents) dg')
Qualified_spec (Logic_name ln sublog) asp pos -> do
l <- lookupLogic "Static analysis: " (tokStr ln) lg
-- analyse spec with empty local env
(sp', NodeSig n' gsigma', dg') <-
ana_SPEC lg gctx (EmptyNode l) (inc name) opts (item asp)
-- construct union with local env
let gsigma = getMaybeSig nsig
adj = adjustPos pos
gsigma'' <- adj $ gsigUnion lg gsigma gsigma'
G_sign lid'' gsig'' <- return gsigma''
incl1 <- adj $ ginclusion lg gsigma gsigma''
incl2 <- adj $ ginclusion lg gsigma' gsigma''
let node = getNewNode dg'
node_contents = DGNode {
dgn_name = name,
dgn_theory = G_theory lid'' gsig'' noSens,
dgn_nf = Nothing,
dgn_sigma = Nothing,
dgn_origin = DGLogicQual }
link1 = DGLink {
dgl_morphism = incl1,
dgl_type = GlobalDef,
dgl_origin = DGLogicQualLenv }
link2 = (n',node,DGLink {
dgl_morphism = incl2,
dgl_type = GlobalDef,
dgl_origin = DGLogicQual })
insLink1 = case nsig of
EmptyNode _ -> id
JustNode (NodeSig n _) -> insEdgeNub (n, node, link1)
return (Qualified_spec (Logic_name ln sublog) (replaceAnnoted sp' asp) pos,
NodeSig node gsigma'',
insLink1 $
insEdgeNub link2 $
insNode (node,node_contents) dg')
Group asp pos -> do
(sp',nsig',dg') <- ana_SPEC lg gctx nsig name opts (item asp)
return (Group (replaceAnnoted sp' asp) pos,nsig',dg')
Spec_inst spname afitargs pos -> do
let adj = adjustPos pos
case Map.lookup spname genv of
Nothing -> fatal_error
("Specification "++ showPretty spname " not found") pos
Just (ViewEntry _) ->
fatal_error
(showPretty spname " is a view, not a specification") pos
Just (ArchEntry _) ->
fatal_error
(showPretty spname
" is an architectural, not a structured specification") pos
Just (UnitEntry _) ->
fatal_error
(showPretty spname
" is a unit specification, not a structured specification") pos
Just (RefEntry) ->
fatal_error
(showPretty spname
" is a refinement specification, not a structured specification") pos
Just (SpecEntry gs@(imps,params,_,body@(NodeSig nB gsigmaB))) ->
case (\x y -> (x,x-y)) (length afitargs) (length params) of
-- the case without parameters leads to a simpler dg
(0,0) -> do
gsigma@(G_sign lid gsig) <-
adj $ gsigUnion lg (getMaybeSig nsig) gsigmaB
case nsig of
-- the subcase with empty local env leads to an even simpler dg
EmptyNode _ ->
-- if the node shall not be named and the logic does not change,
if isInternal name && langNameSig gsigma==langNameSig gsigmaB
-- then just return the body
then return (sp,body,dg)
-- otherwise, we need to create a new one
else do
incl <- adj $ ginclusion lg gsigmaB gsigma
let node = getNewNode dg
node_contents = DGNode {
dgn_name = name,
dgn_theory = G_theory lid gsig noSens,
dgn_nf = Nothing,
dgn_sigma = Nothing,
dgn_origin = DGSpecInst spname}
link = (nB,node,DGLink {
dgl_morphism = incl,
dgl_type = GlobalDef,
dgl_origin = DGSpecInst spname})
return (sp,
NodeSig node gsigma,
insEdgeNub link $
insNode (node,node_contents) dg)
-- the subcase with nonempty local env
JustNode (NodeSig n sigma) -> do
incl1 <- adj $ ginclusion lg sigma gsigma
incl2 <- adj $ ginclusion lg gsigmaB gsigma
let node = getNewNode dg
node_contents = DGNode {
dgn_name = name,
dgn_theory = G_theory lid gsig noSens,
dgn_nf = Nothing,
dgn_sigma = Nothing,
dgn_origin = DGSpecInst spname}
link1 = (n,node,DGLink {
dgl_morphism = incl1,
dgl_type = GlobalDef,
dgl_origin = DGSpecInst spname})
link2 = (nB,node,DGLink {
dgl_morphism = incl2,
dgl_type = GlobalDef,
dgl_origin = DGSpecInst spname})
return (sp,
NodeSig node gsigma,
insEdgeNub link1 $
insEdgeNub link2 $
insNode (node,node_contents) dg)
-- now the case with parameters
(_,0) -> do
let fitargs = map item afitargs
(fitargs',dg',args,_) <-
foldl anaFitArg (return ([],dg,[],extName "A" name))
(zip params fitargs)
let actualargs = reverse args
(gsigma',morDelta) <- adj $ apply_GS lg gs actualargs
gsigmaRes <- adj $ gsigUnion lg (getMaybeSig nsig) gsigma'
G_sign lidRes gsigRes <- return gsigmaRes
incl1 <- adj $ ginclusion lg (getMaybeSig nsig) gsigmaRes
incl2 <- adj $ ginclusion lg gsigma' gsigmaRes
morDelta' <- comp Grothendieck (gEmbed morDelta) incl2
let node = getNewNode dg'
node_contents = DGNode {
dgn_name = name,
dgn_theory = G_theory lidRes gsigRes noSens,
dgn_nf = Nothing,
dgn_sigma = Nothing,
dgn_origin = DGSpecInst spname}
link1 = DGLink {
dgl_morphism = incl1,
dgl_type = GlobalDef,
dgl_origin = DGSpecInst spname}
insLink1 = case nsig of
EmptyNode _ -> id
JustNode (NodeSig n _) -> insEdgeNub (n, node, link1)
link2 = (nB,node,DGLink {
dgl_morphism = morDelta',
dgl_type = GlobalDef,
dgl_origin = DGSpecInst spname})
parLinks = catMaybes (map (parLink gsigmaRes node) actualargs)
return (Spec_inst spname
(map (uncurry replaceAnnoted)
(zip (reverse fitargs') afitargs))
pos,
NodeSig node gsigmaRes ,
foldr insEdgeNub
(insLink1 $
insEdgeNub link2 $
insNode (node,node_contents) dg')
parLinks)
where
anaFitArg res (nsig',fa) = do
(fas',dg1,args,name') <- res
(fa',dg',arg) <- ana_FIT_ARG lg (gannos,genv,dg1)
spname imps nsig' opts name' fa
return (fa':fas',dg',arg:args,inc name')
parLink gsigma' node (_mor_i, NodeSig nA_i sigA_i) = do
incl <- maybeResult $ ginclusion lg sigA_i gsigma'
let link = DGLink {
dgl_morphism = incl,
dgl_type = GlobalDef,
dgl_origin = DGFitSpec }
return (nA_i,node,link)
-- finally the case with conflicting numbers of formal and actual parameters
_ ->
fatal_error
(showPretty spname " expects "++show (length params)++" arguments"
++" but was given "++show (length afitargs)) pos
Data (Logic lidD) (Logic lidP) asp1 asp2 pos ->
do let sp1 = item asp1
sp2 = item asp2
adj = adjustPos pos
Comorphism cid <- adj $ logicInclusion lg (Logic lidD) (Logic lidP)
let lidD' = sourceLogic cid
lidP' = targetLogic cid
(sp1', NodeSig n' (G_sign lid' sigma'), dg1) <-
ana_SPEC lg gctx (EmptyNode (Logic lidD)) (inc name) opts sp1
sigmaD <- adj $ coerceSign lid' lidD' "Analysis of data spec" sigma'
(sigmaD',sensD') <- adj $ map_sign cid sigmaD
let gsigmaD' = G_sign lidP' sigmaD'
node_contents = DGNode {
dgn_name = name,
dgn_theory = G_theory lidP' sigmaD' $ toThSens sensD',
dgn_nf = Nothing,
dgn_sigma = Nothing,
dgn_origin = DGData }
node = getNewNode dg1
link = (n',node,DGLink {
dgl_morphism = GMorphism cid sigmaD (ide lidP' sigmaD'),
dgl_type = GlobalDef,
dgl_origin = DGData })
dg2 = insEdgeNub link $
insNode (node,node_contents) dg1
nsig2 = NodeSig node gsigmaD'
(sp2',nsig3,dg3) <-
ana_SPEC lg (gannos,genv,dg2) (JustNode nsig2) name opts sp2
return (Data (Logic lidD) (Logic lidP)
(replaceAnnoted sp1' asp1)
(replaceAnnoted sp2' asp2)
pos,
nsig3,
dg3)
-- analysis of renamings
ana_ren1 :: LogicGraph -> GMorphism -> G_mapping -> Result GMorphism
ana_ren1 _ (GMorphism r sigma mor)
(G_symb_map (G_symb_map_items_list lid sis)) = do
let lid2 = targetLogic r
sis1 <- coerceSymbMapItemsList lid lid2 "Analysis of renaming" sis
rmap <- stat_symb_map_items lid2 sis1
mor1 <- induced_from_morphism lid2 rmap (cod lid2 mor)
mor2 <- comp lid2 mor mor1
return $ GMorphism r sigma mor2
ana_ren1 lg mor (G_logic_translation (Logic_code tok src tar pos1)) = do
let adj = adjustPos pos1
G_sign srcLid srcSig <- return (cod Grothendieck mor)
c <- adj $ case tok of
Just ctok -> do
Comorphism cid <- lookupComorphism (tokStr ctok) lg
when (isJust src && getLogicStr (fromJust src) /=
language_name (sourceLogic cid))
(fail (getLogicStr (fromJust src)++"is not the source logic of "
++ language_name cid))
when (isJust tar && getLogicStr (fromJust tar) /=
language_name (targetLogic cid))
(fail (getLogicStr (fromJust tar)++"is not the target logic of "
++ language_name cid))
return (Comorphism cid)
Nothing -> case tar of
Just (Logic_name l _) -> do
tarL <- lookupLogic "with logic: " (tokStr l) lg
logicInclusion lg (Logic srcLid) tarL
Nothing -> fail "with logic: cannot determine comorphism"
mor1 <- adj $ gEmbedComorphism c (G_sign srcLid srcSig)
adj $ comp Grothendieck mor mor1
where getLogicStr (Logic_name l _) = tokStr l
ana_ren :: LogicGraph -> Result GMorphism -> G_mapping -> Result GMorphism
ana_ren lg mor_res ren =
do mor <- mor_res
ana_ren1 lg mor ren
ana_RENAMING :: LogicGraph -> G_sign -> HetcatsOpts -> RENAMING
-> Result GMorphism
ana_RENAMING lg gSigma opts (Renaming ren _) =
if isStructured opts
then return (ide Grothendieck gSigma)
else foldl (ana_ren lg) (return (ide Grothendieck gSigma)) ren
-- analysis of restrictions
ana_restr1 :: DGraph -> G_sign -> GMorphism -> G_hiding -> Result GMorphism
ana_restr1 _ (G_sign _lid _sigma) (GMorphism cid sigma1 mor)
(G_symb_list (G_symb_items_list lid' sis')) = do
let lid1 = sourceLogic cid
lid2 = targetLogic cid
sis1 <- coerceSymbItemsList lid' lid1 "Analysis of restriction" sis'
rsys <- stat_symb_items lid1 sis1
let sys = sym_of lid1 sigma1
let sys' = Set.filter (\sy -> any (\rsy -> matches lid1 sy rsy) rsys)
sys
-- if sys' `disjoint` () then return ()
-- else plain_error () "attempt to hide symbols from the local environment" pos
mor1 <- cogenerated_sign lid1 sys' sigma1
mor1' <- map_morphism cid mor1
mor2 <- comp lid2 mor1' mor
return $ GMorphism cid (dom lid1 mor1) mor2
ana_restr1 _dg _gSigma _mor
(G_logic_projection (Logic_code _tok _src _tar pos1)) =
fatal_error "no analysis of logic projections yet" pos1
ana_restr :: DGraph -> G_sign -> Result GMorphism -> G_hiding
-> Result GMorphism
ana_restr dg gSigma mor_res restr =
do mor <- mor_res
ana_restr1 dg gSigma mor restr
ana_RESTRICTION :: DGraph -> G_sign -> G_sign -> HetcatsOpts -> RESTRICTION
-> Result (GMorphism, Maybe GMorphism)
ana_RESTRICTION dg gSigma gSigma' opts restr =
ana_RESTRICTION' dg gSigma gSigma' (isStructured opts) restr
ana_RESTRICTION' :: DGraph -> G_sign -> G_sign -> Bool -> RESTRICTION
-> Result (GMorphism, Maybe GMorphism)
ana_RESTRICTION' _ _ gSigma True _ =
return (ide Grothendieck gSigma,Nothing)
ana_RESTRICTION' dg gSigma gSigma' False (Hidden restr _) =
do mor <- foldl (ana_restr dg gSigma)
(return (ide Grothendieck gSigma'))
restr
return (mor,Nothing)
-- ??? Need to check that local env is not affected !
ana_RESTRICTION' _ (G_sign lid sigma) (G_sign lid' sigma')
False (Revealed (G_symb_map_items_list lid1 sis) pos) =
do let sys = sym_of lid sigma -- local env
sys' = sym_of lid' sigma' -- "big" signature
adj = adjustPos pos
sis' <- adj $ coerceSymbMapItemsList lid1 lid'
"Analysis of restriction" sis
rmap <- adj $ stat_symb_map_items lid' sis'
let sys'' =
Set.fromList
[sy | sy <- Set.toList sys', rsy <- Map.keys rmap, matches lid' sy rsy]
-- domain of rmap intersected with sys'
-- domain of rmap should be checked to match symbols from sys' ???
sys1 <- adj $ coerceSymbolSet lid lid' "Analysis of restriction" sys
-- ??? this is too simple in case that local env is translated
-- to a different logic
mor1 <- adj $ generated_sign lid' (sys1 `Set.union` sys'') sigma'
mor2 <- adj $ induced_from_morphism lid' rmap (dom lid' mor1)
return (gEmbed (G_morphism lid' mor1),
Just (gEmbed (G_morphism lid' mor2)))
ana_FIT_ARG :: LogicGraph -> GlobalContext -> SPEC_NAME -> MaybeNode
-> NodeSig -> HetcatsOpts -> NODE_NAME -> FIT_ARG
-> Result (FIT_ARG, DGraph, (G_morphism,NodeSig))
ana_FIT_ARG lg gctx spname nsigI
(NodeSig nP (G_sign lidP sigmaP)) opts name
(Fit_spec asp gsis pos) = do
let adj = adjustPos pos
(sp', nsigA@(NodeSig nA (G_sign lidA sigmaA)), dg') <-
ana_SPEC lg gctx nsigI name opts (item asp)
G_symb_map_items_list lid sis <- homogenizeGM (Logic lidP) gsis
sigmaA' <- adj $ coerceSign lidA lidP "Analysis of fitting argument" sigmaA
mor <- adj $ if isStructured opts then return (ide lidP sigmaP)
else do
rmap <- stat_symb_map_items lid sis
rmap' <- if null sis then return Map.empty
else coerceRawSymbolMap lid lidP
"Analysis of fitting argument" rmap
induced_from_to_morphism lidP rmap' sigmaP sigmaA'
{-
let symI = sym_of lidP sigmaI'
symmap_mor = symmap_of lidP mor
-- are symbols of the imports left untouched?
if Set.all (\sy -> lookupFM symmap_mor sy == Just sy) symI
then return ()
else plain_error () "Fitting morphism must not affect import" ( pos)
-} -- ??? does not work
-- ??? also output some symbol that is affected
let link = (nP,nA,DGLink {
dgl_morphism = gEmbed (G_morphism lidP mor),
dgl_type = GlobalThm LeftOpen None LeftOpen,
dgl_origin = DGSpecInst spname})
return (Fit_spec (replaceAnnoted sp' asp) gsis pos,
insEdgeNub link dg',
(G_morphism lidP mor,nsigA)
)
ana_FIT_ARG lg (gannos,genv,dg) spname nsigI (NodeSig nP gsigmaP)
opts name fv@(Fit_view vn afitargs pos) = do
let adj = adjustPos pos
case Map.lookup vn genv of
Nothing -> fatal_error
("View "++ showPretty vn " not found") pos
Just (SpecEntry _) ->
fatal_error
(showPretty spname " is a specification, not a view") pos
Just (ArchEntry _) ->
fatal_error
(showPretty spname
" is an architectural specification, not a view ") pos
Just (UnitEntry _) ->
fatal_error
(showPretty spname
" is a unit specification, not a view") pos
Just (RefEntry) ->
fatal_error
(showPretty spname
" is a refinement specification, not a view") pos
Just (ViewEntry (src,mor,gs@(imps,params,_,target))) -> do
let nSrc = getNode src
nTar = getNode target
gsigmaS = getSig src
gsigmaT = getSig target
gsigmaI = getMaybeSig nsigI
GMorphism cid _ morHom <- return mor
let lid = targetLogic cid
when (not (language_name (sourceLogic cid) == language_name lid))
(fatal_error
"heterogeneous fitting views not yet implemented"
pos)
case (\x y -> (x,x-y)) (length afitargs) (length params) of
-- the case without parameters leads to a simpler dg
(0,0) -> case nsigI of
-- the subcase with empty import leads to a simpler dg
EmptyNode _ -> do
let link = (nP,nSrc,DGLink {
dgl_morphism = ide Grothendieck gsigmaP,
dgl_type = GlobalThm LeftOpen None LeftOpen,
dgl_origin = DGFitView spname})
return (fv,insEdgeNub link dg,(G_morphism lid morHom,target))
-- the subcase with nonempty import
JustNode (NodeSig nI _) -> do
gsigmaIS <- adj $ gsigUnion lg gsigmaI gsigmaS
when (not (is_subgsign gsigmaP gsigmaIS))
(pplain_error ()
(ptext "Parameter does not match source of fittig view."
$$ ptext "Parameter signature:"
$$ printText gsigmaP
$$ text "Source signature of fitting view (united with import):"
$$ printText gsigmaIS) pos)
G_sign lidI sigI1 <- return gsigmaI
sigI <- adj $ coerceSign lidI lid
"Analysis of instantiation with import" sigI1
mor_I <- adj $ morphism_union lid morHom $ ide lid sigI
gsigmaA <- adj $ gsigUnion lg gsigmaI gsigmaT
G_sign lidA gsigA <- return gsigmaA
G_sign lidP gsigP <- return gsigmaP
incl1 <- adj $ ginclusion lg gsigmaI gsigmaA
incl2 <- adj $ ginclusion lg gsigmaT gsigmaA
incl3 <- adj $ ginclusion lg gsigmaI gsigmaP
incl4 <- adj $ ginclusion lg gsigmaS gsigmaP
let [nA,n'] = newNodes 2 dg
node_contentsA = DGNode {
dgn_name = name,
dgn_theory = G_theory lidA gsigA noSens,
dgn_nf = Nothing,
dgn_sigma = Nothing,
dgn_origin = DGFitViewA spname}
node_contents' = DGNode {
dgn_name = inc name,
dgn_theory = G_theory lidP gsigP noSens,
dgn_nf = Nothing,
dgn_sigma = Nothing,
dgn_origin = DGFitView spname}
link = (nP,n',DGLink {
dgl_morphism = ide Grothendieck gsigmaP,
dgl_type = GlobalThm LeftOpen None LeftOpen,
dgl_origin = DGFitView spname})
link1 = (nSrc,n',DGLink {
dgl_morphism = incl4,
dgl_type = GlobalDef,
dgl_origin = DGFitView spname})
link2 = (nTar,nA,DGLink {
dgl_morphism = incl2,
dgl_type = GlobalDef,
dgl_origin = DGFitViewA spname})
link3 = (nI,n',DGLink {
dgl_morphism = incl3,
dgl_type = GlobalDef,
dgl_origin = DGFitViewImp spname})
link4 = (nI,nA,DGLink {
dgl_morphism = incl1,
dgl_type = GlobalDef,
dgl_origin = DGFitViewAImp spname})
return (fv,
insEdgeNub link $
insEdgeNub link1 $
insEdgeNub link2 $
insEdgeNub link3 $
insEdgeNub link4 $
insNode (nA,node_contentsA) $
insNode (n',node_contents') dg,
(G_morphism lid mor_I, NodeSig nA gsigmaA))
-- now the case with parameters
(_,0) -> do
let fitargs = map item afitargs
(fitargs',dg',args,_) <-
foldl anaFitArg (return ([],dg,[],extName "A" name))
(zip params fitargs)
let actualargs = reverse args
(gsigmaA,mor_f) <- adj $ apply_GS lg gs actualargs
let gmor_f = gEmbed mor_f
gsigmaRes <- adj $ gsigUnion lg gsigmaI gsigmaA
G_sign lidRes gsigRes <- return gsigmaRes
mor1 <- adj $ comp Grothendieck mor gmor_f
incl1 <- adj $ ginclusion lg gsigmaA gsigmaRes
mor' <- adj $ comp Grothendieck gmor_f incl1
GMorphism cid1 _ mor1Hom <- return mor1
let lid1 = targetLogic cid1
when (not (language_name (sourceLogic cid1) == language_name lid1))
(fatal_error
("heterogeneous fitting views not yet implemented")
pos)
G_sign lidI sigI1 <- return gsigmaI
sigI <- adj $ coerceSign lidI lid1
"Analysis of instantiation with parameters" sigI1
theta <- adj $ morphism_union lid1 mor1Hom (ide lid1 sigI)
incl2 <- adj $ ginclusion lg gsigmaI gsigmaRes
incl3 <- adj $ ginclusion lg gsigmaI gsigmaP
incl4 <- adj $ ginclusion lg gsigmaS gsigmaP
G_sign lidP gsigP <- return gsigmaP
let [nA,n'] = newNodes 2 dg'
node_contentsA = DGNode {
dgn_name = name,
dgn_theory = G_theory lidRes gsigRes noSens,
dgn_nf = Nothing,
dgn_sigma = Nothing,
dgn_origin = DGFitViewA spname}
node_contents' = DGNode {
dgn_name = extName "V" name,
dgn_theory = G_theory lidP gsigP noSens,
dgn_nf = Nothing,
dgn_sigma = Nothing,
dgn_origin = DGFitView spname}
link = (nP,n',DGLink {
dgl_morphism = ide Grothendieck gsigmaP,
dgl_type = GlobalThm LeftOpen None LeftOpen,
dgl_origin = DGFitView spname})
link1 = (nSrc,n',DGLink {
dgl_morphism = incl4,
dgl_type = GlobalDef,
dgl_origin = DGFitView spname})
link2 = (nTar,nA,DGLink {
dgl_morphism = mor',
dgl_type = GlobalDef,
dgl_origin = DGFitViewA spname})
fitLinks = [link,link1,link2] ++ case nsigI of
EmptyNode _ -> []
JustNode (NodeSig nI _) -> let
link3 = (nI,n',DGLink {
dgl_morphism = incl3,
dgl_type = GlobalDef,
dgl_origin = DGFitViewImp spname})
link4 = (nI,nA,DGLink {
dgl_morphism = incl2,
dgl_type = GlobalDef,
dgl_origin = DGFitViewAImp spname})
in [link3,link4]
parLinks = catMaybes (map (parLink gsigmaRes nA) actualargs)
return (Fit_view vn
(map (uncurry replaceAnnoted)
(zip (reverse fitargs') afitargs))
pos,
foldr insEdgeNub
(insNode (nA,node_contentsA) $
insNode (n',node_contents') dg')
(fitLinks++parLinks),
(G_morphism lid1 theta, NodeSig nA gsigmaRes))
where
anaFitArg res (nsig',fa) = do
(fas',dg1,args,name') <- res
(fa',dg',arg) <- ana_FIT_ARG lg (gannos,genv,dg1)
spname imps nsig' opts name' fa
return (fa':fas',dg',arg:args,inc name')
parLink gsigmaRes node (_mor_i,nsigA_i) = do
let nA_i = getNode nsigA_i
incl <- maybeResult $ ginclusion lg (getSig nsigA_i) gsigmaRes
let link = DGLink {
dgl_morphism = incl,
dgl_type = GlobalDef,
dgl_origin = DGFitView spname }
return (nA_i,node,link)
-- finally the case with conflicting numbers of formal and actual parameters
_ ->
fatal_error
(showPretty spname " expects "++show (length params)++" arguments"
++" but was given "++show (length afitargs)) pos
-- Extension of signature morphisms (for instantitations)
-- first some auxiliary functions
{- not really needed:
-- for an Id, compute the list of components that are relevant for extension
idComponents :: Id -> Set.Set Id -> [Id]
idComponents (Id toks comps pos) ids =
foldl (\x y -> y++x) []
$ map (\id1 -> if id1 `Set.member` ids
then [id1]
else idComponents id1 ids)
comps
--
componentRelation :: Set.Set Id -> Set.Set (Id,[Id])
componentRelation ids =
Set.map (\id -> (id,idComponents id ids)) ids
-}
mapID :: Map.Map Id (Set.Set Id) -> Id -> Result Id
mapID idmap i@(Id toks comps pos1) =
case Map.lookup i idmap of
Nothing -> do
compsnew <- sequence $ map (mapID idmap) comps
return (Id toks compsnew pos1)
Just ids -> case Set.size ids of
0 -> return i
1 -> return $ Set.findMin ids
_ -> pplain_error i
(ptext "Identifier component " <+> printText i
<+> ptext "can be mapped in various ways:"
<+> printText ids) nullRange
extID1 :: Map.Map Id (Set.Set Id) -> Id
-> Result (EndoMap Id) -> Result (EndoMap Id)
extID1 idmap i@(Id toks comps pos1) m = do
m1 <- m
compsnew <- sequence $ map (mapID idmap) comps
if comps==compsnew
then return m1
else return (Map.insert i (Id toks compsnew pos1) m1)
extID :: Set.Set Id -> Map.Map Id (Set.Set Id) -> Result (EndoMap Id)
extID ids idmap = Set.fold (extID1 idmap) (return Map.empty) ids
extendMorphism :: G_sign -- ^ formal parameter
-> G_sign -- ^ body
-> G_sign -- ^ actual parameter
-> G_morphism -- ^ fitting morphism
-> Result(G_sign,G_morphism)
extendMorphism (G_sign lid sigmaP) (G_sign lidB sigmaB1)
(G_sign lidA sigmaA1) (G_morphism lidM fittingMor1) = do
-- for now, only homogeneous instantiations....
sigmaB <- coerceSign lidB lid "Extension of symbol map" sigmaB1
sigmaA <- coerceSign lidA lid "Extension of symbol map" sigmaA1
fittingMor <- coerceMorphism lidM lid "Extension of symbol map" fittingMor1
let symsP = sym_of lid sigmaP
symsB = sym_of lid sigmaB
idsB = Set.map (sym_name lid) symsB
h = symmap_of lid fittingMor
symbMapToRawSymbMap =
Map.foldWithKey (\sy1 sy2 -> Map.insert (symbol_to_raw lid sy1)
(symbol_to_raw lid sy2))
Map.empty
rh = symbMapToRawSymbMap h
idh = Map.foldWithKey
(\sy1 sy2 -> Rel.setInsert (sym_name lid sy1) (sym_name lid sy2))
Map.empty h
idhExt <- extID idsB idh
let rIdExt = Map.foldWithKey
(\id1 id2 -> Map.insert (id_to_raw lid id1) (id_to_raw lid id2))
Map.empty
(foldr (\i -> Map.delete i) idhExt $ Map.keys idh)
r = rh `Map.union` rIdExt
-- do we need combining function catching the clashes???
mor <- induced_from_morphism lid r sigmaB
let hmor = symmap_of lid mor
sigmaAD = cod lid mor
sigma <- final_union lid sigmaA sigmaAD
let illShared = (sym_of lid sigmaA `Set.intersection` sym_of lid sigmaAD )
Set.\\ Rel.image h symsP
when (not (Set.null illShared))
(pplain_error () (ptext
"Symbols shared between actual parameter and body must be in formal parameter"
$$ printText illShared ) nullRange)
let myKernel m = Set.fromDistinctAscList $ comb1 $ Map.assocs m
comb1 [] = []
comb1 (p : qs) =
comb2 p qs [] ++ comb1 qs
comb2 _ [] rs = rs
comb2 p@(a, b) ((c, d) : qs) rs =
comb2 p qs $ if b == d then (a, c) : rs else rs
newIdentifications = myKernel hmor Set.\\ myKernel h
when (not (Set.null newIdentifications))
(pplain_error () (ptext
"Fitting morphism leads to forbidden identifications"
$$ printText newIdentifications) nullRange)
incl <- inclusion lid sigmaAD sigma
mor1 <- comp lid mor incl
return (G_sign lid sigma, G_morphism lid mor1)
apply_GS :: LogicGraph -> ExtGenSig -> [(G_morphism,NodeSig)]
-> Result(G_sign,G_morphism)
apply_GS lg (nsigI,_params,gsigmaP,nsigB) args = do
let mor_i = map fst args
gsigmaA_i = map (getSig . snd) args
gsigmaB = getSig nsigB
gsigmaI = getMaybeSig nsigI
G_sign lidI sigmaI <- return gsigmaI
let idI = ide lidI sigmaI
gsigmaA <- gsigManyUnion lg gsigmaA_i
mor_f <- homogeneousMorManyUnion (G_morphism lidI idI:mor_i)
extendMorphism gsigmaP gsigmaB gsigmaA mor_f
-- | analyze a GENERICITY
-- Parameters: global context, current logic, just-structure-flag, GENERICITY
ana_GENERICITY :: LogicGraph -> GlobalContext -> AnyLogic -> HetcatsOpts
-> NODE_NAME -> GENERICITY
-> Result (GENERICITY, GenericitySig, DGraph)
-- zero parameters,
ana_GENERICITY _ (_,_,dg) l _ _
gen@(Genericity (Params []) (Imported imps) pos) = do
when (not (null imps))
(plain_error () "Parameterless specifications must not have imports"
pos)
return (gen,(EmptyNode l, [], EmptyNode l),dg)
-- one parameter ...
ana_GENERICITY lg gctx@(gannos,genv,_) l opts name
(Genericity (Params [asp]) imps pos) = do
(imps',nsigI,dg') <- ana_IMPORTS lg gctx l opts (extName "I" name) imps
(sp',nsigP,dg'') <- ana_SPEC lg (gannos,genv,dg') nsigI
name opts (item asp)
return (Genericity (Params [replaceAnnoted sp' asp]) imps' pos,
(nsigI, [nsigP], JustNode nsigP),
dg'')
-- ... and more parameters
ana_GENERICITY lg gctx@(gannos,genv,_) l opts name
(Genericity params imps pos) = do
let adj = adjustPos pos
(imps',nsigI,dg') <- ana_IMPORTS lg gctx l opts (extName "I" name) imps
(params',nsigPs,dg'') <-
ana_PARAMS lg (gannos,genv,dg') l nsigI opts (inc name) params
gsigmaP <- adj $ gsigManyUnion lg (map getSig nsigPs)
G_sign lidP gsigP <- return gsigmaP
let node_contents = DGNode {
dgn_name = name,
dgn_theory = G_theory lidP gsigP noSens,
dgn_nf = Nothing,
dgn_sigma = Nothing,
dgn_origin = DGFormalParams }
node = getNewNode dg''
dg''' = insNode (node,node_contents) dg''
inslink dgres (NodeSig n sigma) = do
dg <- dgres
incl <- adj $ ginclusion lg sigma gsigmaP
return (insEdgeNub (n,node,DGLink {
dgl_morphism = incl,
dgl_type = GlobalDef,
dgl_origin = DGFormalParams }) dg)
dg4 <- foldl inslink (return dg''') nsigPs
return (Genericity params' imps' pos,
(nsigI, nsigPs, JustNode $ NodeSig node gsigmaP),
dg4)
ana_PARAMS :: LogicGraph -> GlobalContext -> AnyLogic -> MaybeNode
-> HetcatsOpts -> NODE_NAME -> PARAMS
-> Result (PARAMS, [NodeSig], DGraph)
ana_PARAMS lg (gannos,genv,dg) _ nsigI opts name (Params asps) = do
(sps',pars,dg',_) <- foldl ana (return ([],[],dg,name)) (map item asps)
return (Params (map (uncurry replaceAnnoted)
(zip (reverse sps') asps)),
reverse pars,
dg')
where
ana res sp = do
(sps',pars,dg1,n) <- res
(sp',par,dg') <- ana_SPEC lg (gannos,genv,dg1) nsigI n opts sp
return (sp':sps',par:pars,dg',inc n)
ana_IMPORTS :: LogicGraph -> GlobalContext -> AnyLogic -> HetcatsOpts
-> NODE_NAME -> IMPORTED
-> Result (IMPORTED, MaybeNode, DGraph)
ana_IMPORTS lg gctx@(_, _, dg) l opts name imps@(Imported asps) =
case asps of
[] -> return (imps, EmptyNode l, dg)
_ -> do
let sp = Union asps nullRange
(Union asps' _, nsig', dg') <-
ana_SPEC lg gctx (EmptyNode l) name opts sp
return (Imported asps', JustNode nsig', dg')
-- ??? emptyExplicit stuff needs to be added here
-- | analyze a VIEW_TYPE
-- The first three arguments give the global context
-- The AnyLogic is the current logic
-- The NodeSig is the signature of the parameter of the view
-- flag, whether just the structure shall be analysed
ana_VIEW_TYPE :: LogicGraph -> GlobalContext -> AnyLogic
-> MaybeNode -> HetcatsOpts -> NODE_NAME -> VIEW_TYPE
-> Result (VIEW_TYPE, (NodeSig, NodeSig), DGraph)
ana_VIEW_TYPE lg gctx@(gannos,genv,_) l parSig opts name
(View_type aspSrc aspTar pos) = do
(spSrc',srcNsig,dg') <-
ana_SPEC lg gctx (EmptyNode l) (extName "S" name) opts (item aspSrc)
(spTar',tarNsig,dg'') <-
ana_SPEC lg (gannos,genv,dg') parSig
(extName "T" name) opts (item aspTar)
return (View_type (replaceAnnoted spSrc' aspSrc)
(replaceAnnoted spTar' aspTar)
pos,
(srcNsig,tarNsig),
dg'')
homogenizeGM :: AnyLogic
-> [Syntax.AS_Structured.G_mapping] -> Result G_symb_map_items_list
homogenizeGM (Logic lid) gsis =
foldl homogenize1 (return (G_symb_map_items_list lid [])) gsis
where
homogenize1 res
(Syntax.AS_Structured.G_symb_map (G_symb_map_items_list lid1 sis1)) = do
(G_symb_map_items_list lid2 sis) <- res
sis1' <- coerceSymbMapItemsList lid1 lid2 "" sis1
return (G_symb_map_items_list lid2 (sis++sis1'))
homogenize1 res _ = res
-- | check if structured analysis should be performed
isStructured :: HetcatsOpts -> Bool
isStructured a = case analysis a of Structured -> True
_ -> False
-- | Auxiliary function for not yet implemented features
ana_err :: String -> a
ana_err fname =
error ("*** Analysis of " ++ fname ++ " is not yet implemented!")