| author | wenzelm |
| Mon, 09 Oct 2006 02:20:07 +0200 | |
| changeset 20914 | 3f065aa89792 |
| parent 20890 | 052bde912a51 |
| child 21036 | 0eed532acfca |
| permissions | -rw-r--r-- |
|
18620
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
1 |
(* Title: Pure/Isar/specification.ML |
|
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
2 |
ID: $Id$ |
|
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
3 |
Author: Makarius |
|
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
4 |
|
| 19664 | 5 |
Common local_theory specifications --- with type-inference and |
| 18810 | 6 |
toplevel polymorphism. |
|
18620
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
7 |
*) |
|
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
8 |
|
|
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
9 |
signature SPECIFICATION = |
|
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
10 |
sig |
| 20890 | 11 |
val quiet_mode: bool ref |
12 |
val print_consts: local_theory -> (string * typ -> bool) -> (string * typ) list -> unit |
|
| 18771 | 13 |
val read_specification: (string * string option * mixfix) list -> |
| 18954 | 14 |
((string * Attrib.src list) * string list) list -> local_theory -> |
| 18771 | 15 |
(((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) * |
| 18954 | 16 |
local_theory |
| 18771 | 17 |
val cert_specification: (string * typ option * mixfix) list -> |
| 18954 | 18 |
((string * Attrib.src list) * term list) list -> local_theory -> |
| 18771 | 19 |
(((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) * |
| 18954 | 20 |
local_theory |
21 |
val axiomatization: (string * string option * mixfix) list -> |
|
22 |
((bstring * Attrib.src list) * string list) list -> local_theory -> |
|
23 |
(term list * (bstring * thm list) list) * local_theory |
|
24 |
val axiomatization_i: (string * typ option * mixfix) list -> |
|
25 |
((bstring * Attrib.src list) * term list) list -> local_theory -> |
|
26 |
(term list * (bstring * thm list) list) * local_theory |
|
27 |
val definition: |
|
| 18786 | 28 |
((string * string option * mixfix) option * ((string * Attrib.src list) * string)) list -> |
| 18954 | 29 |
local_theory -> (term * (bstring * thm)) list * local_theory |
30 |
val definition_i: |
|
| 18786 | 31 |
((string * typ option * mixfix) option * ((string * Attrib.src list) * term)) list -> |
| 18954 | 32 |
local_theory -> (term * (bstring * thm)) list * local_theory |
| 20784 | 33 |
val abbreviation: Syntax.mode -> ((string * string option * mixfix) option * string) list -> |
| 19080 | 34 |
local_theory -> local_theory |
| 20784 | 35 |
val abbreviation_i: Syntax.mode -> ((string * typ option * mixfix) option * term) list -> |
| 19080 | 36 |
local_theory -> local_theory |
| 20784 | 37 |
val const_syntax: Syntax.mode -> (xstring * mixfix) list -> local_theory -> local_theory |
38 |
val const_syntax_i: Syntax.mode -> (string * mixfix) list -> local_theory -> local_theory |
|
| 20914 | 39 |
val theorems: string -> ((bstring * Attrib.src list) * (thmref * Attrib.src list) list) list |
40 |
-> local_theory -> (bstring * thm list) list * local_theory |
|
41 |
val theorems_i: string -> ((bstring * Attrib.src list) * (thm list * Attrib.src list) list) list |
|
42 |
-> local_theory -> (bstring * thm list) list * local_theory |
|
|
18620
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
43 |
end; |
|
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
44 |
|
|
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
45 |
structure Specification: SPECIFICATION = |
|
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
46 |
struct |
|
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
47 |
|
| 20890 | 48 |
(* diagnostics *) |
49 |
||
50 |
val quiet_mode = ref false; |
|
51 |
||
52 |
fun print_consts _ _ [] = () |
|
53 |
| print_consts ctxt pred cs = |
|
54 |
if ! quiet_mode then () else Pretty.writeln (ProofDisplay.pretty_consts ctxt pred cs); |
|
55 |
||
| 20914 | 56 |
fun present_results ctxt kind res = |
57 |
if ! quiet_mode then () else ProofDisplay.present_results ctxt ((kind, ""), res); |
|
58 |
||
| 19664 | 59 |
|
|
18620
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
60 |
(* prepare specification *) |
|
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
61 |
|
| 18828 | 62 |
fun prep_specification prep_vars prep_propp prep_att raw_vars raw_specs ctxt = |
|
18620
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
63 |
let |
| 18670 | 64 |
val thy = ProofContext.theory_of ctxt; |
|
18620
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
65 |
|
| 18670 | 66 |
val (vars, vars_ctxt) = ctxt |> prep_vars raw_vars; |
67 |
val (xs, params_ctxt) = vars_ctxt |> ProofContext.add_fixes_i vars; |
|
68 |
val ((specs, vs), specs_ctxt) = |
|
| 19585 | 69 |
prep_propp (params_ctxt, map (map (rpair []) o snd) raw_specs) |
|
18620
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
70 |
|> swap |>> map (map fst) |
| 18771 | 71 |
||>> fold_map ProofContext.inferred_param xs; |
|
18620
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
72 |
|
| 18771 | 73 |
val params = vs ~~ map #3 vars; |
|
18620
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
74 |
val names = map (fst o fst) raw_specs; |
| 18670 | 75 |
val atts = map (map (prep_att thy) o snd o fst) raw_specs; |
|
18620
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
76 |
in ((params, (names ~~ atts) ~~ specs), specs_ctxt) end; |
|
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
77 |
|
|
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
78 |
fun read_specification x = |
| 18771 | 79 |
prep_specification ProofContext.read_vars ProofContext.read_propp Attrib.intern_src x; |
|
18620
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
80 |
fun cert_specification x = |
| 18670 | 81 |
prep_specification ProofContext.cert_vars ProofContext.cert_propp (K I) x; |
|
18620
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
82 |
|
|
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
83 |
|
| 18771 | 84 |
(* axiomatization *) |
|
18620
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
85 |
|
| 20890 | 86 |
fun gen_axioms prep raw_vars raw_specs lthy = |
|
18620
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
87 |
let |
| 20890 | 88 |
val (vars, specs) = fst (prep raw_vars raw_specs lthy); |
| 18828 | 89 |
val cs = map fst vars; |
|
18880
b8a1c3cdf739
axiomatization: retrict parameters to occurrences in specs;
wenzelm
parents:
18828
diff
changeset
|
90 |
val spec_frees = member (op =) (fold (fold Term.add_frees o snd) specs []); |
| 18786 | 91 |
|
| 20890 | 92 |
val ((consts, axioms), lthy') = lthy |
93 |
|> LocalTheory.consts spec_frees vars |
|
| 20914 | 94 |
||> fold (fold Variable.fix_frees o snd) specs (* FIXME !? *) |
| 20890 | 95 |
||>> LocalTheory.axioms specs; |
| 18786 | 96 |
|
| 20890 | 97 |
(* FIXME generic target!? *) |
98 |
val hs = map (Term.head_of o #2 o Logic.dest_equals o Thm.prop_of o #2) consts; |
|
99 |
val lthy'' = lthy' |> LocalTheory.theory (Theory.add_finals_i false hs); |
|
100 |
||
101 |
val _ = print_consts lthy' spec_frees cs; |
|
102 |
in ((map #1 consts, axioms), lthy'') end; |
|
| 18786 | 103 |
|
| 18954 | 104 |
val axiomatization = gen_axioms read_specification; |
105 |
val axiomatization_i = gen_axioms cert_specification; |
|
|
18620
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
106 |
|
| 18786 | 107 |
|
108 |
(* definition *) |
|
109 |
||
| 20890 | 110 |
fun gen_defs prep args lthy = |
| 18786 | 111 |
let |
| 20890 | 112 |
fun define (raw_var, (raw_a, raw_prop)) lthy1 = |
| 18786 | 113 |
let |
| 20890 | 114 |
val (vars, [((raw_name, atts), [prop])]) = |
115 |
fst (prep (the_list raw_var) [(raw_a, [raw_prop])] lthy1); |
|
116 |
val (((x, T), rhs), prove) = LocalDefs.derived_def lthy1 true prop; |
|
117 |
val name = Thm.def_name_optional x raw_name; |
|
| 18786 | 118 |
val mx = (case vars of [] => NoSyn | [((x', _), mx)] => |
119 |
if x = x' then mx |
|
120 |
else error ("Head of definition " ^ quote x ^ " differs from declaration " ^ quote x'));
|
|
| 20890 | 121 |
val ((lhs, (_, th)), lthy2) = lthy1 |
122 |
(* |> LocalTheory.def ((x, mx), ((name ^ "_raw", []), rhs)); FIXME *) |
|
123 |
|> LocalTheory.def ((x, mx), ((name, []), rhs)); |
|
124 |
val ((b, [th']), lthy3) = lthy2 |
|
| 20914 | 125 |
|> LocalTheory.note ((name, atts), [prove lthy2 th]); |
| 20890 | 126 |
in (((x, T), (lhs, (b, th'))), LocalTheory.reinit lthy3) end; |
| 18786 | 127 |
|
| 20890 | 128 |
val ((cs, defs), lthy') = lthy |> fold_map define args |>> split_list; |
|
18880
b8a1c3cdf739
axiomatization: retrict parameters to occurrences in specs;
wenzelm
parents:
18828
diff
changeset
|
129 |
val def_frees = member (op =) (fold (Term.add_frees o fst) defs []); |
| 20890 | 130 |
val _ = print_consts lthy' def_frees cs; |
131 |
in (defs, lthy') end; |
|
| 18786 | 132 |
|
| 18954 | 133 |
val definition = gen_defs read_specification; |
134 |
val definition_i = gen_defs cert_specification; |
|
| 18786 | 135 |
|
| 19080 | 136 |
|
137 |
(* abbreviation *) |
|
138 |
||
| 20890 | 139 |
fun gen_abbrevs prep mode args lthy = |
| 19080 | 140 |
let |
| 20890 | 141 |
fun abbrev (raw_var, raw_prop) lthy1 = |
| 19080 | 142 |
let |
|
19372
3ff5f1777743
abbreviation(_i): do not expand abbreviations, do not use derived_def;
wenzelm
parents:
19080
diff
changeset
|
143 |
val ((vars, [(_, [prop])]), _) = |
|
3ff5f1777743
abbreviation(_i): do not expand abbreviations, do not use derived_def;
wenzelm
parents:
19080
diff
changeset
|
144 |
prep (the_list raw_var) [(("", []), [raw_prop])]
|
| 20890 | 145 |
(lthy1 |> ProofContext.expand_abbrevs false); |
146 |
val ((x, T), rhs) = LocalDefs.abs_def (#2 (LocalDefs.cert_def lthy1 prop)); |
|
| 19080 | 147 |
val mx = (case vars of [] => NoSyn | [((x', _), mx)] => |
148 |
if x = x' then mx |
|
149 |
else error ("Head of abbreviation " ^ quote x ^ " differs from declaration " ^ quote x'));
|
|
150 |
in |
|
| 20890 | 151 |
lthy1 |
| 19664 | 152 |
|> LocalTheory.abbrevs mode [((x, mx), rhs)] |
| 19080 | 153 |
|> pair (x, T) |
154 |
end; |
|
155 |
||
| 20890 | 156 |
val (cs, lthy1) = lthy |
| 19544 | 157 |
|> ProofContext.set_syntax_mode mode |
158 |
|> fold_map abbrev args |
|
| 20890 | 159 |
||> ProofContext.restore_syntax_mode lthy; |
160 |
val _ = print_consts lthy1 (K false) cs; |
|
161 |
in lthy1 end; |
|
| 19080 | 162 |
|
163 |
val abbreviation = gen_abbrevs read_specification; |
|
164 |
val abbreviation_i = gen_abbrevs cert_specification; |
|
165 |
||
| 19664 | 166 |
|
167 |
(* const syntax *) |
|
168 |
||
| 20890 | 169 |
fun gen_syntax intern_const mode raw_args lthy = |
170 |
let val args = raw_args |> map (apfst (intern_const (ProofContext.consts_of lthy))) |
|
171 |
in lthy |> LocalTheory.const_syntax mode args end; |
|
| 19664 | 172 |
|
173 |
val const_syntax = gen_syntax Consts.intern; |
|
174 |
val const_syntax_i = gen_syntax (K I); |
|
175 |
||
| 20914 | 176 |
|
177 |
(* theorems *) |
|
178 |
||
179 |
fun gen_theorems prep_thms prep_att kind raw_facts lthy = |
|
180 |
let |
|
181 |
val k = if kind = "" then [] else [Attrib.kind kind]; |
|
182 |
val attrib = prep_att (ProofContext.theory_of lthy); |
|
183 |
val facts = raw_facts |> map (fn ((name, atts), bs) => |
|
184 |
((name, map attrib atts), |
|
185 |
bs |> map (fn (b, more_atts) => (prep_thms lthy b, map attrib more_atts @ k)))); |
|
186 |
val (res, lthy') = lthy |> LocalTheory.notes facts; |
|
187 |
val _ = present_results lthy' kind res; |
|
188 |
in (res, lthy') end; |
|
189 |
||
190 |
val theorems = gen_theorems ProofContext.get_thms Attrib.intern_src; |
|
191 |
val theorems_i = gen_theorems (K I) (K I); |
|
192 |
||
|
18620
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
193 |
end; |