author | wenzelm |
Sat, 07 Mar 2009 22:17:25 +0100 | |
changeset 30345 | 76fd85bbf139 |
parent 29585 | c23295521af5 |
child 31023 | d027411c9a38 |
permissions | -rw-r--r-- |
23152 | 1 |
(* Title: HOLCF/Tools/pcpodef_package.ML |
2 |
Author: Brian Huffman |
|
3 |
||
4 |
Primitive domain definitions for HOLCF, similar to Gordon/HOL-style |
|
29060
d7bde0b4bf72
tuned signature: pcpodef_proof, pcpodef_proof_cmd etc.;
wenzelm
parents:
28965
diff
changeset
|
5 |
typedef (see also ~~/src/HOL/Tools/typedef_package.ML). |
23152 | 6 |
*) |
7 |
||
8 |
signature PCPODEF_PACKAGE = |
|
9 |
sig |
|
30345 | 10 |
val pcpodef_proof: (bool * binding) * (binding * string list * mixfix) * term |
11 |
* (binding * binding) option -> theory -> Proof.state |
|
12 |
val pcpodef_proof_cmd: (bool * binding) * (binding * string list * mixfix) * string |
|
13 |
* (binding * binding) option -> theory -> Proof.state |
|
14 |
val cpodef_proof: (bool * binding) * (binding * string list * mixfix) * term |
|
15 |
* (binding * binding) option -> theory -> Proof.state |
|
16 |
val cpodef_proof_cmd: (bool * binding) * (binding * string list * mixfix) * string |
|
17 |
* (binding * binding) option -> theory -> Proof.state |
|
23152 | 18 |
end; |
19 |
||
20 |
structure PcpodefPackage: PCPODEF_PACKAGE = |
|
21 |
struct |
|
22 |
||
23 |
(** type definitions **) |
|
24 |
||
25 |
(* prepare_cpodef *) |
|
26 |
||
27 |
fun declare_type_name a = Variable.declare_constraints (Logic.mk_type (TFree (a, dummyS))); |
|
28 |
||
29060
d7bde0b4bf72
tuned signature: pcpodef_proof, pcpodef_proof_cmd etc.;
wenzelm
parents:
28965
diff
changeset
|
29 |
fun adm_const T = Const (@{const_name adm}, (T --> HOLogic.boolT) --> HOLogic.boolT); |
23152 | 30 |
fun mk_adm (x, T, P) = adm_const T $ absfree (x, T, P); |
31 |
||
32 |
fun prepare_pcpodef prep_term pcpo def name (t, vs, mx) raw_set opt_morphs thy = |
|
33 |
let |
|
29060
d7bde0b4bf72
tuned signature: pcpodef_proof, pcpodef_proof_cmd etc.;
wenzelm
parents:
28965
diff
changeset
|
34 |
val _ = Theory.requires thy "Pcpodef" "pcpodefs"; |
23152 | 35 |
val ctxt = ProofContext.init thy; |
30345 | 36 |
|
37 |
val full = Sign.full_name thy; |
|
38 |
val full_name = full name; |
|
39 |
val bname = Binding.name_of name; |
|
23152 | 40 |
|
41 |
(*rhs*) |
|
42 |
val set = prep_term (ctxt |> fold declare_type_name vs) raw_set; |
|
43 |
val setT = Term.fastype_of set; |
|
29060
d7bde0b4bf72
tuned signature: pcpodef_proof, pcpodef_proof_cmd etc.;
wenzelm
parents:
28965
diff
changeset
|
44 |
val rhs_tfrees = Term.add_tfrees set []; |
23152 | 45 |
val oldT = HOLogic.dest_setT setT handle TYPE _ => |
24920 | 46 |
error ("Not a set type: " ^ quote (Syntax.string_of_typ ctxt setT)); |
29060
d7bde0b4bf72
tuned signature: pcpodef_proof, pcpodef_proof_cmd etc.;
wenzelm
parents:
28965
diff
changeset
|
47 |
|
d7bde0b4bf72
tuned signature: pcpodef_proof, pcpodef_proof_cmd etc.;
wenzelm
parents:
28965
diff
changeset
|
48 |
(*goal*) |
29063
7619f0561cd7
pcpodef package: state two goals, instead of encoded conjunction;
wenzelm
parents:
29060
diff
changeset
|
49 |
val goal_UU_mem = HOLogic.mk_Trueprop (HOLogic.mk_mem (Const (@{const_name UU}, oldT), set)); |
7619f0561cd7
pcpodef package: state two goals, instead of encoded conjunction;
wenzelm
parents:
29060
diff
changeset
|
50 |
val goal_nonempty = |
7619f0561cd7
pcpodef package: state two goals, instead of encoded conjunction;
wenzelm
parents:
29060
diff
changeset
|
51 |
HOLogic.mk_Trueprop (HOLogic.mk_exists ("x", oldT, HOLogic.mk_mem (Free ("x", oldT), set))); |
7619f0561cd7
pcpodef package: state two goals, instead of encoded conjunction;
wenzelm
parents:
29060
diff
changeset
|
52 |
val goal_admissible = |
7619f0561cd7
pcpodef package: state two goals, instead of encoded conjunction;
wenzelm
parents:
29060
diff
changeset
|
53 |
HOLogic.mk_Trueprop (mk_adm ("x", oldT, HOLogic.mk_mem (Free ("x", oldT), set))); |
23152 | 54 |
|
55 |
(*lhs*) |
|
56 |
val defS = Sign.defaultS thy; |
|
57 |
val lhs_tfrees = map (fn v => (v, the_default defS (AList.lookup (op =) rhs_tfrees v))) vs; |
|
58 |
val lhs_sorts = map snd lhs_tfrees; |
|
29060
d7bde0b4bf72
tuned signature: pcpodef_proof, pcpodef_proof_cmd etc.;
wenzelm
parents:
28965
diff
changeset
|
59 |
|
30345 | 60 |
val tname = Binding.map_name (Syntax.type_name mx) t; |
23152 | 61 |
val full_tname = full tname; |
62 |
val newT = Type (full_tname, map TFree lhs_tfrees); |
|
63 |
||
30345 | 64 |
val (Rep_name, Abs_name) = |
65 |
(case opt_morphs of |
|
66 |
NONE => (Binding.prefix_name "Rep_" name, Binding.prefix_name "Abs_" name) |
|
67 |
| SOME morphs => morphs); |
|
23152 | 68 |
val RepC = Const (full Rep_name, newT --> oldT); |
29060
d7bde0b4bf72
tuned signature: pcpodef_proof, pcpodef_proof_cmd etc.;
wenzelm
parents:
28965
diff
changeset
|
69 |
fun lessC T = Const (@{const_name sq_le}, T --> T --> HOLogic.boolT); |
28073 | 70 |
val less_def = Logic.mk_equals (lessC newT, |
71 |
Abs ("x", newT, Abs ("y", newT, lessC oldT $ (RepC $ Bound 1) $ (RepC $ Bound 0)))); |
|
23152 | 72 |
|
28073 | 73 |
fun make_po tac thy1 = |
74 |
let |
|
75 |
val ((_, {type_definition, set_def, ...}), thy2) = thy1 |
|
28662 | 76 |
|> TypedefPackage.add_typedef def (SOME name) (t, vs, mx) set opt_morphs tac; |
28073 | 77 |
val lthy3 = thy2 |
29060
d7bde0b4bf72
tuned signature: pcpodef_proof, pcpodef_proof_cmd etc.;
wenzelm
parents:
28965
diff
changeset
|
78 |
|> TheoryTarget.instantiation ([full_tname], lhs_tfrees, @{sort po}); |
28073 | 79 |
val less_def' = Syntax.check_term lthy3 less_def; |
80 |
val ((_, (_, less_definition')), lthy4) = lthy3 |
|
30345 | 81 |
|> Specification.definition (NONE, |
82 |
((Binding.prefix_name "less_" (Binding.suffix_name "_def" name), []), less_def')); |
|
28073 | 83 |
val ctxt_thy = ProofContext.init (ProofContext.theory_of lthy4); |
84 |
val less_definition = singleton (ProofContext.export lthy4 ctxt_thy) less_definition'; |
|
85 |
val thy5 = lthy4 |
|
86 |
|> Class.prove_instantiation_instance |
|
29060
d7bde0b4bf72
tuned signature: pcpodef_proof, pcpodef_proof_cmd etc.;
wenzelm
parents:
28965
diff
changeset
|
87 |
(K (Tactic.rtac (@{thm typedef_po} OF [type_definition, less_definition]) 1)) |
28394 | 88 |
|> LocalTheory.exit_global; |
28073 | 89 |
in ((type_definition, less_definition, set_def), thy5) end; |
23152 | 90 |
|
91 |
fun make_cpo admissible (type_def, less_def, set_def) theory = |
|
92 |
let |
|
93 |
val admissible' = fold_rule (the_list set_def) admissible; |
|
28377
73b380ba1743
proper transfer of theorems that involve classes being instantiated here;
wenzelm
parents:
28083
diff
changeset
|
94 |
val cpo_thms = map (Thm.transfer theory) [type_def, less_def, admissible']; |
73b380ba1743
proper transfer of theorems that involve classes being instantiated here;
wenzelm
parents:
28083
diff
changeset
|
95 |
val theory' = theory |
29060
d7bde0b4bf72
tuned signature: pcpodef_proof, pcpodef_proof_cmd etc.;
wenzelm
parents:
28965
diff
changeset
|
96 |
|> AxClass.prove_arity (full_tname, lhs_sorts, @{sort cpo}) |
d7bde0b4bf72
tuned signature: pcpodef_proof, pcpodef_proof_cmd etc.;
wenzelm
parents:
28965
diff
changeset
|
97 |
(Tactic.rtac (@{thm typedef_cpo} OF cpo_thms) 1); |
28377
73b380ba1743
proper transfer of theorems that involve classes being instantiated here;
wenzelm
parents:
28083
diff
changeset
|
98 |
val cpo_thms' = map (Thm.transfer theory') cpo_thms; |
23152 | 99 |
in |
28377
73b380ba1743
proper transfer of theorems that involve classes being instantiated here;
wenzelm
parents:
28083
diff
changeset
|
100 |
theory' |
30345 | 101 |
|> Sign.add_path (Binding.name_of name) |
23152 | 102 |
|> PureThy.add_thms |
30345 | 103 |
([((Binding.prefix_name "adm_" name, admissible'), []), |
104 |
((Binding.prefix_name "cont_" Rep_name, @{thm typedef_cont_Rep} OF cpo_thms'), []), |
|
105 |
((Binding.prefix_name "cont_" Abs_name, @{thm typedef_cont_Abs} OF cpo_thms'), []), |
|
106 |
((Binding.prefix_name "lub_" name, @{thm typedef_lub} OF cpo_thms'), []), |
|
107 |
((Binding.prefix_name "thelub_" name, @{thm typedef_thelub} OF cpo_thms'), []), |
|
108 |
((Binding.prefix_name "compact_" name, @{thm typedef_compact} OF cpo_thms'), [])]) |
|
23152 | 109 |
|> snd |
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24509
diff
changeset
|
110 |
|> Sign.parent_path |
23152 | 111 |
end; |
112 |
||
29063
7619f0561cd7
pcpodef package: state two goals, instead of encoded conjunction;
wenzelm
parents:
29060
diff
changeset
|
113 |
fun make_pcpo UU_mem (type_def, less_def, set_def) theory = |
23152 | 114 |
let |
29063
7619f0561cd7
pcpodef package: state two goals, instead of encoded conjunction;
wenzelm
parents:
29060
diff
changeset
|
115 |
val UU_mem' = fold_rule (the_list set_def) UU_mem; |
7619f0561cd7
pcpodef package: state two goals, instead of encoded conjunction;
wenzelm
parents:
29060
diff
changeset
|
116 |
val pcpo_thms = map (Thm.transfer theory) [type_def, less_def, UU_mem']; |
28377
73b380ba1743
proper transfer of theorems that involve classes being instantiated here;
wenzelm
parents:
28083
diff
changeset
|
117 |
val theory' = theory |
29060
d7bde0b4bf72
tuned signature: pcpodef_proof, pcpodef_proof_cmd etc.;
wenzelm
parents:
28965
diff
changeset
|
118 |
|> AxClass.prove_arity (full_tname, lhs_sorts, @{sort pcpo}) |
d7bde0b4bf72
tuned signature: pcpodef_proof, pcpodef_proof_cmd etc.;
wenzelm
parents:
28965
diff
changeset
|
119 |
(Tactic.rtac (@{thm typedef_pcpo} OF pcpo_thms) 1); |
28377
73b380ba1743
proper transfer of theorems that involve classes being instantiated here;
wenzelm
parents:
28083
diff
changeset
|
120 |
val pcpo_thms' = map (Thm.transfer theory') pcpo_thms; |
23152 | 121 |
in |
28377
73b380ba1743
proper transfer of theorems that involve classes being instantiated here;
wenzelm
parents:
28083
diff
changeset
|
122 |
theory' |
30345 | 123 |
|> Sign.add_path (Binding.name_of name) |
23152 | 124 |
|> PureThy.add_thms |
30345 | 125 |
([((Binding.suffix_name "_strict" Rep_name, @{thm typedef_Rep_strict} OF pcpo_thms'), []), |
126 |
((Binding.suffix_name "_strict" Abs_name, @{thm typedef_Abs_strict} OF pcpo_thms'), []), |
|
127 |
((Binding.suffix_name "_strict_iff" Rep_name, @{thm typedef_Rep_strict_iff} OF pcpo_thms'), []), |
|
128 |
((Binding.suffix_name "_strict_iff" Abs_name, @{thm typedef_Abs_strict_iff} OF pcpo_thms'), []), |
|
129 |
((Binding.suffix_name "_defined" Rep_name, @{thm typedef_Rep_defined} OF pcpo_thms'), []), |
|
130 |
((Binding.suffix_name "_defined" Abs_name, @{thm typedef_Abs_defined} OF pcpo_thms'), [])]) |
|
23152 | 131 |
|> snd |
24712
64ed05609568
proper Sign operations instead of Theory aliases;
wenzelm
parents:
24509
diff
changeset
|
132 |
|> Sign.parent_path |
23152 | 133 |
end; |
134 |
||
29063
7619f0561cd7
pcpodef package: state two goals, instead of encoded conjunction;
wenzelm
parents:
29060
diff
changeset
|
135 |
fun pcpodef_result UU_mem admissible = |
7619f0561cd7
pcpodef package: state two goals, instead of encoded conjunction;
wenzelm
parents:
29060
diff
changeset
|
136 |
make_po (Tactic.rtac exI 1 THEN Tactic.rtac UU_mem 1) |
7619f0561cd7
pcpodef package: state two goals, instead of encoded conjunction;
wenzelm
parents:
29060
diff
changeset
|
137 |
#-> (fn defs => make_cpo admissible defs #> make_pcpo UU_mem defs); |
23152 | 138 |
|
29063
7619f0561cd7
pcpodef package: state two goals, instead of encoded conjunction;
wenzelm
parents:
29060
diff
changeset
|
139 |
fun cpodef_result nonempty admissible = |
7619f0561cd7
pcpodef package: state two goals, instead of encoded conjunction;
wenzelm
parents:
29060
diff
changeset
|
140 |
make_po (Tactic.rtac nonempty 1) |
7619f0561cd7
pcpodef package: state two goals, instead of encoded conjunction;
wenzelm
parents:
29060
diff
changeset
|
141 |
#-> make_cpo admissible; |
7619f0561cd7
pcpodef package: state two goals, instead of encoded conjunction;
wenzelm
parents:
29060
diff
changeset
|
142 |
in |
7619f0561cd7
pcpodef package: state two goals, instead of encoded conjunction;
wenzelm
parents:
29060
diff
changeset
|
143 |
if pcpo |
7619f0561cd7
pcpodef package: state two goals, instead of encoded conjunction;
wenzelm
parents:
29060
diff
changeset
|
144 |
then (goal_UU_mem, goal_admissible, pcpodef_result) |
7619f0561cd7
pcpodef package: state two goals, instead of encoded conjunction;
wenzelm
parents:
29060
diff
changeset
|
145 |
else (goal_nonempty, goal_admissible, cpodef_result) |
7619f0561cd7
pcpodef package: state two goals, instead of encoded conjunction;
wenzelm
parents:
29060
diff
changeset
|
146 |
end |
30345 | 147 |
handle ERROR msg => |
148 |
cat_error msg ("The error(s) above occurred in cpodef " ^ quote (Binding.str_of name)); |
|
23152 | 149 |
|
150 |
||
29060
d7bde0b4bf72
tuned signature: pcpodef_proof, pcpodef_proof_cmd etc.;
wenzelm
parents:
28965
diff
changeset
|
151 |
(* proof interface *) |
d7bde0b4bf72
tuned signature: pcpodef_proof, pcpodef_proof_cmd etc.;
wenzelm
parents:
28965
diff
changeset
|
152 |
|
d7bde0b4bf72
tuned signature: pcpodef_proof, pcpodef_proof_cmd etc.;
wenzelm
parents:
28965
diff
changeset
|
153 |
local |
23152 | 154 |
|
155 |
fun gen_pcpodef_proof prep_term pcpo ((def, name), typ, set, opt_morphs) thy = |
|
156 |
let |
|
29063
7619f0561cd7
pcpodef package: state two goals, instead of encoded conjunction;
wenzelm
parents:
29060
diff
changeset
|
157 |
val (goal1, goal2, make_result) = |
23152 | 158 |
prepare_pcpodef prep_term pcpo def name typ set opt_morphs thy; |
29063
7619f0561cd7
pcpodef package: state two goals, instead of encoded conjunction;
wenzelm
parents:
29060
diff
changeset
|
159 |
fun after_qed [[th1, th2]] = ProofContext.theory (make_result th1 th2); |
7619f0561cd7
pcpodef package: state two goals, instead of encoded conjunction;
wenzelm
parents:
29060
diff
changeset
|
160 |
in Proof.theorem_i NONE after_qed [[(goal1, []), (goal2, [])]] (ProofContext.init thy) end; |
23152 | 161 |
|
29060
d7bde0b4bf72
tuned signature: pcpodef_proof, pcpodef_proof_cmd etc.;
wenzelm
parents:
28965
diff
changeset
|
162 |
in |
d7bde0b4bf72
tuned signature: pcpodef_proof, pcpodef_proof_cmd etc.;
wenzelm
parents:
28965
diff
changeset
|
163 |
|
d7bde0b4bf72
tuned signature: pcpodef_proof, pcpodef_proof_cmd etc.;
wenzelm
parents:
28965
diff
changeset
|
164 |
fun pcpodef_proof x = gen_pcpodef_proof Syntax.check_term true x; |
d7bde0b4bf72
tuned signature: pcpodef_proof, pcpodef_proof_cmd etc.;
wenzelm
parents:
28965
diff
changeset
|
165 |
fun pcpodef_proof_cmd x = gen_pcpodef_proof Syntax.read_term true x; |
23152 | 166 |
|
29060
d7bde0b4bf72
tuned signature: pcpodef_proof, pcpodef_proof_cmd etc.;
wenzelm
parents:
28965
diff
changeset
|
167 |
fun cpodef_proof x = gen_pcpodef_proof Syntax.check_term false x; |
d7bde0b4bf72
tuned signature: pcpodef_proof, pcpodef_proof_cmd etc.;
wenzelm
parents:
28965
diff
changeset
|
168 |
fun cpodef_proof_cmd x = gen_pcpodef_proof Syntax.read_term false x; |
d7bde0b4bf72
tuned signature: pcpodef_proof, pcpodef_proof_cmd etc.;
wenzelm
parents:
28965
diff
changeset
|
169 |
|
d7bde0b4bf72
tuned signature: pcpodef_proof, pcpodef_proof_cmd etc.;
wenzelm
parents:
28965
diff
changeset
|
170 |
end; |
d7bde0b4bf72
tuned signature: pcpodef_proof, pcpodef_proof_cmd etc.;
wenzelm
parents:
28965
diff
changeset
|
171 |
|
23152 | 172 |
|
173 |
||
174 |
(** outer syntax **) |
|
175 |
||
176 |
local structure P = OuterParse and K = OuterKeyword in |
|
177 |
||
178 |
val typedef_proof_decl = |
|
179 |
Scan.optional (P.$$$ "(" |-- |
|
30345 | 180 |
((P.$$$ "open" >> K false) -- Scan.option P.binding || P.binding >> (fn s => (true, SOME s))) |
23152 | 181 |
--| P.$$$ ")") (true, NONE) -- |
30345 | 182 |
(P.type_args -- P.binding) -- P.opt_infix -- (P.$$$ "=" |-- P.term) -- |
183 |
Scan.option (P.$$$ "morphisms" |-- P.!!! (P.binding -- P.binding)); |
|
23152 | 184 |
|
185 |
fun mk_pcpodef_proof pcpo ((((((def, opt_name), (vs, t)), mx), A), morphs)) = |
|
29060
d7bde0b4bf72
tuned signature: pcpodef_proof, pcpodef_proof_cmd etc.;
wenzelm
parents:
28965
diff
changeset
|
186 |
(if pcpo then pcpodef_proof_cmd else cpodef_proof_cmd) |
30345 | 187 |
((def, the_default (Binding.map_name (Syntax.type_name mx) t) opt_name), (t, vs, mx), A, morphs); |
23152 | 188 |
|
24867 | 189 |
val _ = |
23152 | 190 |
OuterSyntax.command "pcpodef" "HOLCF type definition (requires admissibility proof)" K.thy_goal |
191 |
(typedef_proof_decl >> |
|
192 |
(Toplevel.print oo (Toplevel.theory_to_proof o mk_pcpodef_proof true))); |
|
193 |
||
24867 | 194 |
val _ = |
23152 | 195 |
OuterSyntax.command "cpodef" "HOLCF type definition (requires admissibility proof)" K.thy_goal |
196 |
(typedef_proof_decl >> |
|
197 |
(Toplevel.print oo (Toplevel.theory_to_proof o mk_pcpodef_proof false))); |
|
198 |
||
199 |
end; |
|
200 |
||
201 |
end; |