6 Package for defining functions on datatypes by primitive recursion. |
6 Package for defining functions on datatypes by primitive recursion. |
7 *) |
7 *) |
8 |
8 |
9 signature PRIMREC_PACKAGE = |
9 signature PRIMREC_PACKAGE = |
10 sig |
10 sig |
11 val primrec: ((binding * string) * Token.src list) list -> theory -> theory * thm list |
11 val primrec: ((binding * string) * Token.src list) list -> theory -> thm list * theory |
12 val primrec_i: ((binding * term) * attribute list) list -> theory -> theory * thm list |
12 val primrec_i: ((binding * term) * attribute list) list -> theory -> thm list * theory |
13 end; |
13 end; |
14 |
14 |
15 structure PrimrecPackage : PRIMREC_PACKAGE = |
15 structure PrimrecPackage : PRIMREC_PACKAGE = |
16 struct |
16 struct |
17 |
17 |
171 val ([def_thm], thy1) = thy |
171 val ([def_thm], thy1) = thy |
172 |> Sign.add_path (Long_Name.base_name fname) |
172 |> Sign.add_path (Long_Name.base_name fname) |
173 |> Global_Theory.add_defs false [Thm.no_attributes (apfst Binding.name def)]; |
173 |> Global_Theory.add_defs false [Thm.no_attributes (apfst Binding.name def)]; |
174 |
174 |
175 val rewrites = def_thm :: map (mk_meta_eq o Thm.transfer thy1) (#rec_rewrites con_info) |
175 val rewrites = def_thm :: map (mk_meta_eq o Thm.transfer thy1) (#rec_rewrites con_info) |
176 val eqn_thms = |
176 val eqn_thms0 = |
177 eqn_terms |> map (fn t => |
177 eqn_terms |> map (fn t => |
178 Goal.prove_global thy1 [] [] (Ind_Syntax.traceIt "next primrec equation = " thy1 t) |
178 Goal.prove_global thy1 [] [] (Ind_Syntax.traceIt "next primrec equation = " thy1 t) |
179 (fn {context = ctxt, ...} => |
179 (fn {context = ctxt, ...} => |
180 EVERY [rewrite_goals_tac ctxt rewrites, resolve_tac ctxt @{thms refl} 1])); |
180 EVERY [rewrite_goals_tac ctxt rewrites, resolve_tac ctxt @{thms refl} 1])); |
181 |
181 in |
182 val (eqn_thms', thy2) = |
182 thy1 |
183 thy1 |
183 |> Global_Theory.add_thms ((eqn_names ~~ eqn_thms0) ~~ eqn_atts) |
184 |> Global_Theory.add_thms ((eqn_names ~~ eqn_thms) ~~ eqn_atts); |
184 |-> (fn eqn_thms => |
185 val (_, thy3) = |
185 Global_Theory.add_thmss [((Binding.name "simps", eqn_thms), [Simplifier.simp_add])]) |
186 thy2 |
186 |>> the_single |
187 |> Global_Theory.add_thmss [((Binding.name "simps", eqn_thms'), [Simplifier.simp_add])] |
187 ||> Sign.parent_path |
188 ||> Sign.parent_path; |
188 end; |
189 in (thy3, eqn_thms') end; |
|
190 |
189 |
191 fun primrec args thy = |
190 fun primrec args thy = |
192 primrec_i (map (fn ((name, s), srcs) => |
191 primrec_i (map (fn ((name, s), srcs) => |
193 ((name, Syntax.read_prop_global thy s), map (Attrib.attribute_cmd_global thy) srcs)) |
192 ((name, Syntax.read_prop_global thy s), map (Attrib.attribute_cmd_global thy) srcs)) |
194 args) thy; |
193 args) thy; |
197 (* outer syntax *) |
196 (* outer syntax *) |
198 |
197 |
199 val _ = |
198 val _ = |
200 Outer_Syntax.command \<^command_keyword>\<open>primrec\<close> "define primitive recursive functions on datatypes" |
199 Outer_Syntax.command \<^command_keyword>\<open>primrec\<close> "define primitive recursive functions on datatypes" |
201 (Scan.repeat1 (Parse_Spec.opt_thm_name ":" -- Parse.prop) |
200 (Scan.repeat1 (Parse_Spec.opt_thm_name ":" -- Parse.prop) |
202 >> (Toplevel.theory o (#1 oo (primrec o map (fn ((x, y), z) => ((x, z), y)))))); |
201 >> (Toplevel.theory o (#2 oo (primrec o map (fn ((x, y), z) => ((x, z), y)))))); |
203 |
202 |
204 end; |
203 end; |
205 |
204 |