1 (* Title: HOLCF/Tools/pcpodef_package.ML
5 Primitive domain definitions for HOLCF, similar to Gordon/HOL-style
9 signature PCPODEF_PACKAGE =
11 val quiet_mode: bool ref
12 val pcpodef_proof: (bool * string) * (bstring * string list * mixfix) * string
13 * (string * string) option -> theory -> Proof.state
14 val pcpodef_proof_i: (bool * string) * (bstring * string list * mixfix) * term
15 * (string * string) option -> theory -> Proof.state
16 val cpodef_proof: (bool * string) * (bstring * string list * mixfix) * string
17 * (string * string) option -> theory -> Proof.state
18 val cpodef_proof_i: (bool * string) * (bstring * string list * mixfix) * term
19 * (string * string) option -> theory -> Proof.state
22 structure PcpodefPackage: PCPODEF_PACKAGE =
25 (** theory context references **)
27 val typedef_po = thm "typedef_po";
28 val typedef_cpo = thm "typedef_cpo";
29 val typedef_pcpo = thm "typedef_pcpo";
30 val typedef_lub = thm "typedef_lub";
31 val typedef_thelub = thm "typedef_thelub";
32 val typedef_compact = thm "typedef_compact";
33 val cont_Rep = thm "typedef_cont_Rep";
34 val cont_Abs = thm "typedef_cont_Abs";
35 val Rep_strict = thm "typedef_Rep_strict";
36 val Abs_strict = thm "typedef_Abs_strict";
37 val Rep_defined = thm "typedef_Rep_defined";
38 val Abs_defined = thm "typedef_Abs_defined";
41 (** type definitions **)
45 val quiet_mode = ref false;
46 fun message s = if ! quiet_mode then () else writeln s;
51 fun err_in_cpodef msg name =
52 cat_error msg ("The error(s) above occurred in cpodef " ^ quote name);
54 fun declare_type_name a = Variable.declare_constraints (Logic.mk_type (TFree (a, dummyS)));
56 fun adm_const T = Const ("Adm.adm", (T --> HOLogic.boolT) --> HOLogic.boolT);
57 fun mk_adm (x, T, P) = adm_const T $ absfree (x, T, P);
59 fun prepare_pcpodef prep_term pcpo def name (t, vs, mx) raw_set opt_morphs thy =
61 val ctxt = ProofContext.init thy;
62 val full = Sign.full_name thy;
65 val full_name = full name;
66 val set = prep_term (ctxt |> fold declare_type_name vs) raw_set;
67 val setT = Term.fastype_of set;
68 val rhs_tfrees = term_tfrees set;
69 val oldT = HOLogic.dest_setT setT handle TYPE _ =>
70 error ("Not a set type: " ^ quote (Syntax.string_of_typ ctxt setT));
72 HOLogic.mk_exists ("x", oldT, HOLogic.mk_mem (Free ("x", oldT), A));
74 mk_adm ("x", oldT, HOLogic.mk_mem (Free ("x", oldT), A));
75 fun mk_UU_mem A = HOLogic.mk_mem (Const ("Pcpo.UU", oldT), A);
77 then HOLogic.mk_Trueprop (HOLogic.mk_conj (mk_UU_mem set, mk_admissible set))
78 else HOLogic.mk_Trueprop (HOLogic.mk_conj (mk_nonempty set, mk_admissible set));
81 val defS = Sign.defaultS thy;
82 val lhs_tfrees = map (fn v => (v, the_default defS (AList.lookup (op =) rhs_tfrees v))) vs;
83 val lhs_sorts = map snd lhs_tfrees;
84 val tname = Syntax.type_name t mx;
85 val full_tname = full tname;
86 val newT = Type (full_tname, map TFree lhs_tfrees);
88 val (Rep_name, Abs_name) = the_default ("Rep_" ^ name, "Abs_" ^ name) opt_morphs;
89 val RepC = Const (full Rep_name, newT --> oldT);
90 fun lessC T = Const (@{const_name Porder.sq_le}, T --> T --> HOLogic.boolT);
91 val less_def = ("less_" ^ name ^ "_def", Logic.mk_equals (lessC newT,
92 Abs ("x", newT, Abs ("y", newT, lessC oldT $ (RepC $ Bound 1) $ (RepC $ Bound 0)))));
94 fun make_po tac theory = theory
95 |> TypedefPackage.add_typedef_i def (SOME name) (t, vs, mx) set opt_morphs tac
96 ||> AxClass.prove_arity (full_tname, lhs_sorts, ["Porder.sq_ord"])
97 (Class.intro_classes_tac [])
98 ||>> PureThy.add_defs_i true [Thm.no_attributes less_def]
99 |-> (fn ((_, {type_definition, set_def, ...}), [less_definition]) =>
100 AxClass.prove_arity (full_tname, lhs_sorts, ["Porder.po"])
101 (Tactic.rtac (typedef_po OF [type_definition, less_definition]) 1)
102 #> pair (type_definition, less_definition, set_def));
104 fun make_cpo admissible (type_def, less_def, set_def) theory =
106 val admissible' = fold_rule (the_list set_def) admissible;
107 val cpo_thms = [type_def, less_def, admissible'];
110 |> AxClass.prove_arity (full_tname, lhs_sorts, ["Pcpo.cpo"])
111 (Tactic.rtac (typedef_cpo OF cpo_thms) 1)
112 |> Sign.add_path name
114 ([(("adm_" ^ name, admissible'), []),
115 (("cont_" ^ Rep_name, cont_Rep OF cpo_thms), []),
116 (("cont_" ^ Abs_name, cont_Abs OF cpo_thms), []),
117 (("lub_" ^ name, typedef_lub OF cpo_thms), []),
118 (("thelub_" ^ name, typedef_thelub OF cpo_thms), []),
119 (("compact_" ^ name, typedef_compact OF cpo_thms), [])])
124 fun make_pcpo UUmem (type_def, less_def, set_def) theory =
126 val UUmem' = fold_rule (the_list set_def) UUmem;
127 val pcpo_thms = [type_def, less_def, UUmem'];
130 |> AxClass.prove_arity (full_tname, lhs_sorts, ["Pcpo.pcpo"])
131 (Tactic.rtac (typedef_pcpo OF pcpo_thms) 1)
132 |> Sign.add_path name
134 ([((Rep_name ^ "_strict", Rep_strict OF pcpo_thms), []),
135 ((Abs_name ^ "_strict", Abs_strict OF pcpo_thms), []),
136 ((Rep_name ^ "_defined", Rep_defined OF pcpo_thms), []),
137 ((Abs_name ^ "_defined", Abs_defined OF pcpo_thms), [])
143 fun pcpodef_result UUmem_admissible theory =
145 val UUmem = UUmem_admissible RS conjunct1;
146 val admissible = UUmem_admissible RS conjunct2;
149 |> make_po (Tactic.rtac exI 1 THEN Tactic.rtac UUmem 1)
150 |-> (fn defs => make_cpo admissible defs #> make_pcpo UUmem defs)
153 fun cpodef_result nonempty_admissible theory =
155 val nonempty = nonempty_admissible RS conjunct1;
156 val admissible = nonempty_admissible RS conjunct2;
159 |> make_po (Tactic.rtac nonempty 1)
160 |-> make_cpo admissible
163 in (goal, if pcpo then pcpodef_result else cpodef_result) end
164 handle ERROR msg => err_in_cpodef msg name;
167 (* cpodef_proof interface *)
169 fun gen_pcpodef_proof prep_term pcpo ((def, name), typ, set, opt_morphs) thy =
171 val (goal, pcpodef_result) =
172 prepare_pcpodef prep_term pcpo def name typ set opt_morphs thy;
173 fun after_qed [[th]] = ProofContext.theory (pcpodef_result th);
174 in Proof.theorem_i NONE after_qed [[(goal, [])]] (ProofContext.init thy) end;
176 fun pcpodef_proof x = gen_pcpodef_proof Syntax.read_term true x;
177 fun pcpodef_proof_i x = gen_pcpodef_proof Syntax.check_term true x;
179 fun cpodef_proof x = gen_pcpodef_proof Syntax.read_term false x;
180 fun cpodef_proof_i x = gen_pcpodef_proof Syntax.check_term false x;
185 local structure P = OuterParse and K = OuterKeyword in
187 (* cf. HOL/Tools/typedef_package.ML *)
188 val typedef_proof_decl =
189 Scan.optional (P.$$$ "(" |--
190 ((P.$$$ "open" >> K false) -- Scan.option P.name || P.name >> (fn s => (true, SOME s)))
191 --| P.$$$ ")") (true, NONE) --
192 (P.type_args -- P.name) -- P.opt_infix -- (P.$$$ "=" |-- P.term) --
193 Scan.option (P.$$$ "morphisms" |-- P.!!! (P.name -- P.name));
195 fun mk_pcpodef_proof pcpo ((((((def, opt_name), (vs, t)), mx), A), morphs)) =
196 (if pcpo then pcpodef_proof else cpodef_proof)
197 ((def, the_default (Syntax.type_name t mx) opt_name), (t, vs, mx), A, morphs);
200 OuterSyntax.command "pcpodef" "HOLCF type definition (requires admissibility proof)" K.thy_goal
201 (typedef_proof_decl >>
202 (Toplevel.print oo (Toplevel.theory_to_proof o mk_pcpodef_proof true)));
205 OuterSyntax.command "cpodef" "HOLCF type definition (requires admissibility proof)" K.thy_goal
206 (typedef_proof_decl >>
207 (Toplevel.print oo (Toplevel.theory_to_proof o mk_pcpodef_proof false)));