14 string -> string list -> thm list -> tactic option -> theory -> theory |
14 string -> string list -> thm list -> tactic option -> theory -> theory |
15 val add_typedef_i: string -> bstring * string list * mixfix -> |
15 val add_typedef_i: string -> bstring * string list * mixfix -> |
16 term -> string list -> thm list -> tactic option -> theory -> theory |
16 term -> string list -> thm list -> tactic option -> theory -> theory |
17 val add_typedef_i_no_def: string -> bstring * string list * mixfix -> |
17 val add_typedef_i_no_def: string -> bstring * string list * mixfix -> |
18 term -> string list -> thm list -> tactic option -> theory -> theory |
18 term -> string list -> thm list -> tactic option -> theory -> theory |
19 val typedef_proof: (string * (bstring * string list * mixfix) * string) * Comment.text |
19 val typedef_proof: |
|
20 (string * (bstring * string list * mixfix) * string * (string * string) option) * Comment.text |
20 -> bool -> theory -> ProofHistory.T |
21 -> bool -> theory -> ProofHistory.T |
21 val typedef_proof_i: (string * (bstring * string list * mixfix) * term) * Comment.text |
22 val typedef_proof_i: |
|
23 (string * (bstring * string list * mixfix) * term * (string * string) option) * Comment.text |
22 -> bool -> theory -> ProofHistory.T |
24 -> bool -> theory -> ProofHistory.T |
23 end; |
25 end; |
24 |
26 |
25 structure TypedefPackage: TYPEDEF_PACKAGE = |
27 structure TypedefPackage: TYPEDEF_PACKAGE = |
26 struct |
28 struct |
94 fun cert_term sg _ t = Thm.cterm_of sg t handle TERM (msg, _) => error msg; |
96 fun cert_term sg _ t = Thm.cterm_of sg t handle TERM (msg, _) => error msg; |
95 |
97 |
96 fun err_in_typedef name = |
98 fun err_in_typedef name = |
97 error ("The error(s) above occurred in typedef " ^ quote name); |
99 error ("The error(s) above occurred in typedef " ^ quote name); |
98 |
100 |
99 fun prepare_typedef prep_term no_def name (t, vs, mx) raw_set thy = |
101 fun prepare_typedef prep_term no_def name (t, vs, mx) raw_set opt_morphs thy = |
100 let |
102 let |
101 val _ = Theory.requires thy "Typedef" "typedefs"; |
103 val _ = Theory.requires thy "Typedef" "typedefs"; |
102 val sign = Theory.sign_of thy; |
104 val sign = Theory.sign_of thy; |
103 val full = Sign.full_name sign; |
105 val full = Sign.full_name sign; |
104 |
106 |
110 val oldT = HOLogic.dest_setT setT handle TYPE _ => |
112 val oldT = HOLogic.dest_setT setT handle TYPE _ => |
111 error ("Not a set type: " ^ quote (Sign.string_of_typ sign setT)); |
113 error ("Not a set type: " ^ quote (Sign.string_of_typ sign setT)); |
112 fun mk_nonempty A = |
114 fun mk_nonempty A = |
113 HOLogic.mk_Trueprop (HOLogic.mk_exists ("x", oldT, HOLogic.mk_mem (Free ("x", oldT), A))); |
115 HOLogic.mk_Trueprop (HOLogic.mk_exists ("x", oldT, HOLogic.mk_mem (Free ("x", oldT), A))); |
114 val goal = mk_nonempty set; |
116 val goal = mk_nonempty set; |
115 val goal_pat = mk_nonempty (Var ((name, 0), setT)); |
117 val vname = take_suffix Symbol.is_digit (Symbol.explode name) |
|
118 |> apfst implode |> apsnd (#1 o Term.read_int); |
|
119 val goal_pat = mk_nonempty (Var (vname, setT)); |
116 |
120 |
117 (*lhs*) |
121 (*lhs*) |
118 val lhs_tfrees = map (fn v => (v, if_none (assoc (rhs_tfrees, v)) HOLogic.termS)) vs; |
122 val lhs_tfrees = map (fn v => (v, if_none (assoc (rhs_tfrees, v)) HOLogic.termS)) vs; |
119 val tname = Syntax.type_name t mx; |
123 val tname = Syntax.type_name t mx; |
120 val full_tname = full tname; |
124 val full_tname = full tname; |
121 val newT = Type (full_tname, map TFree lhs_tfrees); |
125 val newT = Type (full_tname, map TFree lhs_tfrees); |
122 |
126 |
123 val Rep_name = "Rep_" ^ name; |
127 val (Rep_name, Abs_name) = if_none opt_morphs ("Rep_" ^ name, "Abs_" ^ name); |
124 val Abs_name = "Abs_" ^ name; |
|
125 |
|
126 val setC = Const (full_name, setT); |
128 val setC = Const (full_name, setT); |
127 val RepC = Const (full Rep_name, newT --> oldT); |
129 val RepC = Const (full Rep_name, newT --> oldT); |
128 val AbsC = Const (full Abs_name, oldT --> newT); |
130 val AbsC = Const (full Abs_name, oldT --> newT); |
129 val x_new = Free ("x", newT); |
131 val x_new = Free ("x", newT); |
130 val y_old = Free ("y", oldT); |
132 val y_old = Free ("y", oldT); |
202 |
204 |
203 (* add_typedef interfaces *) |
205 (* add_typedef interfaces *) |
204 |
206 |
205 fun gen_add_typedef prep_term no_def name typ set names thms tac thy = |
207 fun gen_add_typedef prep_term no_def name typ set names thms tac thy = |
206 let |
208 let |
207 val (cset, goal, _, typedef_att) = prepare_typedef prep_term no_def name typ set thy; |
209 val (cset, goal, _, typedef_att) = prepare_typedef prep_term no_def name typ set None thy; |
208 val result = prove_nonempty thy cset goal (names, thms, tac); |
210 val result = prove_nonempty thy cset goal (names, thms, tac); |
209 in (thy, result) |> typedef_att |> #1 end; |
211 in (thy, result) |> typedef_att |> #1 end; |
210 |
212 |
211 val add_typedef = gen_add_typedef read_term false; |
213 val add_typedef = gen_add_typedef read_term false; |
212 val add_typedef_i = gen_add_typedef cert_term false; |
214 val add_typedef_i = gen_add_typedef cert_term false; |
213 val add_typedef_i_no_def = gen_add_typedef cert_term true; |
215 val add_typedef_i_no_def = gen_add_typedef cert_term true; |
214 |
216 |
215 |
217 |
216 (* typedef_proof interface *) |
218 (* typedef_proof interface *) |
217 |
219 |
218 fun gen_typedef_proof prep_term ((name, typ, set), comment) int thy = |
220 fun gen_typedef_proof prep_term ((name, typ, set, opt_morphs), comment) int thy = |
219 let val (_, goal, goal_pat, att) = prepare_typedef prep_term false name typ set thy in |
221 let val (_, goal, goal_pat, att) = prepare_typedef prep_term false name typ set opt_morphs thy in |
220 thy |
222 thy |> |
221 |> IsarThy.theorem_i Drule.internalK ((("", [att]), (goal, ([goal_pat], []))), comment) int |
223 IsarThy.theorem_i Drule.internalK ((("", [att]), (goal, ([goal_pat], []))), comment) int |
222 end; |
224 end; |
223 |
225 |
224 val typedef_proof = gen_typedef_proof read_term; |
226 val typedef_proof = gen_typedef_proof read_term; |
225 val typedef_proof_i = gen_typedef_proof cert_term; |
227 val typedef_proof_i = gen_typedef_proof cert_term; |
226 |
228 |
236 Toplevel.theory (add_typedecls [(t, vs, mx)]))); |
238 Toplevel.theory (add_typedecls [(t, vs, mx)]))); |
237 |
239 |
238 |
240 |
239 val typedef_proof_decl = |
241 val typedef_proof_decl = |
240 Scan.option (P.$$$ "(" |-- P.name --| P.$$$ ")") -- |
242 Scan.option (P.$$$ "(" |-- P.name --| P.$$$ ")") -- |
241 (P.type_args -- P.name) -- P.opt_infix -- (P.$$$ "=" |-- P.term) -- P.marg_comment; |
243 (P.type_args -- P.name) -- P.opt_infix -- (P.$$$ "=" |-- P.term) -- |
242 |
244 Scan.option (P.$$$ "morphisms" |-- P.!!! (P.name -- P.name)) -- |
243 fun mk_typedef_proof ((((opt_name, (vs, t)), mx), A), comment) = |
245 P.marg_comment; |
244 typedef_proof ((if_none opt_name (Syntax.type_name t mx), (t, vs, mx), A), comment); |
246 |
|
247 fun mk_typedef_proof (((((opt_name, (vs, t)), mx), A), morphs), comment) = |
|
248 typedef_proof ((if_none opt_name (Syntax.type_name t mx), (t, vs, mx), A, morphs), comment); |
245 |
249 |
246 val typedefP = |
250 val typedefP = |
247 OuterSyntax.command "typedef" "HOL type definition (requires non-emptiness proof)" K.thy_goal |
251 OuterSyntax.command "typedef" "HOL type definition (requires non-emptiness proof)" K.thy_goal |
248 (typedef_proof_decl >> (Toplevel.print oo (Toplevel.theory_to_proof o mk_typedef_proof))); |
252 (typedef_proof_decl >> (Toplevel.print oo (Toplevel.theory_to_proof o mk_typedef_proof))); |
249 |
253 |
250 |
254 |
|
255 val _ = OuterSyntax.add_keywords ["morphisms"]; |
251 val _ = OuterSyntax.add_parsers [typedeclP, typedefP]; |
256 val _ = OuterSyntax.add_parsers [typedeclP, typedefP]; |
252 |
257 |
253 end; |
258 end; |
254 |
259 |
255 |
260 |