author | huffman |
Tue, 26 Jul 2005 18:29:59 +0200 | |
changeset 16922 | 2128ac2aa5db |
parent 16919 | 6df23e3180fb |
child 17057 | 0934ac31985f |
permissions | -rw-r--r-- |
16696 | 1 |
(* Title: HOLCF/pcpodef_package.ML |
2 |
ID: $Id$ |
|
3 |
Author: Brian Huffman |
|
4 |
||
5 |
Gordon/HOL-style type definitions for HOLCF. |
|
6 |
*) |
|
7 |
||
8 |
signature PCPODEF_PACKAGE = |
|
9 |
sig |
|
10 |
val quiet_mode: bool ref |
|
11 |
val pcpodef_proof: (bool * string) * (bstring * string list * mixfix) * string |
|
12 |
* (string * string) option -> bool -> theory -> ProofHistory.T |
|
13 |
val pcpodef_proof_i: (bool * string) * (bstring * string list * mixfix) * term |
|
14 |
* (string * string) option -> bool -> theory -> ProofHistory.T |
|
15 |
val cpodef_proof: (bool * string) * (bstring * string list * mixfix) * string |
|
16 |
* (string * string) option -> bool -> theory -> ProofHistory.T |
|
17 |
val cpodef_proof_i: (bool * string) * (bstring * string list * mixfix) * term |
|
18 |
* (string * string) option -> bool -> theory -> ProofHistory.T |
|
19 |
end; |
|
20 |
||
21 |
structure PcpodefPackage: PCPODEF_PACKAGE = |
|
22 |
struct |
|
23 |
||
24 |
||
25 |
(** theory context references **) |
|
26 |
||
27 |
val typedef_po = thm "typedef_po"; |
|
28 |
val typedef_cpo = thm "typedef_cpo"; |
|
16919
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
29 |
val typedef_pcpo = thm "typedef_pcpo"; |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
30 |
val typedef_lub = thm "typedef_lub"; |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
31 |
val typedef_thelub = thm "typedef_thelub"; |
16696 | 32 |
val cont_Rep = thm "typedef_cont_Rep"; |
33 |
val cont_Abs = thm "typedef_cont_Abs"; |
|
34 |
val Rep_strict = thm "typedef_Rep_strict"; |
|
35 |
val Abs_strict = thm "typedef_Abs_strict"; |
|
36 |
val Rep_defined = thm "typedef_Rep_defined"; |
|
37 |
val Abs_defined = thm "typedef_Abs_defined"; |
|
38 |
||
39 |
||
40 |
(** type definitions **) |
|
41 |
||
42 |
(* messages *) |
|
43 |
||
44 |
val quiet_mode = ref false; |
|
45 |
fun message s = if ! quiet_mode then () else writeln s; |
|
46 |
||
47 |
||
48 |
(* prepare_cpodef *) |
|
49 |
||
50 |
fun read_term thy used s = |
|
51 |
#1 (Thm.read_def_cterm (thy, K NONE, K NONE) used true (s, HOLogic.typeT)); |
|
52 |
||
53 |
fun cert_term thy _ t = Thm.cterm_of thy t handle TERM (msg, _) => error msg; |
|
54 |
||
55 |
fun err_in_cpodef name = |
|
56 |
error ("The error(s) above occurred in cpodef " ^ quote name); |
|
57 |
||
58 |
fun adm_const T = Const ("Adm.adm", (T --> HOLogic.boolT) --> HOLogic.boolT); |
|
59 |
fun mk_adm (x, T, P) = adm_const T $ absfree (x, T, P); |
|
60 |
||
61 |
fun gen_prepare_pcpodef prep_term pcpo def name (t, vs, mx) raw_set opt_morphs thy = |
|
62 |
let |
|
63 |
val full = Sign.full_name thy; |
|
64 |
||
65 |
(*rhs*) |
|
66 |
val full_name = full name; |
|
67 |
val cset = prep_term thy vs raw_set; |
|
68 |
val {T = setT, t = set, ...} = Thm.rep_cterm cset; |
|
69 |
val rhs_tfrees = term_tfrees set; |
|
70 |
val oldT = HOLogic.dest_setT setT handle TYPE _ => |
|
71 |
error ("Not a set type: " ^ quote (Sign.string_of_typ thy setT)); |
|
72 |
fun mk_nonempty A = |
|
73 |
HOLogic.mk_exists ("x", oldT, HOLogic.mk_mem (Free ("x", oldT), A)); |
|
74 |
fun mk_admissible A = |
|
75 |
mk_adm ("x", oldT, HOLogic.mk_mem (Free ("x", oldT), A)); |
|
76 |
fun mk_UU_mem A = HOLogic.mk_mem (Const ("Pcpo.UU", oldT), A); |
|
77 |
val goal = if pcpo |
|
78 |
then HOLogic.mk_Trueprop (HOLogic.mk_conj (mk_UU_mem set, mk_admissible set)) |
|
79 |
else HOLogic.mk_Trueprop (HOLogic.mk_conj (mk_nonempty set, mk_admissible set)); |
|
80 |
||
81 |
(*lhs*) |
|
82 |
val lhs_tfrees = map (fn v => (v, getOpt (assoc (rhs_tfrees, v), HOLogic.typeS))) 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); |
|
87 |
||
88 |
val (Rep_name, Abs_name) = getOpt (opt_morphs, ("Rep_" ^ name, "Abs_" ^ name)); |
|
89 |
val RepC = Const (full Rep_name, newT --> oldT); |
|
90 |
fun lessC T = Const ("Porder.op <<", 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))))); |
|
93 |
||
94 |
fun option_fold_rule NONE = I |
|
95 |
| option_fold_rule (SOME def) = fold_rule [def]; |
|
96 |
||
97 |
fun make_po tac theory = |
|
98 |
theory |
|
99 |
|> TypedefPackage.add_typedef_i def (SOME name) (t, vs, mx) set opt_morphs tac |
|
100 |
|>> AxClass.add_inst_arity_i (full_tname, lhs_sorts, ["Porder.sq_ord"]) |
|
101 |
(AxClass.intro_classes_tac []) |
|
102 |
|>>> PureThy.add_defs_i true [Thm.no_attributes less_def] |
|
103 |
|> (fn (theory', ({type_definition, set_def, ...}, [less_definition])) => |
|
104 |
theory' |
|
105 |
|> AxClass.add_inst_arity_i (full_tname, lhs_sorts, ["Porder.po"]) |
|
106 |
(Tactic.rtac (typedef_po OF [type_definition, less_definition]) 1) |
|
107 |
|> rpair (type_definition, less_definition, set_def)); |
|
108 |
||
16919
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
109 |
fun make_cpo admissible (theory, defs as (type_def, less_def, set_def)) = |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
110 |
let |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
111 |
val admissible' = option_fold_rule set_def admissible; |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
112 |
val cpo_thms = [type_def, less_def, admissible']; |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
113 |
val (theory', _) = |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
114 |
theory |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
115 |
|> AxClass.add_inst_arity_i (full_tname, lhs_sorts, ["Pcpo.cpo"]) |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
116 |
(Tactic.rtac (typedef_cpo OF cpo_thms) 1) |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
117 |
|> Theory.add_path name |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
118 |
|> PureThy.add_thms |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
119 |
([(("adm_" ^ name, admissible'), []), |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
120 |
(("cont_" ^ Rep_name, cont_Rep OF cpo_thms), []), |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
121 |
(("cont_" ^ Abs_name, cont_Abs OF cpo_thms), []), |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
122 |
(("lub_" ^ name, typedef_lub OF cpo_thms), []), |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
123 |
(("thelub_" ^ name, typedef_thelub OF cpo_thms), [])]) |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
124 |
|>> Theory.parent_path; |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
125 |
in (theory', defs) end; |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
126 |
|
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
127 |
fun make_pcpo UUmem (theory, defs as (type_def, less_def, set_def)) = |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
128 |
let |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
129 |
val UUmem' = option_fold_rule set_def UUmem; |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
130 |
val pcpo_thms = [type_def, less_def, UUmem']; |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
131 |
val (theory', _) = |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
132 |
theory |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
133 |
|> AxClass.add_inst_arity_i (full_tname, lhs_sorts, ["Pcpo.pcpo"]) |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
134 |
(Tactic.rtac (typedef_pcpo OF pcpo_thms) 1) |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
135 |
|> Theory.add_path name |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
136 |
|> PureThy.add_thms |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
137 |
([((Rep_name ^ "_strict", Rep_strict OF pcpo_thms), []), |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
138 |
((Abs_name ^ "_strict", Abs_strict OF pcpo_thms), []), |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
139 |
((Rep_name ^ "_defined", Rep_defined OF pcpo_thms), []), |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
140 |
((Abs_name ^ "_defined", Abs_defined OF pcpo_thms), []) |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
141 |
]) |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
142 |
|>> Theory.parent_path; |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
143 |
in (theory', defs) end; |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
144 |
|
16696 | 145 |
fun pcpodef_result (theory, UUmem_admissible) = |
146 |
let |
|
147 |
val UUmem = UUmem_admissible RS conjunct1; |
|
148 |
val admissible = UUmem_admissible RS conjunct2; |
|
149 |
in |
|
150 |
theory |
|
151 |
|> make_po (Tactic.rtac exI 1 THEN Tactic.rtac UUmem 1) |
|
16919
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
152 |
|> make_cpo admissible |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
153 |
|> make_pcpo UUmem |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
154 |
|> (fn (theory', (type_def, _, _)) => (theory', type_def)) |
16696 | 155 |
end; |
156 |
||
157 |
fun cpodef_result (theory, nonempty_admissible) = |
|
158 |
let |
|
159 |
val nonempty = nonempty_admissible RS conjunct1; |
|
160 |
val admissible = nonempty_admissible RS conjunct2; |
|
161 |
in |
|
162 |
theory |
|
163 |
|> make_po (Tactic.rtac nonempty 1) |
|
16919
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
164 |
|> make_cpo admissible |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
165 |
|> (fn (theory', (type_def, _, _)) => (theory', type_def)) |
16696 | 166 |
end; |
167 |
||
168 |
in (goal, if pcpo then pcpodef_result else cpodef_result) end |
|
169 |
handle ERROR => err_in_cpodef name; |
|
170 |
||
171 |
(* cpodef_proof interface *) |
|
172 |
||
173 |
fun gen_pcpodef_proof prep_term pcpo ((def, name), typ, set, opt_morphs) int thy = |
|
174 |
let |
|
175 |
val (goal, att) = |
|
176 |
gen_prepare_pcpodef prep_term pcpo def name typ set opt_morphs thy; |
|
177 |
in thy |> IsarThy.theorem_i Drule.internalK (("", [att]), (goal, ([], []))) int end; |
|
178 |
||
179 |
val pcpodef_proof = gen_pcpodef_proof read_term true; |
|
180 |
val pcpodef_proof_i = gen_pcpodef_proof cert_term true; |
|
181 |
||
182 |
val cpodef_proof = gen_pcpodef_proof read_term false; |
|
183 |
val cpodef_proof_i = gen_pcpodef_proof cert_term false; |
|
184 |
||
185 |
||
186 |
(** outer syntax **) |
|
187 |
||
188 |
local structure P = OuterParse and K = OuterSyntax.Keyword in |
|
189 |
||
190 |
(* copied from HOL/Tools/typedef_package.ML *) |
|
191 |
val typedef_proof_decl = |
|
192 |
Scan.optional (P.$$$ "(" |-- |
|
193 |
((P.$$$ "open" >> K false) -- Scan.option P.name || P.name >> (fn s => (true, SOME s))) |
|
194 |
--| P.$$$ ")") (true, NONE) -- |
|
195 |
(P.type_args -- P.name) -- P.opt_infix -- (P.$$$ "=" |-- P.term) -- |
|
196 |
Scan.option (P.$$$ "morphisms" |-- P.!!! (P.name -- P.name)); |
|
197 |
||
198 |
fun mk_pcpodef_proof pcpo ((((((def, opt_name), (vs, t)), mx), A), morphs)) = |
|
199 |
(if pcpo then pcpodef_proof else cpodef_proof) |
|
200 |
((def, getOpt (opt_name, Syntax.type_name t mx)), (t, vs, mx), A, morphs); |
|
201 |
||
202 |
val pcpodefP = |
|
203 |
OuterSyntax.command "pcpodef" "HOLCF type definition (requires admissibility proof)" K.thy_goal |
|
204 |
(typedef_proof_decl >> |
|
205 |
(Toplevel.print oo (Toplevel.theory_to_proof o mk_pcpodef_proof true))); |
|
206 |
||
207 |
val cpodefP = |
|
208 |
OuterSyntax.command "cpodef" "HOLCF type definition (requires admissibility proof)" K.thy_goal |
|
209 |
(typedef_proof_decl >> |
|
210 |
(Toplevel.print oo (Toplevel.theory_to_proof o mk_pcpodef_proof false))); |
|
211 |
||
212 |
val _ = OuterSyntax.add_parsers [pcpodefP, cpodefP]; |
|
213 |
||
214 |
end; |
|
215 |
||
216 |
end; |