69 open Logic Ind_Syntax; |
69 open Logic Ind_Syntax; |
70 |
70 |
71 (*internal version*) |
71 (*internal version*) |
72 fun add_fp_def_i (rec_tms, dom_sum, intr_tms) thy = |
72 fun add_fp_def_i (rec_tms, dom_sum, intr_tms) thy = |
73 let |
73 let |
|
74 val dummy = (*has essential ancestors?*) |
|
75 require_thy thy "Inductive" "(co)inductive definitions" |
|
76 |
74 val sign = sign_of thy; |
77 val sign = sign_of thy; |
75 |
78 |
76 (*recT and rec_params should agree for all mutually recursive components*) |
79 (*recT and rec_params should agree for all mutually recursive components*) |
77 val rec_hds = map head_of rec_tms; |
80 val rec_hds = map head_of rec_tms; |
78 |
81 |
79 val _ = assert_all is_Const rec_hds |
82 val dummy = assert_all is_Const rec_hds |
80 (fn t => "Recursive set not previously declared as constant: " ^ |
83 (fn t => "Recursive set not previously declared as constant: " ^ |
81 Sign.string_of_term sign t); |
84 Sign.string_of_term sign t); |
82 |
85 |
83 (*Now we know they are all Consts, so get their names, type and params*) |
86 (*Now we know they are all Consts, so get their names, type and params*) |
84 val rec_names = map (#1 o dest_Const) rec_hds |
87 val rec_names = map (#1 o dest_Const) rec_hds |
85 and (Const(_,recT),rec_params) = strip_comb (hd rec_tms); |
88 and (Const(_,recT),rec_params) = strip_comb (hd rec_tms); |
86 |
89 |
87 val _ = assert_all Syntax.is_identifier rec_names |
90 val dummy = assert_all Syntax.is_identifier rec_names |
88 (fn a => "Name of recursive set not an identifier: " ^ a); |
91 (fn a => "Name of recursive set not an identifier: " ^ a); |
89 |
92 |
90 local (*Checking the introduction rules*) |
93 local (*Checking the introduction rules*) |
91 val intr_sets = map (#2 o rule_concl_msg sign) intr_tms; |
94 val intr_sets = map (#2 o rule_concl_msg sign) intr_tms; |
92 fun intr_ok set = |
95 fun intr_ok set = |
93 case head_of set of Const(a,recT) => a mem rec_names | _ => false; |
96 case head_of set of Const(a,recT) => a mem rec_names | _ => false; |
94 in |
97 in |
95 val _ = assert_all intr_ok intr_sets |
98 val dummy = assert_all intr_ok intr_sets |
96 (fn t => "Conclusion of rule does not name a recursive set: " ^ |
99 (fn t => "Conclusion of rule does not name a recursive set: " ^ |
97 Sign.string_of_term sign t); |
100 Sign.string_of_term sign t); |
98 end; |
101 end; |
99 |
102 |
100 val _ = assert_all is_Free rec_params |
103 val dummy = assert_all is_Free rec_params |
101 (fn t => "Param in recursion term not a free variable: " ^ |
104 (fn t => "Param in recursion term not a free variable: " ^ |
102 Sign.string_of_term sign t); |
105 Sign.string_of_term sign t); |
103 |
106 |
104 (*** Construct the lfp definition ***) |
107 (*** Construct the lfp definition ***) |
105 val mk_variant = variant (foldr add_term_names (intr_tms,[])); |
108 val mk_variant = variant (foldr add_term_names (intr_tms,[])); |
111 Sign.string_of_term sign Q); |
114 Sign.string_of_term sign Q); |
112 |
115 |
113 (*Makes a disjunct from an introduction rule*) |
116 (*Makes a disjunct from an introduction rule*) |
114 fun lfp_part intr = (*quantify over rule's free vars except parameters*) |
117 fun lfp_part intr = (*quantify over rule's free vars except parameters*) |
115 let val prems = map dest_tprop (strip_imp_prems intr) |
118 let val prems = map dest_tprop (strip_imp_prems intr) |
116 val _ = seq (fn rec_hd => seq (chk_prem rec_hd) prems) rec_hds |
119 val dummy = seq (fn rec_hd => seq (chk_prem rec_hd) prems) rec_hds |
117 val exfrees = term_frees intr \\ rec_params |
120 val exfrees = term_frees intr \\ rec_params |
118 val zeq = eq_const $ (Free(z',iT)) $ (#1 (rule_concl intr)) |
121 val zeq = eq_const $ (Free(z',iT)) $ (#1 (rule_concl intr)) |
119 in foldr mk_exists (exfrees, fold_bal (app conj) (zeq::prems)) end; |
122 in foldr mk_exists (exfrees, fold_bal (app conj) (zeq::prems)) end; |
120 |
123 |
121 (*The Part(A,h) terms -- compose injections to make h*) |
124 (*The Part(A,h) terms -- compose injections to make h*) |
133 val lfp_abs = absfree(X', iT, |
136 val lfp_abs = absfree(X', iT, |
134 mk_Collect(z', dom_sum, fold_bal (app disj) part_intrs)); |
137 mk_Collect(z', dom_sum, fold_bal (app disj) part_intrs)); |
135 |
138 |
136 val lfp_rhs = Fp.oper $ dom_sum $ lfp_abs |
139 val lfp_rhs = Fp.oper $ dom_sum $ lfp_abs |
137 |
140 |
138 val _ = seq (fn rec_hd => deny (rec_hd occs lfp_rhs) |
141 val dummy = seq (fn rec_hd => deny (rec_hd occs lfp_rhs) |
139 "Illegal occurrence of recursion operator") |
142 "Illegal occurrence of recursion operator") |
140 rec_hds; |
143 rec_hds; |
141 |
144 |
142 (*** Make the new theory ***) |
145 (*** Make the new theory ***) |
143 |
146 |
155 (case parts of |
158 (case parts of |
156 [_] => [] (*no mutual recursion*) |
159 [_] => [] (*no mutual recursion*) |
157 | _ => rec_tms ~~ (*define the sets as Parts*) |
160 | _ => rec_tms ~~ (*define the sets as Parts*) |
158 map (subst_atomic [(Free(X',iT),big_rec_tm)]) parts)); |
161 map (subst_atomic [(Free(X',iT),big_rec_tm)]) parts)); |
159 |
162 |
160 val _ = seq (writeln o Sign.string_of_term sign o #2) axpairs |
163 val dummy = seq (writeln o Sign.string_of_term sign o #2) axpairs |
161 |
164 |
162 in thy |> add_defs_i axpairs end |
165 in thy |> add_defs_i axpairs end |
163 |
166 |
164 |
167 |
165 (*Expects the recursive sets to have been defined already. |
168 (*Expects the recursive sets to have been defined already. |
166 con_ty_lists specifies the constructors in the form (name,prems,mixfix) *) |
169 con_ty_lists specifies the constructors in the form (name,prems,mixfix) *) |
167 fun add_constructs_def (rec_names, con_ty_lists) thy = |
170 fun add_constructs_def (rec_names, con_ty_lists) thy = |
168 let |
171 let |
169 val _ = writeln" Defining the constructor functions..."; |
172 val dummy = (*has essential ancestors?*) |
|
173 require_thy thy "Datatype" "(co)datatype definitions" |
|
174 |
|
175 val dummy = writeln" Defining the constructor functions..."; |
170 val case_name = "f"; (*name for case variables*) |
176 val case_name = "f"; (*name for case variables*) |
171 |
177 |
172 (** Define the constructors **) |
178 (** Define the constructors **) |
173 |
179 |
174 (*The empty tuple is 0*) |
180 (*The empty tuple is 0*) |