-
Notifications
You must be signed in to change notification settings - Fork 0
/
apx.tex
1406 lines (1056 loc) · 68.2 KB
/
apx.tex
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
\chapter{haskell-syntax.k}
\begin{lstlisting}
// Syntax from haskell 2010 Report
// https://www.haskell.org/onlinereport/haskell2010/haskellch10.html#x17-17500010
module HASKELL-SYNTAX
syntax Integer ::= Token{([0-9]+)
| (([0][o]|[0][O])[0-7]+)
| (([0][x] | [0][X])[0-9a-fA-F]+)} [onlyLabel]
syntax CusFloat ::= Token{([0-9]+[\.][0-9]+([e E][\+\-]?[0-9]+)?)
|([0-9]+[e E][\+\-]?[0-9]+)} [onlyLabel]
syntax CusChar ::= Token{[\'](~[\'\\\&])[\']} [onlyLabel]
syntax CusString ::= Token{[\"](~[\"\\]*)[\"]} [onlyLabel]
syntax VarId ::= Token{[a-z\_][a-z A-Z\_0-9\']*} [onlyLabel] | "size" [onlyLabel]
syntax ConId ::= Token{[A-Z][a-zA-Z \_0-9\']*} [onlyLabel]
syntax VarSym ::= Token{
([\! \# \$ \% \& \* \+ \/ \> \? \^][\! \# \$ \% \& \* \+ \. \/ \< \= \> \? \@ \\ \^ \| \- \~ \:]*)
|[\-] | [\.]
|([\.][\! \# \$ \% \& \* \+ \/ \< \= \> \? \@ \\ \^ \| \- \~ \:][\! \# \$ \% \& \* \+ \. \/ \< \= \> \? \@ \\ \^ \| \- \~ \:]*)
| ([\-][\! \# \$ \% \& \* \+ \. \/ \< \= \? \@ \\ \^ \| \~ \:][\! \# \$ \% \& \* \+ \. \/ \< \= \> \? \@ \\ \^ \| \~ \:]*)
| ([\@][\! \# \$ \% \& \* \+ \. \/ \< \= \> \? \@ \\ \^ \| \- \~ \:]+)
| ([\~][\! \# \$ \% \& \* \+ \. \/ \< \= \> \? \@ \\ \^ \| \- \~ \:]+)
| ([\\][\! \# \$ \% \& \* \+ \. \/ \< \= \> \? \@ \\ \^ \| \- \~ \:]+)
| ([\|][\! \# \$ \% \& \* \+ \. \/ \< \= \> \? \@ \\ \^ \| \- \~ \:]+)
| ([\:][\! \# \$ \% \& \* \+ \. \/ \< \= \> \? \@ \\ \^ \| \- \~][\! \# \$ \% \& \* \+ \. \/ \< \= \> \? \@ \\ \^ \| \- \~ \:]*)
| ([\<][\! \# \$ \% \& \* \+ \. \/ \< \= \> \? \@ \\ \^ \| \~ \:][\! \# \$ \% \& \* \+ \. \/ \< \= \> \? \@ \\ \^ \| \- \~ \:]*)
| ([\=][\! \# \$ \% \& \* \+ \. \/ \< \= \? \@ \\ \^ \| \~ \:][\! \# \$ \% \& \* \+ \. \/ \< \= \> \? \@ \\ \^ \| \- \~ \:]*)} [onlyLabel]
syntax ConSym ::= Token{[\:][\! \# \$ \% \& \* \+ \. \/ \< \= \> \? \@ \\ \^ \| \- \~][\! \# \$ \% \& \* \+ \. \/ \< \= \> \? \@ \\ \^ \| \- \~ \:]*} [onlyLabel]
syntax IntFloat ::= "(" Integer ")" [bracket] //NOT OFFICIAL SYNTAX
| "(" CusFloat ")" [bracket]
syntax Literal ::= IntFloat | CusChar | CusString
syntax TyCon ::= ConId
syntax ModId ::= ConId | ConId "." ModId [klabel('conModId)]
syntax QTyCon ::= TyCon | ModId "." TyCon [klabel('conTyCon)]
syntax QVarId ::= VarId | ModId "." VarId [klabel('qVarIdCon)]
syntax QVarSym ::= VarSym | ModId "." VarSym [klabel('qVarSymCon)]
syntax QConSym ::= ConSym | ModId "." ConSym [klabel('qConSymCon)]
syntax TyVars ::= List{TyVar, ""} [klabel('typeVars)] //used in SimpleType syntax
syntax TyVar ::= VarId
syntax TyVarTuple ::= TyVar "," TyVar [klabel('twoTypeVarTuple)]
| TyVar "," TyVarTuple [klabel('typeVarTupleCon)]
syntax Con ::= ConId | "(" ConSym ")" [klabel('conSymBracket)]
syntax Var ::= VarId | "(" VarSym ")" [klabel('varSymBracket)]
syntax QVar ::= QVarId | "(" QConSym ")" [klabel('qVarBracket)]
syntax QCon ::= QTyCon | "(" GConSym ")" [klabel('gConBracket)]
syntax QConOp ::= GConSym | "`" QTyCon "`" [klabel('qTyConQuote)]
syntax QVarOp ::= QVarSym | "`" QVarId "`" [klabel('qVarIdQuote)]
syntax VarOp ::= VarSym | "`" VarId "`" [klabel('varIdQuote)]
syntax ConOp ::= ConSym | "`" ConId "`" [klabel('conIdQuote)]
syntax GConSym ::= ":" | QConSym
syntax Vars ::= Var
| Var "," Vars [klabel('varCon)]
syntax VarsType ::= Vars "::" Type [klabel('varAssign)]
syntax Ops ::= Op
| Op "," Ops [klabel('opCon)]
syntax Fixity ::= "infixl" | "infixr" | "infix"
syntax Op ::= VarOp | ConOp
syntax CQName ::= Var | Con | QVar
syntax QOp ::= QVarOp | QConOp
syntax ModuleName ::= "module" ModId [klabel('moduleName)]
syntax Module ::= ModuleName "where" Body [klabel('module)]
| ModuleName Exports "where" Body [klabel('moduleExp)]
| Body [klabel('moduleBody)]
syntax Body ::= "{" ImpDecls ";" TopDecls "}" [klabel('bodyimpandtop)]
| "{" ImpDecls "}" [klabel('bodyimpdecls)]
| "{" TopDecls "}" [klabel('bodytopdecls)]
syntax ImpDecls ::= List{ImpDecl, ";"} [klabel('impDecls)]
syntax Exports ::= "(" ExportList OptComma ")"
syntax ExportList ::= List{Export, ","}
syntax Export ::= QVar
| QTyCon OptCQList
| ModuleName
//optional cname list
syntax OptCQList ::= "(..)"
| "(" CQList ")" [klabel('cqListBracket)]
//Liyi: a check needs to place in preprocessing to check
//if the CQList is a cname list or a qvar list.
| "" [onlyLabel, klabel('emptyOptCNameList)]
syntax CQList ::= List{CQName, ","}
syntax ImpDecl ::= "import" OptQualified ModId OptAsModId OptImpSpec [klabel('impDecl)]
| "" [onlyLabel, klabel('emptyImpDecl)]
syntax OptQualified ::= "qualified"
| "" [onlyLabel, klabel('emptyQualified)]
syntax OptAsModId ::= "as" ModId
| "" [onlyLabel, klabel('emptyOptAsModId)]
syntax OptImpSpec ::= ImpSpec
| "" [onlyLabel, klabel('emptyOptImpSpec)]
syntax ImpSpecKey ::= "(" ImportList OptComma ")"
syntax ImpSpec ::= ImpSpecKey
| "hiding" ImpSpecKey
syntax ImportList ::= List{Import, ","}
syntax Import ::= Var
| TyCon CQList
syntax TopDecls ::= List{TopDecl, ";"} [klabel('topdeclslist)]
syntax TopDecl ::= Decl [klabel('topdecldecl)]
> "type" SimpleType "=" Type [klabel('type)]
| "data" OptContext SimpleType OptConstrs OptDeriving [klabel('data)]
| "newtype" OptContext SimpleType "=" NewConstr OptDeriving [klabel('newtype)]
| "class" OptContext ConId TyVar OptCDecls [klabel('class)]
| "instance" OptContext QTyCon Inst OptIDecls [klabel('instance)]
| "default" Types [klabel('default)]
| "foreign" FDecl [klabel('foreign)]
syntax FDecl ::= "import" CallConv CusString Var "::" FType
| "import" CallConv Safety CusString Var "::" FType
| "export" CallConv Safety CusString Var "::" FType
//Liyi: fdecl needs to use special function in preprocessing
// to get the actually elements from the impent and expent from the CusString
//did string analysis
syntax Safety ::= "unsafe" | "safe"
syntax CallConv ::= "ccall" | "stdcall" | "cplusplus" | "jvm" | "dotnet"
syntax FType ::= FrType
| FaType "->" FType // unsure about this one syntax is ambiguous UNFINISHED
syntax FrType ::= FaType
| "()"
syntax FaType ::= QTyCon ATypeList
//define declaration.
syntax OptDecls ::= "where" Decls | "" [onlyLabel, klabel('emptyOptDecls)]
syntax Decls ::= "{" DeclsList "}" [klabel('decls)]
syntax DeclsList ::= List{Decl, ";"} [klabel('declsList)]
syntax Decl ::= GenDecl
| FunLhs Rhs [klabel('declFunLhsRhs)]
| Pat Rhs [klabel('declPatRhs)]
syntax OptCDecls ::= "where" CDecls | "" [onlyLabel, klabel('emptyOptCDecls)]
syntax CDecls ::= "{" CDeclsList "}"
syntax CDeclsList ::= List{CDecl, ";"}
syntax CDecl ::= GenDecl
| FunLhs Rhs
| Var Rhs
syntax OptIDecls ::= "where" IDecls | "" [onlyLabel, klabel('emptyOptIDecls)]
syntax IDecls ::= "{" IDeclsList "}"
syntax IDeclsList ::= List{IDecl, ";"} [klabel('ideclslist)]
syntax IDecl ::= FunLhs Rhs [klabel('cdeclFunLhsRhs)]
| Var Rhs [klabel('cdeclVarRhs)]
| "" [onlyLabel, klabel('emptyIDecl)]
syntax GenDecl ::= VarsType
| Vars "::" Context "=>" Type [klabel('genAssignContext)]
| Fixity Ops
| Fixity Integer Ops
| "" [onlyLabel, klabel('emptyGenDecl)]
//three optional data type for the TopDecl data operator.
//deriving data type
syntax OptDeriving ::= Deriving | "" [onlyLabel, klabel('emptyDeriving)]
syntax Deriving ::= "deriving" DClass
| "deriving" "(" DClassList ")"
syntax DClassList ::= List{DClass, ","}
syntax DClass ::= QTyCon
syntax FunLhs ::= Var APatList [klabel('varApatList)]
| Pat VarOp Pat [klabel('patVarOpPat)]
| "(" FunLhs ")" APatList [klabel('funlhsApatList)]
syntax Rhs ::= "=" Exp OptDecls [klabel('eqExpOptDecls)]
| GdRhs OptDecls [klabel('gdRhsOptDecls)]
syntax GdRhs ::= Guards "=" Exp
| Guards "=" Exp GdRhs
syntax Guards ::= "|" GuardList
syntax GuardList ::= Guard | Guard "," GuardList [klabel('guardListCon)]
syntax Guard ::= Pat "<-" InfixExp
| "let" Decls
| InfixExp
//definition of exp
syntax Exp ::= InfixExp
> InfixExp "::" Type [klabel('expAssign)]
| InfixExp "::" Context "=>" Type [klabel('expAssignContext)]
syntax InfixExp ::= LExp
> "-" InfixExp [klabel('minusInfix)]
> LExp QOp InfixExp
syntax LExp ::= AExp
> "\\" APatList "->" Exp [klabel('lambdaFun)]
| "let" Decls "in" Exp [klabel('letIn)]
| "if" Exp OptSemicolon "then" Exp OptSemicolon "else" Exp [klabel('ifThenElse)]
| "case" Exp "of" "{" Alts "}" [klabel('caseOf)]
| "do" "{" Stmts "}" [klabel('doBlock)]
\end{lstlisting}
LExp is an important sort for the inference function. This is because LExp defines the different expression types which the inference function has specific rules for.
\begin{lstlisting}
syntax OptSemicolon ::= ";" | "" [onlyLabel, klabel('emptySemicolon)]
syntax OptComma ::= "," | "" [onlyLabel, klabel('emptyComma)]
syntax AExp ::= QVar [klabel('aexpQVar)]
| GCon [klabel('aexpGCon)]
| Literal [klabel('aexpLiteral)]
> AExp AExp [left, klabel('funApp)]
> QCon "{" FBindList "}"
| AExp "{" FBindList "}" //aexp cannot be qcon UNFINISHED
//Liyi: first, does not understand the syntax, it is the Qcon {FBindlist}
//or QCon? Second, place a check in preprosssing.
//and also check the Fbindlist here must be at least one argument
> "(" Exp ")" [bracket]
| "(" ExpTuple ")"
| "[" ExpList "]"
| "[" Exp OptExpComma ".." OptExp "]"
| "[" Exp "|" Quals "]"
| "(" InfixExp QOp ")"
| "(" QOp InfixExp ")" //qop cannot be - (minus) UNFINISHED
//Liyi: place a check here to check if QOp is a minus
syntax OptExpComma ::= "," Exp | "" [onlyLabel, klabel('emptyExpComma)]
syntax OptExp ::= Exp | "" [onlyLabel, klabel('emptyExp)]
syntax ExpList ::= Exp | Exp "," ExpList [right]
syntax ExpTuple ::= Exp "," Exp [right, klabel('twoExpTuple)]
| Exp "," ExpTuple [right, klabel('expTupleCon)]
//constr datatypes
syntax OptConstrs ::= "=" Constrs [klabel('nonemptyConstrs)] | "" [onlyLabel, klabel('emptyConstrs)]
syntax Constrs ::= Constr [klabel('singleConstr)] | Constr "|" Constrs [klabel('multConstr)]
syntax Constr ::= Con OptBangATypes [klabel('constrCon)] // (arity con = k, k ≥ 0) UNFINISHED
| SubConstr ConOp SubConstr
| Con "{" FieldDeclList "}"
syntax NewConstr ::= Con AType [klabel('newConstrCon)]
| Con "{" Var "::" Type "}"
syntax SubConstr ::= BType | "!" AType
syntax FieldDeclList ::= List{FieldDecl, ","}
syntax FieldDecl ::= VarsType
| Vars "::" "!" AType
syntax OptBangATypes ::= List{OptBangAType, " "} [klabel('optBangATypes)]
syntax OptBangAType ::= OptBang AType [klabel('optBangAType)]
syntax OptBang ::= "!" | "" [onlyLabel, klabel('emptyBang)]
syntax OptContext ::= Context "=>" | "" [onlyLabel, klabel('emptyContext)]
syntax Context ::= Class
| "(" Classes ")"
syntax Classes ::= List{Class, ","}
syntax SimpleClass ::= QTyCon TyVar [klabel('classCon)]
syntax Class ::= SimpleClass
| QTyCon "(" TyVar ATypeList ")"
//Liyi: a check in preprossing to check if the Atype list is empty
//it must have at least one item
//define type and simple type
syntax SimpleType ::= TyCon TyVars [klabel('simpleTypeCon)]
syntax Type ::= BType
| BType "->" Type [klabel('typeArrow)]
syntax BType ::= AType
| BType AType [klabel('baTypeCon)]
syntax ATypeList ::= List{AType, ""} [klabel('atypeList)]
syntax AType ::= GTyCon [klabel('atypeGTyCon)]
| TyVar [klabel('atypeTyVar)]
| "(" TypeTuple ")" [klabel('atypeTuple)]
| "[" Type "]" [klabel('tyList)]
| "(" Type ")" [bracket]
syntax TypeTuple ::= Type "," Type [right,klabel('twoTypeTuple)]
| Type "," TypeTuple [klabel('typeTupleCon)]
syntax Types ::= List{Type, ","}
syntax GConCommas ::= "," | "," GConCommas
syntax GConCommon ::= "()" | "[]" | "(" GConCommas ")" //was incorrect syntax
syntax GTyCon ::= QTyCon
| GConCommon
| "(->)"
syntax GCon ::= GConCommon
| QCon
//inst definition
syntax Inst ::= GTyCon
| "(" GTyCon TyVars ")" //TyVars must be distinct UNFINISHED
| "(" TyVarTuple ")" //TyVars must be distinct
| "[" TyVar "]" [klabel('tyVarList)]
| "(" TyVar "->" TyVar ")" //TyVars must be distinct
//pat definition
syntax Pat ::= LPat QConOp Pat
| LPat
syntax LPat ::= APat
| "-" IntFloat [klabel('minusPat)]
| GCon APatList [klabel('lpatCon)]//arity gcon = k UNFINISHED
syntax APatList ::= APat | APat APatList [klabel('apatCon)]
syntax APat ::= Var [klabel('apatVar)]
| Var "@" APat
| GCon
| QCon "{" FPats "}"
| Literal [klabel('apatLiteral)]
| "_"
| "(" Pat ")" [bracket]
| "(" PatTuple ")"
| "[" PatList "]"
| "~" APat
syntax PatTuple ::= Pat "," Pat [klabel('twoPatTuple)]
| Pat "," PatTuple [klabel('patTupleCon)]
syntax PatList ::= Pat
| Pat "," PatList [klabel('patListCon)]
syntax FPats ::= List{FPat, ","}
syntax FPat ::= QVar "=" Pat
//definition of quals
syntax Quals ::= Qual | Qual "," Quals [klabel('qualCon)]
syntax Qual ::= Pat "<-" Exp
| "let" Decls
| Exp
//definition of alts
syntax Alts ::= Alt | Alt ";" Alts
syntax Alt ::= Pat "->" Exp [klabel('altArrow)]
| Pat "->" Exp "where" Decls
| "" [onlyLabel, klabel('emptyAlt)]
//definition of stmts
syntax Stmts ::= StmtList Exp OptSemicolon
syntax StmtList ::= List{Stmt, ""}
syntax Stmt ::= Exp ";"
| Pat "<-" Exp ";"
| "let" Decls ";"
| ";"
//definition of fbind
syntax FBindList ::= List{FBind, ","}
syntax FBind ::= QVar "=" Exp
\end{lstlisting}
\chapter{haskell-configuration.k}
\begin{lstlisting}
requires "haskell-syntax.k"
module HASKELL-CONFIGURATION
imports HASKELL-SYNTAX
syntax KItem ::= "startImportRecursion"
syntax KItem ::= callInit(K)
//syntax KItem ::= initPreModule(K) [function]
//syntax KItem ::= tChecker(K) [function]
configuration
<T>
<k> $PGM:ModuleList ~> startImportRecursion </k>
<tempModule> .K </tempModule>
<tempCode> .K </tempCode>
<typeIterator> 1 </typeIterator>
<tempAlpha> .K </tempAlpha>
<tempAlphaMap> .Map </tempAlphaMap>
<tempBeta> .Map </tempBeta>
<tempT> .K </tempT>
<tempDelta> .Map </tempDelta>
<tempAlphaStar> .K </tempAlphaStar>
<tempBetaStar> .K </tempBetaStar>
<importTree> .List </importTree>
<recurImportTree> .List </recurImportTree>
<impTreeVMap> .Map </impTreeVMap>
<modules> //static information about a module
<module multiplicity="*">
<moduleName> .K </moduleName>
<moduleAlphaStar> .K </moduleAlphaStar>
<moduleBetaStar> .K </moduleBetaStar>
<moduleImpAlphas> .List </moduleImpAlphas>
<moduleImpBetas> .List </moduleImpBetas>
<moduleCompCode> .K </moduleCompCode>
<moduleTempCode> .K </moduleTempCode>
<imports> .Set </imports>
<classes> //static information about a module
<class multiplicity="*">
<className> .K </className>
</class>
</classes>
</module>
</modules>
</T>
endmodule
\end{lstlisting}
\chapter{haskell-preprocessing.k}
\begin{lstlisting}
//
requires "haskell-syntax.k"
requires "haskell-configuration.k"
module HASKELL-PREPROCESSING
imports HASKELL-SYNTAX
imports HASKELL-CONFIGURATION
//USER DEFINED LIST
//definition of ElemList
//syntax KItem ::= ElemList
syntax ElemList ::= List{Element,","} [strict]
// syntax Int ::= lengthOfList(ElemList) [function]
// rule lengthOfList(.ElemList) => 0
// rule lengthOfList(val(K:K),L:ElemList) => 1 +Int lengthOfList(L)
// rule lengthOfList(valValue(K:K),L:ElemList) => 1 +Int lengthOfList(L)
syntax Element ::= val(K) [strict]
syntax ElementResult ::= valValue(K)
syntax Element ::= ElementResult
syntax KResult ::= ElementResult
rule val(K:KResult) => valValue(K) [structural]
//form ElemList
// syntax ElemList ::= formElemList(K) [function]
//CONVERT ~> TO List
//list convert
// syntax List ::= convertToList(K) [function]
// rule convertToList(.K) => .List
// rule convertToList(A:KItem ~> B:K) => ListItem(A) convertToList(B)
syntax KItem ::= dealWithImports(K,K)
rule <k> 'modListSingle('module(A:K,, B:K)) => dealWithImports(A,B) ...</k>
(.Bag =>
<module>... //DOT DOT DOT MEANS OVERWRITE ONLY SOME OF THE DEFAULTS
<moduleName> A </moduleName>
...</module>
)
rule <k> 'modList('module(A:K,, B:K),, C:K) => dealWithImports(A,B) ~> C ...</k>
(.Bag =>
<module>... //DOT DOT DOT MEANS OVERWRITE ONLY SOME OF THE DEFAULTS
<moduleName> A </moduleName>
...</module>
)
// rule dealWithImports(Mod:K, A:K) => callInit(A)
// rule <k> dealWithImports(Mod:K, A:K) => callInit(A) ...</k>
rule <k> dealWithImports(Mod:K, 'bodyimpandtop(A:K,, B:K)) => .K ...</k>
<importTree> L:List => L importListConvert(Mod, A) </importTree>
<recurImportTree> L:List => L importListConvert(Mod, A) </recurImportTree>
<moduleName> Mod </moduleName>
<imports> S:Set (.Set => SetItem(A)) </imports>
<moduleTempCode> OldTemp:K => B </moduleTempCode>
rule <k> dealWithImports(Mod:K, 'bodyimpdecls(A:K)) => .K ...</k>
<importTree> L:List => L importListConvert(Mod, A) </importTree>
<recurImportTree> L:List => L importListConvert(Mod, A) </recurImportTree>
<moduleName> Mod </moduleName>
<imports> S:Set (.Set => SetItem(A)) </imports>
// rule <k> dealWithImports(Mod:K, 'bodytopdecls(A:K)) => callInit(A) ...</k>
rule <k> dealWithImports(Mod:K, 'bodytopdecls(B:K)) => .K ...</k>
<moduleName> Mod </moduleName>
<moduleTempCode> OldTemp:K => B </moduleTempCode>
//importlist convert
syntax List ::= importListConvert(K,K) [function]
syntax KItem ::= impObject(K,K)
rule importListConvert(Name:K, 'impDecls(A:K,, Rest:K)) => importListConvert(Name, A) importListConvert(Name, Rest)
rule importListConvert('moduleName(Name:K), 'impDecl(A:K,, Modid:K,, C:K,, D:K)) => ListItem(impObject(Name, Modid))
rule importListConvert(Name:K, .ImpDecls) => .List
/*NEW TODO ALGORITHM
1. Construct tree for module inclusion
2. Check tree for cycles
3. Go to each leaf and recursively go up the tree and build alpha* and beta* for the types of the module and the children
(and specify scoping) (desugar the scope so that each type specifies the scope) */
syntax KItem ::= "checkImportCycle"
syntax KItem ::= "recurseImportTree"
/* rule <k> performNextChecks
=> checkUseVars
~> (checkLabelUses
~> (checkBlockAddress(.K)
~> (checkNoNormalBlocksHavingLandingpad(.K, TNS -Set TES)
~> (checkAllExpBlocksHavingLandingpad(.K, TES)
~> (checkAllExpInFromInvoke(.K, TES)
~> (checkLandingpad
~> checkLandingDomResumes)))))) ...</k> */
rule <k> startImportRecursion => checkImportCycle
~> (recurseImportTree)...</k>
syntax KItem ::= cycleCheck(K,Map,List,List) [function] //current node, map of all nodes to visited or not, stack, graph
syntax Map ::= createVisitMap(List,Map) [function] //graph, visitmap
syntax KItem ::= getUnvisitedNode(K,K, Map) [function] //visitmap
syntax List ::= getNodeNeighbors(K,List) [function] //visitmap
rule <k> checkImportCycle
=> cycleCheck(.K,createVisitMap(I, .Map),.List,I) ...</k>
<importTree> I:List </importTree>
<impTreeVMap> .Map => createVisitMap(I, .Map) </impTreeVMap>
syntax KItem ::= "visited"
syntax KItem ::= "unvisited"
syntax KItem ::= "none"
rule createVisitMap(ListItem(impObject(A:K,B:K)) Rest:List, M:Map)
=> createVisitMap(Rest, M[A <- unvisited][B <- unvisited])
rule createVisitMap(.List, M:Map) => M
rule getUnvisitedNode(.K, .K, .Map) => none
rule getUnvisitedNode(.K, .K, (A:K |-> B:K) M:Map)
=> getUnvisitedNode(A, B, M)
rule getUnvisitedNode(A:KItem, unvisited, M:Map) => A
rule getUnvisitedNode(A:KItem, visited, M:Map)
=> getUnvisitedNode(.K, .K, M)
rule getNodeNeighbors(Node:K,.List) => .List
rule getNodeNeighbors(.K,Rest:List) => .List
rule getNodeNeighbors(Node:KItem,ListItem(impObject(Node,B:KItem)) Rest:List) => getNodeNeighbors(Node, Rest) ListItem(B)
rule getNodeNeighbors(Node:KItem,ListItem(impObject(A:KItem,B:KItem)) Rest:List) => getNodeNeighbors(Node, Rest)
requires Node =/=K A
rule cycleCheck(none, M:Map, .List, L:List) => .K
rule cycleCheck(.K, M:Map, .List, I:List) => cycleCheck(getUnvisitedNode(.K, .K, M), M, .List, I)
rule cycleCheck(.K, M:Map, ListItem(Node:K) S:List, I:List) => cycleCheck(Node, M, S, I)
rule cycleCheck(Node:K, M:Map, S:List, I:List)
=> cycleCheck(.K, M[Node <- visited], getNodeNeighbors(Node,I) S, I)
requires Node =/=K .K andBool Node =/=K none
rule cycleCheck(.K, M:Map, ListItem(Node:K) S:List, I:K) => cycleCheck(Node, M, S, I)
requires S =/=K .List
/*
rule cycleCheck(A:K,.K,.K,I:K) => cycleCheck(A,createVisitMap(I,.Map),.List,I)
rule cycleCheck(Node:K, M:Map, S:List, I:K) => cycleCheck(.K, M[Node <- visited], getNodeNeighbors(Node,I) S, I)
rule cycleCheck(.K, M:Map, ListItem(Node:K) S:List, I:K) => cycleCheck(Node, M, S, I)
//rule cycleCheck(.K, M:Map, .K, ListItem(impObject(A:K,B:K)) Rest:List) => cycleCheck(ListItem(impObject(A:K,B:K)) Rest:List)
*/
//COPY IMPORT GRAPH, NEED SECOND GRAPH FOR RECURSING, ADDITIONAL GRAPH FOR SELECTING IMPORTS FOR ALPHA* AND BETA*
//DFS for leaf
//acquire alpha and beta for leaf
//merge alpha and beta with imports to produce alpha* and beta*
//perform checks
//perform inferencing
//insert alpha* and beta* into importing modules
//remove all edges pointing to leaf
syntax KItem ::= "leafDFS"
syntax KItem ::= "getAlphaAndBeta"
syntax KItem ::= "getAlphaBetaStar"
syntax KItem ::= "performIndividualChecks"
syntax KItem ::= "performIndividualInferencing"
syntax KItem ::= "insertAlphaBetaStar"
syntax KItem ::= "removeAllEdges"
syntax KItem ::= "seeIfFinished"
rule <k> recurseImportTree => leafDFS
~> (getAlphaAndBeta
//~> (getAlphaBetaStar
~> (performIndividualInferencing))...</k>
//rule <k> dealWithImports(Mod:K, 'bodytopdecls(A:K)) => callInit(A) ...</k>
// rule <k> leaf
// => cycleCheck(.K,createVisitMap(I, .Map),.List,I) ...</k>
// <importTree> I:List </importTree>
syntax KItem ::= returnLeafDFS(K,List,Map) [function] //current node, map of all nodes to visited or not, stack, graph
syntax KItem ::= innerLeafDFS(K,List) [function]
syntax KItem ::= loadModule(K)
rule <k> leafDFS
=> returnLeafDFS(.K,I,M) ...</k>
<recurImportTree> I:List </recurImportTree>
<impTreeVMap> M:Map </impTreeVMap>
rule returnLeafDFS(.K,ListItem(impObject(Node:KItem,B:KItem)) I:List,M:Map) => returnLeafDFS(B,I,M)
rule returnLeafDFS(Node:KItem,I:List,M:Map) => returnLeafDFS(innerLeafDFS(Node,I),I,M)
requires innerLeafDFS(Node,I) =/=K none
rule returnLeafDFS(Node:KItem,I:List,M:Map) => loadModule(Node)
requires innerLeafDFS(Node,I) ==K none
rule innerLeafDFS(Node:KItem,ListItem(impObject(Node,B:KItem)) I:List) => B
rule innerLeafDFS(Node:KItem,ListItem(impObject(A:KItem,B:KItem)) I:List) => innerLeafDFS(Node,I)
requires Node =/=K A
rule innerLeafDFS(Node:KItem,.List) => none
// returnLeafDFS(Node:KItem,ListItem(impObject(Node,B:KItem)) I:List,M:Map) => returnLeafDFS(B,I,M)
//call before Checker Code
// rule <k> callInit(S:K) => initPreModule(S) ...</k>
// <tempModule> A:K => S </tempModule>
rule <k> loadModule(S:KItem) => .K ...</k>
<tempModule> A:K => S </tempModule>
rule <k> getAlphaAndBeta => initPreModule(Code) ...</k>
<tempModule> Mod:KItem </tempModule>
<moduleName> 'moduleName(Mod) </moduleName>
<moduleTempCode> Code:KItem </moduleTempCode>
//get alpha and beta
syntax KItem ::= Module(K, K)
syntax KItem ::= preModule(K,K) //(alpha, T)
// STEP 1 CONSTRUCT T AND ALPHA
// alpha = type
// T = newtype and data, temporary data structure
syntax KItem ::= initPreModule(K) [function]
syntax KItem ::= getPreModule(K, K) [function] //(Current term, premodule)
syntax KItem ::= makeT (K,K,K,K)
syntax KItem ::= fetchTypes (K,K,K,K)
syntax List ::= makeInnerT (K,K,K) [function] //LIST
syntax List ::= getTypeVars(K) [function] //LIST
syntax KItem ::= getCon(K) [function]
syntax List ::= getArgSorts(K) [function] //LIST
syntax KItem ::= AList(K)
syntax KItem ::= AObject(K,K) //(1st -> 2nd) map without idempotency
syntax KItem ::= ModPlusType(K,K)
syntax KItem ::= TList(K) //list of T objects for every new type introduced by data and newtype
syntax KItem ::= TObject(K,K,K,K) //(module name, type name, entire list of poly type vars, list of inner T pieces)
syntax KItem ::= InnerTPiece(K,K,K,K,K) //(type constructor, poly type vars, argument sorts, entire constr block, type name)
// rule initPreModule('module(I:ModuleName,, J:K)) => getPreModule(J,preModule(AList(.List),TList(.List)))
// rule initPreModule('moduleExp(I:ModuleName,, L:K,, J:K)) => getPreModule(J,preModule(AList(.List),TList(.List)))
// rule initPreModule('moduleBody(J:Body)) => getPreModule(J,preModule(AList(.List),TList(.List)))
rule initPreModule(J:K) => getPreModule(J,preModule(AList(.List),TList(.List)))
rule getPreModule('bodytopdecls(I:K), J:K) => getPreModule(I,J)
rule getPreModule('topdeclslist('type(A:K,, B:K),, Rest:K),J:K) => fetchTypes(A,B,Rest,J) //constructalpha
rule getPreModule('topdeclslist('data(A:K,, B:K,, C:K,, D:K),, Rest:K),J:K) => makeT(B,C,Rest,J)
rule getPreModule('topdeclslist('newtype(A:K,, B:K,, C:K,, D:K),, Rest:K),J:K) => makeT(B,C,Rest,J)
rule getPreModule('topdeclslist('topdecldecl(A:K),, Rest:K),J:K) => getPreModule(Rest,J)
rule getPreModule('topdeclslist('class(A:K,, B:K,, C:K,, D:K),, Rest:K),J:K) => getPreModule(Rest,J)
rule getPreModule('topdeclslist('instance(A:K,, B:K,, C:K,, D:K),, Rest:K),J:K) => getPreModule(Rest,J)
rule getPreModule('topdeclslist('default(A:K,, B:K,, C:K,, D:K),, Rest:K),J:K) => getPreModule(Rest,J)
rule getPreModule('topdeclslist('foreign(A:K,, B:K,, C:K,, D:K),, Rest:K),J:K) => getPreModule(Rest,J)
rule getPreModule(.TopDecls,J:K) => J
//rule getPreModule('module(I:ModuleName,L:K, J:K)) => preModule(J)
rule <k> fetchTypes('simpleTypeCon(I:TyCon,, H:TyVars), 'atypeGTyCon(C:K), Rest:K, preModule(AList(M:List), L:K)) => getPreModule(Rest,preModule(AList(ListItem(AObject(ModPlusType(ModName,I),C)) M), L)) ...</k>
<tempModule> ModName:KItem </tempModule>
rule <k> makeT('simpleTypeCon(I:TyCon,, H:TyVars), D:K, Rest:K, preModule(AList(M:List), TList(ListInside:List))) => getPreModule(Rest,preModule(AList(M),TList(ListItem(TObject(ModName,I,H,makeInnerT(I,H,D))) ListInside))) ...</k>
<tempModule> ModName:KItem </tempModule>
rule makeInnerT(A:K,B:K,'nonemptyConstrs(C:K)) => makeInnerT(A,B,C)
rule makeInnerT(A:K,B:K,'singleConstr(C:K)) => ListItem(InnerTPiece(getCon(C),getTypeVars(C),getArgSorts(C),C,A))
rule makeInnerT(A:K,B:K,'multConstr(C:K,, D:K)) => ListItem(InnerTPiece(getCon(C),getTypeVars(C),getArgSorts(C),C,A)) makeInnerT(A,B,D)
rule getTypeVars('constrCon(A:K,, B:K)) => getTypeVars(B)
rule getTypeVars('optBangATypes(A:K,, Rest:K)) => getTypeVars(A) getTypeVars(Rest)
rule getTypeVars('optBangAType('emptyBang(.KList),, Rest:K)) => getTypeVars(Rest)
rule getTypeVars('atypeGTyCon(A:K)) => .List
rule getTypeVars('atypeTyVar(A:K)) => ListItem(A)
rule getTypeVars(.OptBangATypes) => .List
//rule getCon('emptyConstrs()) => .K
//rule getCon('nonemptyConstrs(A:K)) => getCon(A)
rule getCon('constrCon(A:K,, B:K)) => A
//rule getArgSorts('constrCon(A:K,, B:K)) => B
rule getArgSorts('constrCon(A:K,, B:K)) => getArgSorts(B)
rule getArgSorts('optBangATypes(A:K,, Rest:K)) => getArgSorts(A) getArgSorts(Rest)
rule getArgSorts('optBangAType('emptyBang(.KList),, Rest:K)) => getArgSorts(Rest)
rule getArgSorts('atypeGTyCon(A:K)) => ListItem(A)
rule getArgSorts('atypeTyVar(A:K)) => .List
rule getArgSorts(.OptBangATypes) => .List
rule <k> preModule(A:K,T:K) => startTTransform ...</k>
<tempAlpha> OldAlpha:K => A </tempAlpha>
<tempT> OldT:K => T </tempT>
// STEP 2 PERFORM CHECKS
syntax KItem ::= "error"
syntax KItem ::= "startChecks"
syntax KItem ::= "checkNoSameKey"
//Keys of alpha and keys of T should be unique
syntax KItem ::= "checkTypeConsDontCollide"
//Make sure typeconstructors do not collide in T
syntax KItem ::= "makeAlphaMap"
//make map for alpha
syntax KItem ::= "checkAlphaNoLoops"
//alpha check for no loops
//check alpha to make sure that everything points to a T
syntax KItem ::= "checkArgSortsAreTargets"
//Make sure argument sorts [U] [W,V] are in the set of keys of alpha and targets of T, (keys of T)
syntax KItem ::= "checkParUsed"
//NEED TO CHECK all the polymorphic parameters from right appear on left. RIGHT SIDE ONLY
//NEED TO CHECK UNIQUENESS FOR POLY PARAM ON LEFT SIDE ONLY
// rule <k> preModule(A:K,T:K) => startChecks ...</k>
// <tempAlpha> OldAlpha:K => A </tempAlpha>
// <tempT> OldT:K => T </tempT>
/* rule <k> performNextChecks
=> checkUseVars
~> (checkLabelUses
~> (checkBlockAddress(.K)
~> (checkNoNormalBlocksHavingLandingpad(.K, TNS -Set TES)
~> (checkAllExpBlocksHavingLandingpad(.K, TES)
~> (checkAllExpInFromInvoke(.K, TES)
~> (checkLandingpad
~> checkLandingDomResumes)))))) ...</k> */
rule <k> startChecks
=> checkNoSameKey
~> (checkTypeConsDontCollide
~> (makeAlphaMap
~> (checkAlphaNoLoops
~> (checkArgSortsAreTargets
~> (checkParUsed))))) ...</k>
rule <k> checkTypeConsDontCollide
=> tyConCollCheck(T,.List,.Set) ...</k>
<tempT> T:K </tempT>
//syntax KItem ::= tChecker(K) [function]
syntax KItem ::= tyConCollCheck(K,K,K) [function] //(TList,List of Tycons,Set of Tycons)
syntax KItem ::= lengthCheck(K,K) [function]
//syntax KItem ::= tyConCollCheck(K,K,K) [function]
//syntax K ::= innerCollCheck(K) [function]
//syntax K ::= tyConCollCheckPasser(K, K) [function]
//rule tChecker(preModule(Alpha:Map,T:K,Mod:K)) => tyConCollCheck(innerCollCheck(T),preModule(Alpha,T,Mod))
//rule tyConCollCheck(.K,preModule(Alpha:Map,H:K,Mod:K)) => tyConCollCheck(innerCollCheck(H),preModule(Alpha,H,Mod))
rule tyConCollCheck(TList(ListItem(TObject(ModName:K, A:K,B:K,ListItem(InnerTPiece(Ty:K,E:K,F:K,H:K,G:K)) Inners:List)) Rest:List),J:List,D:Set) =>
tyConCollCheck(TList(ListItem(TObject(ModName,A,B,Inners)) Rest),ListItem(Ty) J, SetItem(Ty) D)
rule tyConCollCheck(TList(ListItem(TObject(ModName:K, A:K,B:K,.List)) Rest:List),J:List,D:Set) =>
tyConCollCheck(TList(Rest),J,D)
rule tyConCollCheck(TList(.List),J:List,D:Set) =>
lengthCheck(size(J),size(D))
rule lengthCheck(A:Int, B:Int) => .K
requires A ==Int B
rule lengthCheck(A:Int, B:Int) => error
requires A =/=Int B
//rule tyConCollCheck(TList(TObject(A:K,B:K,C:K) ~> Rest:K),J:K) => tyConCollCheckPasser(TList(innerCollCheck(TObject(A:K,B:K,C:K)) ~> Rest:K),J:K)
syntax KItem ::= keyCheck(K,K,K,K) [function] //(Alpha, T, List of names, Set of names)
rule <k> checkNoSameKey
=> keyCheck(A, T, .Set, .List) ...</k>
<tempAlpha> A:K </tempAlpha>
<tempT> T:K </tempT>
//rule <k> checkAlphaNoSameKey
// => akeyCheck(.K, .Set) ...</k>
rule keyCheck(AList(ListItem(AObject(A:K,B:K)) C:List), T:K, D:Set, G:List) => keyCheck(AList(C), T, SetItem(A) D, ListItem(A) G)
rule keyCheck(AList(.List), TList(ListItem(TObject(ModName:K, A:K,B:K,C:K)) Rest:List), D:Set, G:List) => keyCheck(AList(.List), TList(Rest), SetItem(A) D, ListItem(A) G)
rule keyCheck(AList(.List), TList(.List), D:Set, G:List) => lengthCheck(size(G),size(D))
syntax KItem ::= makeAlphaM(K,K) [function] //(Alpha, AlphaMap)
syntax KItem ::= tAlphaMap(K) //(AlphaMap) temp alphamap
rule <k> makeAlphaMap
=> makeAlphaM(A, .Map) ...</k>
<tempAlpha> A:K </tempAlpha>
rule makeAlphaM(AList(ListItem(AObject(A:K,B:K)) C:List), M:Map) => makeAlphaM(AList(C), M[A <- B])
rule makeAlphaM(AList(.List), M:Map) => tAlphaMap(M)
rule <k> tAlphaMap(M:K) => .K ...</k>
<tempAlphaMap> OldAlphaMap:K => M </tempAlphaMap>
// syntax KItem ::= tkeyCheck(K,K,K) [function] //(T,List of T,Set of T)
// rule <k> checkTNoSameKey
// => tkeyCheck(T, .Set, T) ...</k>
// <tempT> T:K </tempT>
// rule tkeyCheck(TList(ListItem(TObject(A:K,B:K,C:K)) Rest:List), D:Set, G:K) => tkeyCheck(TList(Rest), SetItem(A) D, G)
// rule tkeyCheck(TList(.List), D:Set, TList(G:List)) => lengthCheck(size(G),size(D))
syntax KItem ::= aloopCheck(K,K,K,K,K,K,K) [function] //(Alpha,List of Alpha,Set of Alpha,CurrNode,lengthcheck,T,BigSet)
rule <k> checkAlphaNoLoops
=> aloopCheck(A,.List,.Set,.K,.K,T,.Set) ...</k>
<tempAlphaMap> A:K </tempAlphaMap>
<tempT> T:K </tempT>
//aloopCheck set and list to check cycles
rule aloopCheck(Alpha:Map (A:KItem |-> B:KItem), D:List, G:Set, .K, .K,T:K,S:Set) => aloopCheck(Alpha, ListItem(B) ListItem(A) D, SetItem(B) SetItem(A) G, B, .K,T,S)
rule aloopCheck(Alpha:Map (H |-> B:KItem), D:List, G:Set, H:KItem, .K,T:K,S:Set) => aloopCheck(Alpha, ListItem(B) D, SetItem(B) G, B, .K,T,S)
rule aloopCheck(Alpha:Map, D:List, G:Set, H:KItem, .K,T:K,S:Set) => aloopCheck(Alpha, .List, .Set, .K, lengthCheck(size(G),size(D)),T,G S) //type rename loop ERROR
requires (notBool H in keys(Alpha)) andBool (H in typeSet(T, .Set) orBool H in S)
rule aloopCheck(Alpha:Map, D:List, G:Set, H:KItem, .K,T:K,S:Set) => error //terminal alpha rename is not in T ERROR
requires (notBool H in keys(Alpha)) andBool (notBool (H in typeSet(T, .Set) orBool H in S))
syntax Set ::= typeSet(K,K) [function] //(K, KSet)
rule typeSet(TList(ListItem(TObject(ModName:K, A:K,B:K,C:K)) Rest:List), D:Set) => typeSet(TList(Rest), SetItem(A) D)
rule typeSet(TList(.List), D:Set) => D
// rule aloopCheck(Alpha:Map, D:List, G:Set, H:KItem, .K) => keys(Alpha) ~> H
// requires notBool H in keys(Alpha)
rule aloopCheck(.Map, .List, .Set, .K, .K,T:K, S:Set) => .K
// rule aloopCheck(AList(Front:List ListItem(AObject(H,B:K)) C:List), D:List, G:Set, H:ConId) => aloopCheck(AList(C:List), ListItem(B) D, SetItem(B) G, B)
// syntax KItem ::= TList(K) //list of T objects for every new type introduced by data and newtype
// syntax KItem ::= TObject(K,K,K) //(type name, entire list of poly type vars, list of inner T pieces)
// syntax KItem ::= InnerTPiece(K,K,K,K,K) //(type constructor, poly type vars, argument sorts, entire constr block, type name)
//Make sure argument sorts [U] [W,V] are in the set of keys of alpha and targets of T, (keys of T)
syntax KItem ::= argSortCheck(K,K,K) [function] //(T,AlphaMap)
rule <k> checkArgSortsAreTargets
=> argSortCheck(T,A,typeSet(T,.Set)) ...</k>
<tempAlphaMap> A:K </tempAlphaMap>
<tempT> T:K </tempT>
rule argSortCheck(TList(ListItem(TObject(ModName:K, A:K,B:K,ListItem(InnerTPiece(C:K,D:K,ListItem(Arg:KItem) ArgsRest:List,E:K,F:K)) InnerRest:List)) TListRest:List),AlphaMap:Map,Tset:Set) => argSortCheck(TList(ListItem(TObject(ModName,A,B,ListItem(InnerTPiece(C,D,ArgsRest,E,F)) InnerRest)) TListRest),AlphaMap,Tset)
requires ((Arg in keys(AlphaMap)) orBool (Arg in Tset))
rule argSortCheck(TList(ListItem(TObject(ModName:K,A:K,B:K,ListItem(InnerTPiece(C:K,D:K,ListItem(Arg:KItem) ArgsRest:List,E:K,F:K)) InnerRest:List)) TListRest:List),AlphaMap:Map,Tset:Set) => error
requires (notBool ((Arg in keys(AlphaMap)) orBool (Arg in Tset)))
rule argSortCheck(TList(ListItem(TObject(ModName:K,A:K,B:K,ListItem(InnerTPiece(C:K,D:K,.List,E:K,F:K)) InnerRest:List)) TListRest:List),AlphaMap:Map,Tset:Set) => argSortCheck(TList(ListItem(TObject(ModName,A,B,InnerRest)) TListRest),AlphaMap,Tset)
rule argSortCheck(TList(ListItem(TObject(ModName:K,A:K,B:K,.List)) TListRest:List),AlphaMap:Map,Tset:Set) => argSortCheck(TList(TListRest),AlphaMap,Tset)
rule argSortCheck(TList(.List),AlphaMap:Map,Tset:Set) => .K
//NEED TO CHECK all the polymorphic parameters from right appear on left. RIGHT SIDE ONLY
//NEED TO CHECK UNIQUENESS FOR POLY PARAM ON LEFT SIDE ONLY
syntax KItem ::= parCheck(K,K) [function] //(T,AlphaMap)
syntax KItem ::= makeTyVarList(K,K,K) [function] //(TyVars, NewList)
syntax KItem ::= lengthRet(K,K,K) [function]
rule <k> checkParUsed
=> parCheck(T,.K) ...</k>
<tempT> T:K </tempT>
//rule makeParLists(TList(ListItem(TObject(A:K,ListItem(Arg:KItem) PolyList:List,C:K)) Rest:List),Tlist:List,Tset:Set) => makeParLists(TList(ListItem(TObject(A,PolyList,C)) Rest),ListItem(Arg) Tlist,SetItem(Arg) Tset)
rule parCheck(TList(ListItem(TObject(ModName:K,A:K,B:K,C:K)) Rest:List),.K) => parCheck(TList(ListItem(TObject(ModName,A:K,B:K,C:K)) Rest:List),makeTyVarList(B,.List,.Set))
rule parCheck(TList(ListItem(TObject(ModName:K,A:K,B:K,ListItem(InnerTPiece(C:K,ListItem(Par:KItem) ParRest:List,D:K,E:K,F:K)) InnerRest:List)) Rest:List),NewSet:Set) =>
parCheck(TList(ListItem(TObject(ModName,A,B,ListItem(InnerTPiece(C,ParRest,D,E,F)) InnerRest)) Rest),NewSet)
requires Par in NewSet
rule parCheck(TList(ListItem(TObject(ModName:K,A:K,B:K,ListItem(InnerTPiece(C:K,ListItem(Par:KItem) ParRest:List,D:K,E:K,F:K)) InnerRest:List)) Rest:List),NewSet:Set) => error
requires notBool (Par in NewSet)
rule parCheck(TList(ListItem(TObject(ModName:K,A:K,B:K,ListItem(InnerTPiece(C:K,.List,D:K,E:K,F:K)) InnerRest:List)) Rest:List),NewSet:Set) =>
parCheck(TList(ListItem(TObject(ModName,A,B,InnerRest)) Rest),NewSet)
rule parCheck(TList(ListItem(TObject(ModName:K,A:K,B:K,.List)) Rest:List),NewSet:Set) =>
parCheck(TList(Rest),NewSet)
rule parCheck(TList(.List),NewSet:Set) => .K
rule makeTyVarList('typeVars(A:K,,Rest:K),NewList:List,NewSet:Set) => makeTyVarList(Rest, ListItem(A) NewList, SetItem(A) NewSet)
rule makeTyVarList(.TyVars,NewList:List,NewSet:Set) => lengthRet(size(NewList),size(NewSet),NewSet)
rule lengthRet(A:Int, B:Int, C:K) => C
requires A ==Int B
rule lengthRet(A:Int, B:Int, C:K) => error
requires A =/=Int B
//rule argSortCheck(TList(ListItem(TObject(A:K,B:K,C:K)
// STEP 3 Transform T into beta
syntax KItem ::= "startTTransform"
syntax KItem ::= "constructDelta"
syntax KItem ::= "constructBeta"
rule <k> startTTransform
=> constructDelta
~> (constructBeta) ...</k>
rule <k> constructDelta
=> makeDelta(T,.Map) ...</k>
<tempT> T:K </tempT>
syntax KItem ::= makeDelta(K,Map) [function] //(T,Delta)
syntax KItem ::= newDelta(Map) //Delta
syntax KItem ::= newBeta(Map) //beta
syntax List ::= retPolyList(K,List) [function] //(T,Delta)
rule makeDelta(TList(ListItem(TObject(ModName:K,A:K,Polys:K,C:K)) Rest:List),M:Map) =>
makeDelta(TList(Rest),M[ModPlusType(ModName,A) <- size(retPolyList(Polys,.List))])
rule makeDelta(TList(.List),M:Map) => newDelta(M)
rule retPolyList('typeVars(A:K,,Rest:K),NewList:List) => retPolyList(Rest, ListItem(A) NewList)
rule retPolyList(.TyVars,L:List) => L
rule <k> newDelta(M:Map)
=> .K ...</k>
<tempDelta> OldDelta:K => M </tempDelta>
rule <k> constructBeta
=> makeBeta(T,.Map) ...</k>
<tempT> T:K </tempT>
syntax KItem ::= makeBeta(K,Map) [function] //(T,Beta,Delta)
rule makeBeta(TList(ListItem(TObject(ModName:K,A:K,B:K,ListItem(InnerTPiece(Con:K,H:K,D:K,E:K,F:K)) InnerRest:List)) Rest:List),Beta:Map) =>
makeBeta(TList(ListItem(TObject(ModName,A,B,InnerRest)) Rest),Beta[ModPlusType(ModName,Con) <- betaParser(E,B,A)])
rule makeBeta(TList(ListItem(TObject(ModName:K,A:K,B:K,.List)) Rest:List),Beta:Map) =>
makeBeta(TList(Rest),Beta)
rule makeBeta(TList(.List),Beta:Map) =>
newBeta(Beta)
// rule makeBeta(TList(ListItem(TObject(ModName:K,A:K,B:K,ListItem(InnerTPiece(C:K,H:K,D:K,E:K,F:K)) InnerRest:List)) Rest:List),Beta:Map) =>
// makeBeta(TList(ListItem(TObject(ModName,A,B,InnerRest)) Rest),Beta)
syntax KItem ::= betaParser(K,K,K) [function] //(Tree Piece,NewSyntax,Parameters,Constr)
syntax Set ::= getTyVarsRHS(K,List) [function]
syntax KItem ::= forAll(Set,K)
syntax KItem ::= funtype(K,K)
syntax Set ::= listToSet(List, Set) [function]
rule listToSet(ListItem(A:KItem) L:List, S:Set) => listToSet(L, SetItem(A) S)
rule listToSet(.List, S:Set) => S
//if optbangAtypes, need to see if first variable is a typecon
//if its a typecon then need to go into Delta and see the amount of parameters it has
//then count the number of optbangAtypes after the typecon
rule betaParser('constrCon(A:K,, B:K), Par:K, Con:K) => forAll(getTyVarsRHS(B,.List), betaParser(B, Par, Con))
rule betaParser('optBangATypes('optBangAType('emptyBang(.KList),, 'atypeTyVar(Tyv:K)),, Rest:K), Par:K, Con:K) => funtype(Tyv, betaParser(Rest, Par, Con))
rule betaParser('optBangATypes('optBangAType('emptyBang(.KList),, 'baTypeCon(A:K,, B:K)),, Rest:K), Par:K, Con:K) => funtype('baTypeCon(A:K,, B:K), betaParser(Rest, Par, Con))
rule betaParser('optBangATypes('optBangAType('emptyBang(.KList),, 'atypeGTyCon(Tyc:K)),, Rest:K), Par:K, Con:K) => funtype(Tyc, betaParser(Rest, Par, Con))
rule betaParser(.OptBangATypes, Par:K, Con:K) => 'simpleTypeCon(Con,, Par)
// rule betaParser('optBangATypes('optBangAType('emptyBang(.KList),, 'atypeGTyCon(Tyc:K)),, Rest:KItem)) => getTypeVars(A) getTypeVars(Rest)
// rule getTypeVars('optBangAType('emptyBang(.KList),, Rest:K)) => getTypeVars(Rest)