author | wenzelm |
Sun, 04 Feb 2007 22:02:18 +0100 | |
changeset 22238 | 090f215ab631 |
parent 21434 | 944f80576be0 |
permissions | -rw-r--r-- |
16696 | 1 |
(* Title: HOLCF/pcpodef_package.ML |
2 |
ID: $Id$ |
|
3 |
Author: Brian Huffman |
|
4 |
||
21352 | 5 |
Primitive domain definitions for HOLCF, similar to Gordon/HOL-style |
6 |
typedef. |
|
16696 | 7 |
*) |
8 |
||
9 |
signature PCPODEF_PACKAGE = |
|
10 |
sig |
|
11 |
val quiet_mode: bool ref |
|
12 |
val pcpodef_proof: (bool * string) * (bstring * string list * mixfix) * string |
|
17336 | 13 |
* (string * string) option -> theory -> Proof.state |
16696 | 14 |
val pcpodef_proof_i: (bool * string) * (bstring * string list * mixfix) * term |
17336 | 15 |
* (string * string) option -> theory -> Proof.state |
16696 | 16 |
val cpodef_proof: (bool * string) * (bstring * string list * mixfix) * string |
17336 | 17 |
* (string * string) option -> theory -> Proof.state |
16696 | 18 |
val cpodef_proof_i: (bool * string) * (bstring * string list * mixfix) * term |
17336 | 19 |
* (string * string) option -> theory -> Proof.state |
16696 | 20 |
end; |
21 |
||
22 |
structure PcpodefPackage: PCPODEF_PACKAGE = |
|
23 |
struct |
|
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"; |
17833 | 32 |
val typedef_compact = thm "typedef_compact"; |
16696 | 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"; |
|
39 |
||
40 |
||
41 |
(** type definitions **) |
|
42 |
||
43 |
(* messages *) |
|
44 |
||
45 |
val quiet_mode = ref false; |
|
46 |
fun message s = if ! quiet_mode then () else writeln s; |
|
47 |
||
48 |
||
49 |
(* prepare_cpodef *) |
|
50 |
||
18678 | 51 |
fun err_in_cpodef msg name = |
52 |
cat_error msg ("The error(s) above occurred in cpodef " ^ quote name); |
|
16696 | 53 |
|
21352 | 54 |
fun declare_type_name a = Variable.declare_constraints (Logic.mk_type (TFree (a, dummyS))); |
55 |
||
16696 | 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); |
|
58 |
||
21352 | 59 |
fun prepare_pcpodef prep_term pcpo def name (t, vs, mx) raw_set opt_morphs thy = |
16696 | 60 |
let |
21352 | 61 |
val ctxt = ProofContext.init thy; |
16696 | 62 |
val full = Sign.full_name thy; |
63 |
||
64 |
(*rhs*) |
|
65 |
val full_name = full name; |
|
21352 | 66 |
val set = prep_term (ctxt |> fold declare_type_name vs) raw_set; |
67 |
val setT = Term.fastype_of set; |
|
16696 | 68 |
val rhs_tfrees = term_tfrees set; |
69 |
val oldT = HOLogic.dest_setT setT handle TYPE _ => |
|
21352 | 70 |
error ("Not a set type: " ^ quote (ProofContext.string_of_typ ctxt setT)); |
16696 | 71 |
fun mk_nonempty A = |
72 |
HOLogic.mk_exists ("x", oldT, HOLogic.mk_mem (Free ("x", oldT), A)); |
|
73 |
fun mk_admissible 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); |
|
76 |
val goal = if pcpo |
|
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)); |
|
21352 | 79 |
|
16696 | 80 |
(*lhs*) |
21352 | 81 |
val defS = Sign.defaultS thy; |
82 |
val lhs_tfrees = map (fn v => (v, the_default defS (AList.lookup (op =) rhs_tfrees v))) vs; |
|
16696 | 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 |
||
21352 | 88 |
val (Rep_name, Abs_name) = the_default ("Rep_" ^ name, "Abs_" ^ name) opt_morphs; |
16696 | 89 |
val RepC = Const (full Rep_name, newT --> oldT); |
17810
3bdf516d93d8
cleaned up; renamed "Porder.op <<" to "Porder.<<"
huffman
parents:
17336
diff
changeset
|
90 |
fun lessC T = Const ("Porder.<<", T --> T --> HOLogic.boolT); |
16696 | 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 |
||
21352 | 94 |
fun make_po tac theory = theory |
16696 | 95 |
|> TypedefPackage.add_typedef_i def (SOME name) (t, vs, mx) set opt_morphs tac |
19349 | 96 |
||> AxClass.prove_arity (full_tname, lhs_sorts, ["Porder.sq_ord"]) |
19110
4bda27adcd2e
moved intro_classes from AxClass to ClassPackage
haftmann
parents:
18799
diff
changeset
|
97 |
(ClassPackage.intro_classes_tac []) |
19349 | 98 |
||>> PureThy.add_defs_i true [Thm.no_attributes less_def] |
20483
04aa552a83bc
TypedefPackage.add_typedef_* now yields name of introduced type constructor
haftmann
parents:
19585
diff
changeset
|
99 |
|-> (fn ((_, {type_definition, set_def, ...}), [less_definition]) => |
19349 | 100 |
AxClass.prove_arity (full_tname, lhs_sorts, ["Porder.po"]) |
18358 | 101 |
(Tactic.rtac (typedef_po OF [type_definition, less_definition]) 1) |
21352 | 102 |
#> pair (type_definition, less_definition, set_def)); |
103 |
||
104 |
fun make_cpo admissible (type_def, less_def, set_def) theory = |
|
16919
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
105 |
let |
21352 | 106 |
val admissible' = fold_rule (the_list set_def) admissible; |
16919
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
107 |
val cpo_thms = [type_def, less_def, admissible']; |
21352 | 108 |
in |
109 |
theory |
|
110 |
|> AxClass.prove_arity (full_tname, lhs_sorts, ["Pcpo.cpo"]) |
|
111 |
(Tactic.rtac (typedef_cpo OF cpo_thms) 1) |
|
112 |
|> Theory.add_path name |
|
113 |
|> PureThy.add_thms |
|
16919
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
114 |
([(("adm_" ^ name, admissible'), []), |
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
115 |
(("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
|
116 |
(("cont_" ^ Abs_name, cont_Abs OF cpo_thms), []), |
17833 | 117 |
(("lub_" ^ name, typedef_lub OF cpo_thms), []), |
118 |
(("thelub_" ^ name, typedef_thelub OF cpo_thms), []), |
|
119 |
(("compact_" ^ name, typedef_compact OF cpo_thms), [])]) |
|
21352 | 120 |
|> snd |
121 |
|> Theory.parent_path |
|
122 |
end; |
|
16919
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
123 |
|
21352 | 124 |
fun make_pcpo UUmem (type_def, less_def, set_def) theory = |
16919
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
125 |
let |
21352 | 126 |
val UUmem' = fold_rule (the_list set_def) UUmem; |
16919
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
127 |
val pcpo_thms = [type_def, less_def, UUmem']; |
21352 | 128 |
in |
129 |
theory |
|
130 |
|> AxClass.prove_arity (full_tname, lhs_sorts, ["Pcpo.pcpo"]) |
|
131 |
(Tactic.rtac (typedef_pcpo OF pcpo_thms) 1) |
|
132 |
|> Theory.add_path name |
|
133 |
|> PureThy.add_thms |
|
16919
6df23e3180fb
removed duplicated code; generate new lub and thelub lemmas for new cpo types
huffman
parents:
16696
diff
changeset
|
134 |
([((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
|
135 |
((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
|
136 |
((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
|
137 |
((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
|
138 |
]) |
21352 | 139 |
|> snd |
140 |
|> Theory.parent_path |
|
141 |
end; |
|
142 |
||
143 |
fun pcpodef_result UUmem_admissible theory = |
|
16696 | 144 |
let |
145 |
val UUmem = UUmem_admissible RS conjunct1; |
|
146 |
val admissible = UUmem_admissible RS conjunct2; |
|
147 |
in |
|
148 |
theory |
|
149 |
|> make_po (Tactic.rtac exI 1 THEN Tactic.rtac UUmem 1) |
|
21352 | 150 |
|-> (fn defs => make_cpo admissible defs #> make_pcpo UUmem defs) |
16696 | 151 |
end; |
21352 | 152 |
|
153 |
fun cpodef_result nonempty_admissible theory = |
|
16696 | 154 |
let |
155 |
val nonempty = nonempty_admissible RS conjunct1; |
|
156 |
val admissible = nonempty_admissible RS conjunct2; |
|
157 |
in |
|
158 |
theory |
|
159 |
|> make_po (Tactic.rtac nonempty 1) |
|
21352 | 160 |
|-> make_cpo admissible |
16696 | 161 |
end; |
21352 | 162 |
|
16696 | 163 |
in (goal, if pcpo then pcpodef_result else cpodef_result) end |
18678 | 164 |
handle ERROR msg => err_in_cpodef msg name; |
16696 | 165 |
|
21352 | 166 |
|
16696 | 167 |
(* cpodef_proof interface *) |
168 |
||
17336 | 169 |
fun gen_pcpodef_proof prep_term pcpo ((def, name), typ, set, opt_morphs) thy = |
16696 | 170 |
let |
21352 | 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); |
|
21434 | 174 |
in Proof.theorem_i NONE after_qed [[(goal, [])]] (ProofContext.init thy) end; |
16696 | 175 |
|
21352 | 176 |
fun pcpodef_proof x = gen_pcpodef_proof ProofContext.read_term true x; |
177 |
fun pcpodef_proof_i x = gen_pcpodef_proof ProofContext.cert_term true x; |
|
16696 | 178 |
|
21352 | 179 |
fun cpodef_proof x = gen_pcpodef_proof ProofContext.read_term false x; |
180 |
fun cpodef_proof_i x = gen_pcpodef_proof ProofContext.cert_term false x; |
|
16696 | 181 |
|
182 |
||
183 |
(** outer syntax **) |
|
184 |
||
17057 | 185 |
local structure P = OuterParse and K = OuterKeyword in |
16696 | 186 |
|
187 |
(* copied from 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)); |
|
194 |
||
195 |
fun mk_pcpodef_proof pcpo ((((((def, opt_name), (vs, t)), mx), A), morphs)) = |
|
196 |
(if pcpo then pcpodef_proof else cpodef_proof) |
|
21352 | 197 |
((def, the_default (Syntax.type_name t mx) opt_name), (t, vs, mx), A, morphs); |
16696 | 198 |
|
199 |
val pcpodefP = |
|
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))); |
|
203 |
||
204 |
val cpodefP = |
|
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))); |
|
208 |
||
209 |
val _ = OuterSyntax.add_parsers [pcpodefP, cpodefP]; |
|
210 |
||
211 |
end; |
|
212 |
||
213 |
end; |