1 ///////////////////////////////////////////////////////////////////////////////
2 // This file is generated automatically using Prop (version 2.3.6),
3 // last updated on Nov 2, 1999.
4 // The original source file is "trs.pcc".
5 ///////////////////////////////////////////////////////////////////////////////
7 #define PROP_STRCMP_USED
8 #define PROP_TUPLE2_USED
11 ///////////////////////////////////////////////////////////////////////////////
13 // These are the routines for building the data structure
14 // for partial evaluting a TRS
16 ///////////////////////////////////////////////////////////////////////////////
30 ///////////////////////////////////////////////////////////////////////////////
32 // Constructors for represents a term
34 ///////////////////////////////////////////////////////////////////////////////
37 ///////////////////////////////////////////////////////////////////////////////
39 // Interface specification of datatype Term
41 ///////////////////////////////////////////////////////////////////////////////
45 ///////////////////////////////////////////////////////////////////////////////
47 // Instantiation of datatype Term
49 ///////////////////////////////////////////////////////////////////////////////
51 Term_CONSterm::Term_CONSterm (TreeAutomaton::Functor x_1
, Cons x_2
, int x_3
, Term
* x_4
)
52 : a_Term(tag_CONSterm
), _1(x_1
), _2(x_2
), _3(x_3
), _4(x_4
)
55 a_Term
* CONSterm (TreeAutomaton::Functor x_1
, Cons x_2
, int x_3
, Term
* x_4
)
57 return new Term_CONSterm (x_1
, x_2
, x_3
, x_4
);
59 Term_VARterm::Term_VARterm (int x_1
, Pat x_2
, Exp x_3
)
60 : a_Term(tag_VARterm
), _1(x_1
), _2(x_2
), _3(x_3
)
63 a_Term
* VARterm (int x_1
, Pat x_2
, Exp x_3
)
65 return new Term_VARterm (x_1
, x_2
, x_3
);
67 Term_CODEterm::Term_CODEterm (Exp x_CODEterm
)
68 : a_Term(tag_CODEterm
), CODEterm(x_CODEterm
)
71 a_Term
* CODEterm (Exp x_CODEterm
)
73 return new Term_CODEterm (x_CODEterm
);
75 Term_OPAQUEterm::Term_OPAQUEterm (Decls x_OPAQUEterm
)
76 : a_Term(tag_OPAQUEterm
), OPAQUEterm(x_OPAQUEterm
)
79 a_Term
* OPAQUEterm (Decls x_OPAQUEterm
)
81 return new Term_OPAQUEterm (x_OPAQUEterm
);
83 Term_PATterm::Term_PATterm (Pat x_PATterm
)
84 : a_Term(tag_PATterm
), PATterm(x_PATterm
)
87 a_Term
* PATterm (Pat x_PATterm
)
89 return new Term_PATterm (x_PATterm
);
97 ///////////////////////////////////////////////////////////////////////////////
99 // Constructor for TRS
101 ///////////////////////////////////////////////////////////////////////////////
102 TRS::TRS(RewritingCompiler
& C
)
103 : compiler(C
), Fmap(*C
.Fmap
), treeauto(*C
.Fmap
->tree_gen
),
106 MatchRules rules
= Fmap
.bottomup_rules
;
107 number_of_rules
= length(rules
);
108 rule_map
= new MatchRule
[number_of_rules
];
109 guard_map
= new Exp
[number_of_rules
];
110 lhs_map
= new Term
[number_of_rules
];
111 rhs_map
= new Term
[number_of_rules
];
112 num_var_map
= new int [number_of_rules
];
113 var_map
= new Exp
* [number_of_rules
];
114 var_pat_map
= new Pat
* [number_of_rules
];
115 residue_map
= new Residues
*[number_of_rules
];
116 literal_map
= new Literal
[Fmap
.G
.functors()];
117 optimized_map
= new Exp
[number_of_rules
];
122 for_each(MatchRule
, r
, rules
)
128 if (r
->rule_number
!= i
) bug("%LTRS::TRS()");
130 guard_map
[i
] = r
->_3
;
136 a_List
<Decl
> * _V1
= r
->_5
;
139 switch (_V1
->_1
->tag__
) {
140 case a_Decl::tag_MARKEDdecl
: {
141 if (((Decl_MARKEDdecl
*)_V1
->_1
)->_2
) {
142 switch (((Decl_MARKEDdecl
*)_V1
->_1
)->_2
->tag__
) {
143 case a_Decl::tag_REPLACEMENTdecl
: {
147 rhs_map
[i
] = OPAQUEterm(r
->_5
);
151 make_rhs(i
,((Decl_REPLACEMENTdecl
*)((Decl_MARKEDdecl
*)_V1
->_1
)->_2
)->_1
);
155 default: { goto L1
; } break;
159 default: { goto L1
; } break;
177 ///////////////////////////////////////////////////////////////////////////////
179 // Destructor for TRS
181 ///////////////////////////////////////////////////////////////////////////////
183 { delete [] rule_map
;
187 delete [] num_var_map
;
189 delete [] var_pat_map
;
190 delete [] residue_map
;
191 delete [] literal_map
;
192 delete [] optimized_map
;
195 ///////////////////////////////////////////////////////////////////////////////
197 // Method to determine if any expression is a pattern variable.
199 ///////////////////////////////////////////////////////////////////////////////
200 Bool
is_pattern_var(Exp exp
)
207 switch (exp
->tag__
) {
208 case a_Exp::tag_DOTexp
: {
209 if (((Exp_DOTexp
*)exp
)->_1
) {
210 switch (((Exp_DOTexp
*)exp
)->_1
->tag__
) {
211 case a_Exp::tag_SELECTORexp
: {
212 if (((Exp_SELECTORexp
*)((Exp_DOTexp
*)exp
)->_1
)->_1
) {
213 switch (((Exp_SELECTORexp
*)((Exp_DOTexp
*)exp
)->_1
)->_1
->tag__
) {
214 case a_Exp::tag_IDexp
: {
215 if (_equal_string(((Exp_IDexp
*)((Exp_SELECTORexp
*)((Exp_DOTexp
*)exp
)->_1
)->_1
)->IDexp
,"redex")) {
223 exp
= ((Exp_SELECTORexp
*)((Exp_DOTexp
*)exp
)->_1
)->_1
;
227 default: { goto L3
; } break;
240 case a_Exp::tag_SELECTORexp
: {
241 if (((Exp_SELECTORexp
*)exp
)->_1
) {
242 switch (((Exp_SELECTORexp
*)exp
)->_1
->tag__
) {
243 case a_Exp::tag_IDexp
: {
244 if (_equal_string(((Exp_IDexp
*)((Exp_SELECTORexp
*)exp
)->_1
)->IDexp
,"redex")) {
252 exp
= ((Exp_SELECTORexp
*)exp
)->_1
;
256 default: { goto L5
; } break;
260 default: { goto L4
; } break;
270 ///////////////////////////////////////////////////////////////////////////////
272 // Method to create the lhs
274 ///////////////////////////////////////////////////////////////////////////////
275 void TRS::make_lhs(Rule r
, Pat pat
)
277 lhs_map
[r
] = make_term(pat
);
279 num_var_map
[r
] = num_vars
;
280 var_map
[r
] = (Exp
*)mem_pool
.m_alloc(sizeof(Exp
)*num_vars
);
281 var_pat_map
[r
] = (Pat
*)mem_pool
.m_alloc(sizeof(Pat
)*num_vars
);
282 residue_map
[r
] = (Residues
*)mem_pool
.c_alloc(sizeof(Residues
)*num_vars
);
283 optimized_map
[r
] = NOexp
;
285 memcpy(var_map
[r
], vars
, sizeof(Exp
)*num_vars
);
286 memcpy(var_pat_map
[r
],var_pats
,sizeof(Pat
)*num_vars
);
289 ///////////////////////////////////////////////////////////////////////////////
291 // Method to create the lhs for rule r
293 ///////////////////////////////////////////////////////////////////////////////
294 Term
TRS::make_term(Pat pat
)
301 switch (pat
->tag__
) {
302 case a_Pat::tag_IDpat
: {
304 return make_term_var(pat
,pat
->selector
);
307 case a_Pat::tag_CONSpat
: {
309 return make_term(pat
,((Pat_CONSpat
*)pat
)->CONSpat
,
318 case a_Pat::tag_APPpat
: {
319 if (((Pat_APPpat
*)pat
)->_1
) {
320 switch (((Pat_APPpat
*)pat
)->_1
->tag__
) {
321 case a_Pat::tag_CONSpat
: {
322 if (((Pat_APPpat
*)pat
)->_2
) {
323 switch (((Pat_APPpat
*)pat
)->_2
->tag__
) {
324 case a_Pat::tag_TUPLEpat
: {
326 return make_term(pat
,((Pat_CONSpat
*)((Pat_APPpat
*)pat
)->_1
)->CONSpat
,((Pat_TUPLEpat
*)((Pat_APPpat
*)pat
)->_2
)->TUPLEpat
);
332 return make_term(pat
,((Pat_CONSpat
*)((Pat_APPpat
*)pat
)->_1
)->CONSpat
,
335 list_1_(((Pat_APPpat
*)pat
)->_2
)
353 case a_Pat::tag_ASpat
: {
355 pat
= ((Pat_ASpat
*)pat
)->_2
;
358 case a_Pat::tag_LITERALpat
: {
361 Fmap
.literal_map
.contains(((Pat_LITERALpat
*)pat
)->LITERALpat
)
366 Functor f
= Fmap
.literal_map
[((Pat_LITERALpat
*)pat
)->LITERALpat
];
367 literal_map
[f
] = ((Pat_LITERALpat
*)pat
)->LITERALpat
;
368 return CONSterm(f
,NOcons
,0,0);
374 case a_Pat::tag_LISTpat
: {
375 if (((Pat_LISTpat
*)pat
)->head
) {
380 LISTpat(((Pat_LISTpat
*)pat
)->cons
, ((Pat_LISTpat
*)pat
)->nil
, ((Pat_LISTpat
*)pat
)->head
->_2
, ((Pat_LISTpat
*)pat
)->tail
)
384 tl_pat
->ty
= pat
->ty
;
385 return make_term(pat
,((Pat_LISTpat
*)pat
)->cons
,
388 list_1_(((Pat_LISTpat
*)pat
)->head
->_1
,list_1_(tl_pat
))
395 if (((Pat_LISTpat
*)pat
)->tail
) {
397 pat
= ((Pat_LISTpat
*)pat
)->tail
;
401 return make_term(pat
,((Pat_LISTpat
*)pat
)->nil
,
412 case a_Pat::tag_MARKEDpat
: {
414 pat
= ((Pat_MARKEDpat
*)pat
)->_2
;
417 default: { goto L8
; } break;
427 ///////////////////////////////////////////////////////////////////////////////
429 // Method to create the lhs for rule r
431 ///////////////////////////////////////////////////////////////////////////////
432 Term
TRS::make_term(Pat pat
, Cons cons
, Pats pats
)
433 { int n
= length(pats
);
434 FunctorTable::Entry
* E
= Fmap
.type_map
.lookup(pat
->ty
);
435 if (E
== 0) { return PATterm(pat
); }
440 Ty _V2
= cons
->alg_ty
;
442 switch (_V2
->tag__
) {
443 case a_Ty::tag_TYCONty
: {
444 if (boxed(((Ty_TYCONty
*)_V2
)->_1
)) {
445 switch (((Ty_TYCONty
*)_V2
)->_1
->tag__
) {
446 case a_TyCon::tag_DATATYPEtycon
: {
448 Functor f
= Fmap
.type_map
.value(E
) + cons
->tag
+
449 (cons
->ty
== NOty
? 0 : ((TyCon_DATATYPEtycon
*)((Ty_TYCONty
*)_V2
)->_1
)->unit
);
450 return CONSterm(f
,cons
,n
,make_term(pats
));
457 bug("TRS::make_term"); return PATterm(pat
);
463 default: { goto L9
; } break;
472 ///////////////////////////////////////////////////////////////////////////////
474 // Method to create the lhs for rule r
476 ///////////////////////////////////////////////////////////////////////////////
477 Term
* TRS::make_term(Pats pats
)
478 { int n
= length(pats
);
479 Term
* args
= (Term
*)mem_pool
.m_alloc(sizeof(Term
) * n
);
481 for_each(Pat
, p
, pats
) args
[i
++] = make_term(p
);
485 ///////////////////////////////////////////////////////////////////////////////
487 // Return the type of an expression
489 ///////////////////////////////////////////////////////////////////////////////
490 Ty
type_of(Exp e
) { return type_of(e
,Env()); }
492 ///////////////////////////////////////////////////////////////////////////////
494 // Method to create the rhs for rule r
496 ///////////////////////////////////////////////////////////////////////////////
497 void TRS::make_rhs(Rule r
, Exp exp
)
499 rhs_map
[r
] = make_term(exp
);
502 ///////////////////////////////////////////////////////////////////////////////
504 // Method to create the rhs
506 ///////////////////////////////////////////////////////////////////////////////
507 Term
TRS::make_term(Exp exp
)
515 switch (exp
->tag__
) {
516 case a_Exp::tag_LITERALexp
: {
519 Fmap
.literal_map
.contains(((Exp_LITERALexp
*)exp
)->LITERALexp
)
524 return CONSterm(Fmap
.literal_map
[((Exp_LITERALexp
*)exp
)->LITERALexp
],0,0,0);
537 return make_term_var(NOpat
,exp
);
543 return CODEterm(exp
);
548 case a_Exp::tag_IDexp
: {
550 return make_term(exp
,((Exp_IDexp
*)exp
)->IDexp
,
559 case a_Exp::tag_APPexp
: {
566 if (((Exp_APPexp
*)exp
)->_1
) {
567 switch (((Exp_APPexp
*)exp
)->_1
->tag__
) {
568 case a_Exp::tag_IDexp
: {
570 if (((Exp_APPexp
*)exp
)->_2
) {
571 switch (((Exp_APPexp
*)exp
)->_2
->tag__
) {
572 case a_Exp::tag_TUPLEexp
: {
575 law_exp
= DatatypeCompiler::lookup_law(((Exp_IDexp
*)((Exp_APPexp
*)exp
)->_1
)->IDexp
,((Exp_TUPLEexp
*)((Exp_APPexp
*)exp
)->_2
)->TUPLEexp
);
576 if (law_exp
== NOexp
) return make_term(exp
,((Exp_IDexp
*)((Exp_APPexp
*)exp
)->_1
)->IDexp
,((Exp_TUPLEexp
*)((Exp_APPexp
*)exp
)->_2
)->TUPLEexp
);
585 law_exp
= DatatypeCompiler::lookup_law(((Exp_IDexp
*)((Exp_APPexp
*)exp
)->_1
)->IDexp
,
588 list_1_(((Exp_APPexp
*)exp
)->_2
)
592 if (law_exp
== NOexp
) return make_term(exp
,((Exp_IDexp
*)((Exp_APPexp
*)exp
)->_1
)->IDexp
,
595 list_1_(((Exp_APPexp
*)exp
)->_2
)
607 default: { goto L12
; } break;
612 if (((Exp_APPexp
*)exp
)->_1
) {
613 switch (((Exp_APPexp
*)exp
)->_1
->tag__
) {
614 case a_Exp::tag_IDexp
: { goto L14
; } break;
615 default: { goto L13
; } break;
620 case a_Exp::tag_LISTexp
: {
627 if (((Exp_LISTexp
*)exp
)->_3
) {
629 if (((Exp_LISTexp
*)exp
)->_3
->_2
) {
632 Exp e2
= LISTexp(((Exp_LISTexp
*)exp
)->_1
, ((Exp_LISTexp
*)exp
)->_2
, ((Exp_LISTexp
*)exp
)->_3
->_2
, ((Exp_LISTexp
*)exp
)->_4
);
634 return make_term(exp
,((Exp_LISTexp
*)exp
)->_1
->name
,
637 list_1_(((Exp_LISTexp
*)exp
)->_3
->_1
,list_1_(e2
))
646 Exp nil_exp
= ((Exp_LISTexp
*)exp
)->_4
== NOexp
? CONSexp(((Exp_LISTexp
*)exp
)->_2
,
652 ,NOexp
) : ((Exp_LISTexp
*)exp
)->_4
;
653 nil_exp
->ty
= exp
->ty
;
654 return make_term(exp
,((Exp_LISTexp
*)exp
)->_1
->name
,
657 list_1_(((Exp_LISTexp
*)exp
)->_3
->_1
,list_1_(nil_exp
))
667 if (((Exp_LISTexp
*)exp
)->_3
) { goto L17
; } else { goto L13
; }
670 case a_Exp::tag_CONSexp
: {
677 if (((Exp_CONSexp
*)exp
)->_3
) {
678 switch (((Exp_CONSexp
*)exp
)->_3
->tag__
) {
679 case a_Exp::tag_TUPLEexp
: {
680 if (((Exp_CONSexp
*)exp
)->_2
) { goto L12
; } else {
683 return make_term(exp
,((Exp_CONSexp
*)exp
)->_1
->name
,((Exp_TUPLEexp
*)((Exp_CONSexp
*)exp
)->_3
)->TUPLEexp
);
688 if (((Exp_CONSexp
*)exp
)->_2
) { goto L12
; } else {
691 return make_term(exp
,((Exp_CONSexp
*)exp
)->_1
->name
,
694 list_1_(((Exp_CONSexp
*)exp
)->_3
)
703 if (((Exp_CONSexp
*)exp
)->_2
) { goto L12
; } else {
706 return make_term(exp
,((Exp_CONSexp
*)exp
)->_1
->name
,
718 if (((Exp_CONSexp
*)exp
)->_3
) {
719 switch (((Exp_CONSexp
*)exp
)->_3
->tag__
) {
720 case a_Exp::tag_TUPLEexp
: {
721 if (((Exp_CONSexp
*)exp
)->_2
) { goto L13
; } else { goto L20
; }
724 if (((Exp_CONSexp
*)exp
)->_2
) { goto L13
; } else { goto L21
; }
728 if (((Exp_CONSexp
*)exp
)->_2
) { goto L13
; } else { goto L22
; }
732 case a_Exp::tag_MARKEDexp
: {
734 exp
= ((Exp_MARKEDexp
*)exp
)->_2
;
737 default: { goto L11
; } break;
747 ///////////////////////////////////////////////////////////////////////////////
749 // Method to create the rhs
751 ///////////////////////////////////////////////////////////////////////////////
752 Term
TRS::make_term(Exp exp
, Id id
, Exps exps
)
753 { int n
= length(exps
);
754 Cons cons
= find_cons(id
);
755 if (cons
== NOcons
) { return CODEterm(exp
); }
756 FunctorTable::Entry
* E
= Fmap
.type_map
.lookup(exp
->ty
);
757 if (E
== 0) { return CODEterm(exp
); }
762 Ty _V3
= cons
->alg_ty
;
764 switch (_V3
->tag__
) {
765 case a_Ty::tag_TYCONty
: {
766 if (boxed(((Ty_TYCONty
*)_V3
)->_1
)) {
767 switch (((Ty_TYCONty
*)_V3
)->_1
->tag__
) {
768 case a_TyCon::tag_DATATYPEtycon
: {
770 Functor f
= Fmap
.type_map
.value(E
) + cons
->tag
+
771 (cons
->ty
== NOty
? 0 : ((TyCon_DATATYPEtycon
*)((Ty_TYCONty
*)_V3
)->_1
)->unit
);
772 return CONSterm(f
,cons
,n
,make_term(exps
));
779 bug("TRS::make_term"); return CODEterm(exp
);
785 default: { goto L23
; } break;
794 ///////////////////////////////////////////////////////////////////////////////
796 // Method to create the term variable
798 ///////////////////////////////////////////////////////////////////////////////
799 Term
TRS::make_term_var(Pat pat
, Exp exp
)
800 { for (int i
= 0; i
< num_vars
; i
++)
801 { if (equal(exp
,vars
[i
])) return VARterm(i
,var_pats
[i
],vars
[i
]); }
802 if (pat
== NOpat
) { return CODEterm(exp
); }
803 if (num_vars
== MAX_VARS
)
804 bug("%LTRS::make_term_var() too many variables");
805 vars
[num_vars
] = exp
;
806 var_pats
[num_vars
] = pat
;
807 return VARterm(num_vars
++,pat
,exp
);
810 ///////////////////////////////////////////////////////////////////////////////
812 // Method to create the term from a list of expressions
814 ///////////////////////////////////////////////////////////////////////////////
815 Term
* TRS::make_term(Exps exps
)
816 { int n
= length(exps
);
817 Term
* args
= (Term
*)mem_pool
.m_alloc(sizeof(Term
) * n
);
819 for_each(Exp
, e
, exps
) args
[i
++] = make_term(e
);
823 /////////////////////////////////////////////////////////////////////////////// //
825 // Method to print a term
827 ///////////////////////////////////////////////////////////////////////////////
828 void TRS::print (std::ostream
& s
, Term term
) const
833 switch (term
->tag__
) {
834 case a_Term::tag_CONSterm
: {
835 switch (((Term_CONSterm
*)term
)->_3
) {
839 is_list_constructor(((Term_CONSterm
*)term
)->_2
)
844 s
<< '#' << ((Term_CONSterm
*)term
)->_2
->name
[1];
853 case a_Term::tag_CONSterm
: {
854 switch (((Term_CONSterm
*)t
)->_3
) {
858 (((Term_CONSterm
*)t
)->_1
== ((Term_CONSterm
*)term
)->_1
)
863 s
<< comma
; print(s
,((Term_CONSterm
*)t
)->_4
[0]); t
= ((Term_CONSterm
*)t
)->_4
[1]; comma
= ", ";
868 default: { goto L24
; }
871 default: { goto L24
; } break;
884 case a_Term::tag_CONSterm
: {
885 switch (((Term_CONSterm
*)t
)->_3
) {
889 (((Term_CONSterm
*)term
)->_2
&& is_list_constructor(((Term_CONSterm
*)t
)->_2
))
896 s
<< comma
; print(s
,t
);
900 default: { goto L25
; }
903 default: { goto L25
; } break;
909 s
<< ((Term_CONSterm
*)term
)->_2
->name
[5];
916 s
<< (((Term_CONSterm
*)term
)->_2
!= NOcons
? ((Term_CONSterm
*)term
)->_2
->name
: Fmap
.G
.functor_name(((Term_CONSterm
*)term
)->_1
));
917 if (((Term_CONSterm
*)term
)->_3
> 0)
919 for (int i
= 0; i
< ((Term_CONSterm
*)term
)->_3
; i
++)
920 { print(s
,((Term_CONSterm
*)term
)->_4
[i
]);
921 if (i
< ((Term_CONSterm
*)term
)->_3
- 1) s
<< ", ";
929 default: { goto L26
; }
932 case a_Term::tag_VARterm
: {
934 s
<< ((Term_VARterm
*)term
)->_2
;
937 case a_Term::tag_CODEterm
: {
939 Bool save
= pretty_print_exp
;
940 pretty_print_exp
= true;
941 s
<< "[[" << ((Term_CODEterm
*)term
)->CODEterm
<< "]]";
942 pretty_print_exp
= save
;
946 case a_Term::tag_OPAQUEterm
: {
953 s
<< "[[" << ((Term_PATterm
*)term
)->PATterm
<< "]]";
963 ///////////////////////////////////////////////////////////////////////////////
965 // Method to print a TRS
967 ///////////////////////////////////////////////////////////////////////////////
968 void TRS::print (std::ostream
& s
) const
969 { s
<< "\n\nBottom-up term rewriting system:\n";
970 Bool save
= pretty_print_exp
;
971 pretty_print_exp
= true;
972 for (Rule r
= 0; r
< number_of_rules
; r
++)
973 { MatchRule rule
= rule_map
[r
];
974 s
<< "line " << rule
->begin_line
<< ": ";
976 if (guard_map
[r
] != NOexp
) s
<< " | " << guard_map
[r
];
977 s
<< ":\n\t"; print(s
,rhs_map
[r
]); s
<< '\n';
978 for (int i
= 0; i
< num_var_map
[r
]; i
++)
979 { Pat pat_var
= var_pat_map
[r
][i
];
980 Ty pat_ty
= pat_var
->ty
;
981 s
<< "\tvariable " << pat_var
<< " : " << pat_ty
;
982 if (! compiler
.has_index(pat_ty
)) s
<< " has no index";
988 pretty_print_exp
= save
;
992 ------------------------------- Statistics -------------------------------
993 Merge matching rules = yes
994 Number of DFA nodes merged = 903
995 Number of ifs generated = 43
996 Number of switches generated = 25
997 Number of labels = 22
999 Adaptive matching = enabled
1000 Fast string matching = disabled
1001 Inline downcasts = enabled
1002 --------------------------------------------------------------------------