author | wenzelm |
Wed, 26 Sep 2007 19:17:59 +0200 | |
changeset 24724 | 0df74bb87a13 |
parent 24676 | 63eaef3207e1 |
child 24734 | ff617b6711f4 |
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 |
|
21036 | 5 |
Derived 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 -> |
24724
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
14 |
((string * Attrib.src list) * string list) list list -> Proof.context -> |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
15 |
(((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) * |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
16 |
Proof.context |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
17 |
val check_specification: (string * typ option * mixfix) list -> |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
18 |
((string * Attrib.src list) * term list) list list -> Proof.context -> |
18771 | 19 |
(((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) * |
24724
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
20 |
Proof.context |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
21 |
val read_free_specification: (string * string option * mixfix) list -> |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
22 |
((string * Attrib.src list) * string list) list -> Proof.context -> |
18771 | 23 |
(((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) * |
24724
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
24 |
Proof.context |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
25 |
val check_free_specification: (string * typ option * mixfix) list -> |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
26 |
((string * Attrib.src list) * term list) list -> Proof.context -> |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
27 |
(((string * typ) * mixfix) list * ((string * Attrib.src list) * term list) list) * |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
28 |
Proof.context |
18954 | 29 |
val axiomatization: (string * string option * mixfix) list -> |
30 |
((bstring * Attrib.src list) * string list) list -> local_theory -> |
|
31 |
(term list * (bstring * thm list) list) * local_theory |
|
32 |
val axiomatization_i: (string * typ option * mixfix) list -> |
|
33 |
((bstring * Attrib.src list) * term list) list -> local_theory -> |
|
34 |
(term list * (bstring * thm list) list) * local_theory |
|
35 |
val definition: |
|
21705 | 36 |
(string * string option * mixfix) option * ((string * Attrib.src list) * string) -> |
37 |
local_theory -> (term * (bstring * thm)) * local_theory |
|
18954 | 38 |
val definition_i: |
21705 | 39 |
(string * typ option * mixfix) option * ((string * Attrib.src list) * term) -> |
40 |
local_theory -> (term * (bstring * thm)) * local_theory |
|
41 |
val abbreviation: Syntax.mode -> (string * string option * mixfix) option * string -> |
|
21532 | 42 |
local_theory -> local_theory |
21705 | 43 |
val abbreviation_i: Syntax.mode -> (string * typ option * mixfix) option * term -> |
21532 | 44 |
local_theory -> local_theory |
21206
2af4c7b3f7ef
replaced const_syntax by notation, which operates on terms;
wenzelm
parents:
21036
diff
changeset
|
45 |
val notation: Syntax.mode -> (string * mixfix) list -> local_theory -> local_theory |
2af4c7b3f7ef
replaced const_syntax by notation, which operates on terms;
wenzelm
parents:
21036
diff
changeset
|
46 |
val notation_i: Syntax.mode -> (term * mixfix) list -> local_theory -> local_theory |
20914 | 47 |
val theorems: string -> ((bstring * Attrib.src list) * (thmref * Attrib.src list) list) list |
48 |
-> local_theory -> (bstring * thm list) list * local_theory |
|
49 |
val theorems_i: string -> ((bstring * Attrib.src list) * (thm list * Attrib.src list) list) list |
|
50 |
-> local_theory -> (bstring * thm list) list * local_theory |
|
21036 | 51 |
val theorem: string -> Method.text option -> (thm list list -> local_theory -> local_theory) -> |
52 |
string * Attrib.src list -> Element.context Locale.element list -> Element.statement -> |
|
24452
93b113c5ac33
- theorem(_i) now also takes "interactive" flag as argument
berghofe
parents:
24219
diff
changeset
|
53 |
bool -> local_theory -> Proof.state |
21036 | 54 |
val theorem_i: string -> Method.text option -> (thm list list -> local_theory -> local_theory) -> |
55 |
string * Attrib.src list -> Element.context_i Locale.element list -> Element.statement_i -> |
|
24452
93b113c5ac33
- theorem(_i) now also takes "interactive" flag as argument
berghofe
parents:
24219
diff
changeset
|
56 |
bool -> local_theory -> Proof.state |
93b113c5ac33
- theorem(_i) now also takes "interactive" flag as argument
berghofe
parents:
24219
diff
changeset
|
57 |
val add_theorem_hook: (bool -> Proof.state -> Proof.state) -> Context.generic -> Context.generic |
18620
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
58 |
end; |
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
59 |
|
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
60 |
structure Specification: SPECIFICATION = |
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
61 |
struct |
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
62 |
|
21230
abfdce60b371
theorem statements: incorporate Obtain.statement, tuned;
wenzelm
parents:
21206
diff
changeset
|
63 |
|
20890 | 64 |
(* diagnostics *) |
65 |
||
66 |
val quiet_mode = ref false; |
|
67 |
||
68 |
fun print_consts _ _ [] = () |
|
69 |
| print_consts ctxt pred cs = |
|
70 |
if ! quiet_mode then () else Pretty.writeln (ProofDisplay.pretty_consts ctxt pred cs); |
|
71 |
||
21036 | 72 |
fun present_results ctxt res = |
73 |
if ! quiet_mode then () else ProofDisplay.present_results ctxt res; |
|
74 |
||
75 |
fun present_results' ctxt kind res = present_results ctxt ((kind, ""), res); |
|
20914 | 76 |
|
19664 | 77 |
|
18620
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
78 |
(* prepare specification *) |
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
79 |
|
24724
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
80 |
local |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
81 |
|
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
82 |
fun close_forms ctxt i xs As = |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
83 |
let |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
84 |
fun add_free (Free (x, _)) = if Variable.is_fixed ctxt x then I else insert (op =) x |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
85 |
| add_free _ = I; |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
86 |
|
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
87 |
val commons = map #1 xs; |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
88 |
val _ = |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
89 |
(case duplicates (op =) commons of [] => () |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
90 |
| dups => error ("Duplicate local variables " ^ commas_quote dups)); |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
91 |
val frees = rev ((fold o fold_aterms) add_free As (rev commons)); |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
92 |
val types = map (TypeInfer.param i o rpair []) (Name.invents Name.context "'a" (length frees)); |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
93 |
val uniform_typing = the o AList.lookup (op =) (frees ~~ types); |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
94 |
|
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
95 |
fun abs_body lev y (Abs (x, T, b)) = Abs (x, T, abs_body (lev + 1) y b) |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
96 |
| abs_body lev y (t $ u) = abs_body lev y t $ abs_body lev y u |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
97 |
| abs_body lev y (t as Free (x, T)) = |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
98 |
if x = y then TypeInfer.constrain (uniform_typing x) (TypeInfer.constrain T (Bound lev)) |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
99 |
else t |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
100 |
| abs_body _ _ a = a; |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
101 |
fun close (y, U) B = |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
102 |
let val B' = abs_body 0 y (Term.incr_boundvars 1 B) |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
103 |
in if Term.loose_bvar1 (B', 0) then Term.all dummyT $ Abs (y, U, B') else B end; |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
104 |
fun close_form A = |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
105 |
let |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
106 |
val occ_frees = rev (fold_aterms add_free A []); |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
107 |
val bounds = xs @ map (rpair dummyT) (subtract (op =) commons occ_frees); |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
108 |
in fold_rev close bounds A end; |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
109 |
in map close_form As end; |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
110 |
|
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
111 |
fun prep_spec prep_vars parse_prop prep_att do_close raw_vars raw_specss ctxt = |
18620
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
112 |
let |
18670 | 113 |
val thy = ProofContext.theory_of ctxt; |
18620
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
114 |
|
18670 | 115 |
val (vars, vars_ctxt) = ctxt |> prep_vars raw_vars; |
116 |
val (xs, params_ctxt) = vars_ctxt |> ProofContext.add_fixes_i vars; |
|
18620
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
117 |
|
24724
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
118 |
val Asss = (map o map) (map (parse_prop params_ctxt) o snd) raw_specss; |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
119 |
val idx = (fold o fold o fold) Term.maxidx_term Asss ~1 + 1; |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
120 |
val specs = |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
121 |
(if do_close then |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
122 |
#1 (fold_map (fn Ass => fn i => (burrow (close_forms params_ctxt i []) Ass, i + 1)) Asss idx) |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
123 |
else Asss) |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
124 |
|> flat |> burrow (Syntax.check_props params_ctxt); |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
125 |
val specs_ctxt = params_ctxt |> (fold o fold) Variable.declare_term specs; |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
126 |
|
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
127 |
val vs = specs_ctxt |> fold_map ProofContext.inferred_param xs |> fst; |
18771 | 128 |
val params = vs ~~ map #3 vars; |
24724
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
129 |
val name_atts = map (fn ((name, atts), _) => (name, map (prep_att thy) atts)) (flat raw_specss); |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
130 |
in ((params, name_atts ~~ specs), specs_ctxt) end; |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
131 |
|
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
132 |
fun read_spec x = prep_spec ProofContext.read_vars Syntax.parse_prop Attrib.intern_src x; |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
133 |
fun check_spec x = prep_spec ProofContext.cert_vars (K I) (K I) x; |
18620
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
134 |
|
24724
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
135 |
in |
21370
d9dd7b4e5e69
replaced Variable.fix_frees by Variable.auto_fixes (depends on body mode);
wenzelm
parents:
21359
diff
changeset
|
136 |
|
24724
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
137 |
fun read_specification x = read_spec true x; |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
138 |
fun check_specification x = check_spec true x; |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
139 |
fun read_free_specification vars specs = read_spec false vars [specs]; |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
140 |
fun check_free_specification vars specs = check_spec false vars [specs]; |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
141 |
|
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
142 |
end; |
18620
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
143 |
|
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
144 |
|
18771 | 145 |
(* axiomatization *) |
18620
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
146 |
|
20890 | 147 |
fun gen_axioms prep raw_vars raw_specs lthy = |
18620
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
148 |
let |
24724
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
149 |
val (vars, specs) = fst (prep raw_vars [raw_specs] lthy); |
18828 | 150 |
val cs = map fst vars; |
24724
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
151 |
val spec_frees = member (op =) ((fold (fold Term.add_frees o snd) specs [])); |
18786 | 152 |
|
24724
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
153 |
val ((constants, axioms), lthy') = lthy |
20890 | 154 |
|> LocalTheory.consts spec_frees vars |
21435 | 155 |
||>> LocalTheory.axioms Thm.axiomK specs; |
24724
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
156 |
val consts = map #1 constants; |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
157 |
val consts' = map (Morphism.term (LocalTheory.target_morphism lthy')) consts; |
18786 | 158 |
|
20890 | 159 |
(* FIXME generic target!? *) |
24724
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
160 |
val hs = map (Term.head_of o #2 o Logic.dest_equals o Thm.prop_of o #2) constants; |
20890 | 161 |
val lthy'' = lthy' |> LocalTheory.theory (Theory.add_finals_i false hs); |
162 |
||
24724
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
163 |
val _ = print_consts lthy' (member (op =) (fold Term.add_frees consts' [])) cs; |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
164 |
in ((consts, axioms), lthy'') end; |
18786 | 165 |
|
18954 | 166 |
val axiomatization = gen_axioms read_specification; |
24724
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
167 |
val axiomatization_i = gen_axioms check_specification; |
18620
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
168 |
|
18786 | 169 |
|
170 |
(* definition *) |
|
171 |
||
21705 | 172 |
fun gen_def prep (raw_var, (raw_a, raw_prop)) lthy = |
18786 | 173 |
let |
21705 | 174 |
val (vars, [((raw_name, atts), [prop])]) = |
175 |
fst (prep (the_list raw_var) [(raw_a, [raw_prop])] lthy); |
|
176 |
val (((x, T), rhs), prove) = LocalDefs.derived_def lthy true prop; |
|
177 |
val name = Thm.def_name_optional x raw_name; |
|
178 |
val mx = (case vars of [] => NoSyn | [((x', _), mx)] => |
|
179 |
if x = x' then mx |
|
180 |
else error ("Head of definition " ^ quote x ^ " differs from declaration " ^ quote x')); |
|
181 |
val ((lhs, (_, th)), lthy2) = lthy |
|
22923 | 182 |
|> LocalTheory.def Thm.definitionK ((x, mx), ((name ^ "_raw", []), rhs)); |
21705 | 183 |
val ((b, [th']), lthy3) = lthy2 |
22744
5cbe966d67a2
Isar definitions are now added explicitly to code theorem table
haftmann
parents:
21860
diff
changeset
|
184 |
|> LocalTheory.note Thm.definitionK |
24624
b8383b1bbae3
distinction between regular and default code theorems
haftmann
parents:
24557
diff
changeset
|
185 |
((name, Code.add_default_func_attr :: atts), [prove lthy2 th]); |
18786 | 186 |
|
21705 | 187 |
val lhs' = Morphism.term (LocalTheory.target_morphism lthy3) lhs; |
188 |
val _ = print_consts lthy3 (member (op =) (Term.add_frees lhs' [])) [(x, T)]; |
|
189 |
in ((lhs, (b, th')), lthy3) end; |
|
18786 | 190 |
|
24724
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
191 |
val definition = gen_def read_free_specification; |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
192 |
val definition_i = gen_def check_free_specification; |
18786 | 193 |
|
19080 | 194 |
|
195 |
(* abbreviation *) |
|
196 |
||
21705 | 197 |
fun gen_abbrev prep mode (raw_var, raw_prop) lthy = |
19080 | 198 |
let |
21705 | 199 |
val ((vars, [(_, [prop])]), _) = |
200 |
prep (the_list raw_var) [(("", []), [raw_prop])] |
|
24676 | 201 |
(lthy |> ProofContext.set_mode ProofContext.mode_abbrev); |
21705 | 202 |
val ((x, T), rhs) = LocalDefs.abs_def (#2 (LocalDefs.cert_def lthy prop)); |
203 |
val mx = (case vars of [] => NoSyn | [((y, _), mx)] => |
|
204 |
if x = y then mx |
|
205 |
else error ("Head of abbreviation " ^ quote x ^ " differs from declaration " ^ quote y)); |
|
206 |
val lthy' = lthy |
|
207 |
|> ProofContext.set_syntax_mode mode (* FIXME !? *) |
|
21793 | 208 |
|> LocalTheory.abbrev mode ((x, mx), rhs) |> snd |
21705 | 209 |
|> ProofContext.restore_syntax_mode lthy; |
24724
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
210 |
|
21705 | 211 |
val _ = print_consts lthy' (K false) [(x, T)] |
21532 | 212 |
in lthy' end; |
19080 | 213 |
|
24724
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
214 |
val abbreviation = gen_abbrev read_free_specification; |
0df74bb87a13
read/check_specification: proper type inference across multiple sections, result is in closed form;
wenzelm
parents:
24676
diff
changeset
|
215 |
val abbreviation_i = gen_abbrev check_free_specification; |
19080 | 216 |
|
19664 | 217 |
|
21230
abfdce60b371
theorem statements: incorporate Obtain.statement, tuned;
wenzelm
parents:
21206
diff
changeset
|
218 |
(* notation *) |
19664 | 219 |
|
21206
2af4c7b3f7ef
replaced const_syntax by notation, which operates on terms;
wenzelm
parents:
21036
diff
changeset
|
220 |
fun gen_notation prep_const mode args lthy = |
21746 | 221 |
lthy |> LocalTheory.notation mode (map (apfst (prep_const lthy)) args); |
19664 | 222 |
|
21206
2af4c7b3f7ef
replaced const_syntax by notation, which operates on terms;
wenzelm
parents:
21036
diff
changeset
|
223 |
val notation = gen_notation ProofContext.read_const; |
2af4c7b3f7ef
replaced const_syntax by notation, which operates on terms;
wenzelm
parents:
21036
diff
changeset
|
224 |
val notation_i = gen_notation (K I); |
19664 | 225 |
|
20914 | 226 |
|
21036 | 227 |
(* fact statements *) |
20914 | 228 |
|
229 |
fun gen_theorems prep_thms prep_att kind raw_facts lthy = |
|
230 |
let |
|
231 |
val attrib = prep_att (ProofContext.theory_of lthy); |
|
232 |
val facts = raw_facts |> map (fn ((name, atts), bs) => |
|
233 |
((name, map attrib atts), |
|
21435 | 234 |
bs |> map (fn (b, more_atts) => (prep_thms lthy b, map attrib more_atts)))); |
235 |
val (res, lthy') = lthy |> LocalTheory.notes kind facts; |
|
21036 | 236 |
val _ = present_results' lthy' kind res; |
20914 | 237 |
in (res, lthy') end; |
238 |
||
239 |
val theorems = gen_theorems ProofContext.get_thms Attrib.intern_src; |
|
240 |
val theorems_i = gen_theorems (K I) (K I); |
|
241 |
||
21036 | 242 |
|
21230
abfdce60b371
theorem statements: incorporate Obtain.statement, tuned;
wenzelm
parents:
21206
diff
changeset
|
243 |
(* complex goal statements *) |
21036 | 244 |
|
245 |
local |
|
246 |
||
21450 | 247 |
fun subtract_prems ctxt1 ctxt2 = |
248 |
Library.drop (length (Assumption.prems_of ctxt1), Assumption.prems_of ctxt2); |
|
249 |
||
21236 | 250 |
fun prep_statement prep_att prep_stmt elems concl ctxt = |
251 |
(case concl of |
|
252 |
Element.Shows shows => |
|
21230
abfdce60b371
theorem statements: incorporate Obtain.statement, tuned;
wenzelm
parents:
21206
diff
changeset
|
253 |
let |
21450 | 254 |
val (_, loc_ctxt, elems_ctxt, propp) = prep_stmt elems (map snd shows) ctxt; |
255 |
val prems = subtract_prems loc_ctxt elems_ctxt; |
|
256 |
val stmt = Attrib.map_specs prep_att (map fst shows ~~ propp); |
|
21370
d9dd7b4e5e69
replaced Variable.fix_frees by Variable.auto_fixes (depends on body mode);
wenzelm
parents:
21359
diff
changeset
|
257 |
val goal_ctxt = fold (fold (Variable.auto_fixes o fst)) propp elems_ctxt; |
21450 | 258 |
in ((prems, stmt, []), goal_ctxt) end |
21236 | 259 |
| Element.Obtains obtains => |
21230
abfdce60b371
theorem statements: incorporate Obtain.statement, tuned;
wenzelm
parents:
21206
diff
changeset
|
260 |
let |
21236 | 261 |
val case_names = obtains |> map_index |
262 |
(fn (i, ("", _)) => string_of_int (i + 1) | (_, (name, _)) => name); |
|
263 |
val constraints = obtains |> map (fn (_, (vars, _)) => |
|
264 |
Locale.Elem (Element.Constrains |
|
265 |
(vars |> map_filter (fn (x, SOME T) => SOME (x, T) | _ => NONE)))); |
|
21036 | 266 |
|
21236 | 267 |
val raw_propp = obtains |> map (fn (_, (_, props)) => map (rpair []) props); |
21450 | 268 |
val (_, loc_ctxt, elems_ctxt, propp) = prep_stmt (elems @ constraints) raw_propp ctxt; |
21236 | 269 |
|
270 |
val thesis = ObjectLogic.fixed_judgment (ProofContext.theory_of ctxt) AutoBind.thesisN; |
|
21036 | 271 |
|
21236 | 272 |
fun assume_case ((name, (vars, _)), asms) ctxt' = |
273 |
let |
|
274 |
val xs = map fst vars; |
|
275 |
val props = map fst asms; |
|
276 |
val (parms, _) = ctxt' |
|
277 |
|> fold Variable.declare_term props |
|
278 |
|> fold_map ProofContext.inferred_param xs; |
|
279 |
val asm = Term.list_all_free (parms, Logic.list_implies (props, thesis)); |
|
280 |
in |
|
281 |
ctxt' |> (snd o ProofContext.add_fixes_i (map (fn x => (x, NONE, NoSyn)) xs)); |
|
21370
d9dd7b4e5e69
replaced Variable.fix_frees by Variable.auto_fixes (depends on body mode);
wenzelm
parents:
21359
diff
changeset
|
282 |
ctxt' |> Variable.auto_fixes asm |
21236 | 283 |
|> ProofContext.add_assms_i Assumption.assume_export |
284 |
[((name, [ContextRules.intro_query NONE]), [(asm, [])])] |
|
285 |
|>> (fn [(_, [th])] => th) |
|
286 |
end; |
|
287 |
||
21658 | 288 |
val atts = map (Attrib.internal o K) |
21236 | 289 |
[RuleCases.consumes (~ (length obtains)), RuleCases.case_names case_names]; |
21450 | 290 |
val prems = subtract_prems loc_ctxt elems_ctxt; |
21236 | 291 |
val stmt = [(("", atts), [(thesis, [])])]; |
292 |
||
293 |
val (facts, goal_ctxt) = elems_ctxt |
|
294 |
|> (snd o ProofContext.add_fixes_i [(AutoBind.thesisN, NONE, NoSyn)]) |
|
295 |
|> fold_map assume_case (obtains ~~ propp) |
|
21435 | 296 |
|-> (fn ths => ProofContext.note_thmss_i Thm.assumptionK |
297 |
[((Obtain.thatN, []), [(ths, [])])] #> #2 #> pair ths); |
|
21450 | 298 |
in ((prems, stmt, facts), goal_ctxt) end); |
21036 | 299 |
|
24452
93b113c5ac33
- theorem(_i) now also takes "interactive" flag as argument
berghofe
parents:
24219
diff
changeset
|
300 |
structure TheoremHook = GenericDataFun |
93b113c5ac33
- theorem(_i) now also takes "interactive" flag as argument
berghofe
parents:
24219
diff
changeset
|
301 |
( |
93b113c5ac33
- theorem(_i) now also takes "interactive" flag as argument
berghofe
parents:
24219
diff
changeset
|
302 |
type T = ((bool -> Proof.state -> Proof.state) * stamp) list; |
93b113c5ac33
- theorem(_i) now also takes "interactive" flag as argument
berghofe
parents:
24219
diff
changeset
|
303 |
val empty = []; |
93b113c5ac33
- theorem(_i) now also takes "interactive" flag as argument
berghofe
parents:
24219
diff
changeset
|
304 |
val extend = I; |
24464 | 305 |
fun merge _ hooks : T = Library.merge (eq_snd op =) hooks; |
24452
93b113c5ac33
- theorem(_i) now also takes "interactive" flag as argument
berghofe
parents:
24219
diff
changeset
|
306 |
); |
93b113c5ac33
- theorem(_i) now also takes "interactive" flag as argument
berghofe
parents:
24219
diff
changeset
|
307 |
|
21036 | 308 |
fun gen_theorem prep_att prep_stmt |
24452
93b113c5ac33
- theorem(_i) now also takes "interactive" flag as argument
berghofe
parents:
24219
diff
changeset
|
309 |
kind before_qed after_qed (name, raw_atts) raw_elems raw_concl int lthy0 = |
21036 | 310 |
let |
21860 | 311 |
val _ = LocalTheory.affirm lthy0; |
21036 | 312 |
val thy = ProofContext.theory_of lthy0; |
21206
2af4c7b3f7ef
replaced const_syntax by notation, which operates on terms;
wenzelm
parents:
21036
diff
changeset
|
313 |
|
21036 | 314 |
val (loc, ctxt, lthy) = |
315 |
(case (TheoryTarget.peek lthy0, exists (fn Locale.Expr _ => true | _ => false) raw_elems) of |
|
316 |
(SOME loc, true) => (* workaround non-modularity of in/includes *) (* FIXME *) |
|
21275 | 317 |
(SOME loc, ProofContext.init thy, LocalTheory.restore lthy0) |
21036 | 318 |
| _ => (NONE, lthy0, lthy0)); |
319 |
||
21435 | 320 |
val attrib = prep_att thy; |
321 |
val atts = map attrib raw_atts; |
|
322 |
||
21532 | 323 |
val elems = raw_elems |> map (Locale.map_elem (Element.map_ctxt_attrib attrib)); |
21450 | 324 |
val ((prems, stmt, facts), goal_ctxt) = |
325 |
prep_statement attrib (prep_stmt loc) elems raw_concl ctxt; |
|
21036 | 326 |
|
327 |
fun after_qed' results goal_ctxt' = |
|
21602 | 328 |
let val results' = |
329 |
burrow (map Goal.norm_result o ProofContext.export goal_ctxt' lthy) results |
|
330 |
in |
|
21036 | 331 |
lthy |
21435 | 332 |
|> LocalTheory.notes kind (map2 (fn (a, _) => fn ths => (a, [(ths, [])])) stmt results') |
21036 | 333 |
|> (fn (res, lthy') => |
334 |
(present_results lthy' ((kind, name), res); |
|
21435 | 335 |
if name = "" andalso null atts then lthy' |
336 |
else #2 (LocalTheory.notes kind [((name, atts), [(maps #2 res, [])])] lthy'))) |
|
21036 | 337 |
|> after_qed results' |
338 |
end; |
|
339 |
in |
|
340 |
goal_ctxt |
|
21450 | 341 |
|> ProofContext.note_thmss_i Thm.assumptionK [((AutoBind.assmsN, []), [(prems, [])])] |
342 |
|> snd |
|
21435 | 343 |
|> Proof.theorem_i before_qed after_qed' (map snd stmt) |
24557 | 344 |
|> Proof.refine_insert facts |
24542 | 345 |
|> Library.apply (map (fn (f, _) => f int) (rev (TheoremHook.get (Context.Proof goal_ctxt)))) |
21036 | 346 |
end; |
347 |
||
21230
abfdce60b371
theorem statements: incorporate Obtain.statement, tuned;
wenzelm
parents:
21206
diff
changeset
|
348 |
in |
abfdce60b371
theorem statements: incorporate Obtain.statement, tuned;
wenzelm
parents:
21206
diff
changeset
|
349 |
|
21036 | 350 |
val theorem = gen_theorem Attrib.intern_src Locale.read_context_statement_i; |
351 |
val theorem_i = gen_theorem (K I) Locale.cert_context_statement; |
|
352 |
||
24452
93b113c5ac33
- theorem(_i) now also takes "interactive" flag as argument
berghofe
parents:
24219
diff
changeset
|
353 |
fun add_theorem_hook f = TheoremHook.map (cons (f, stamp ())); |
93b113c5ac33
- theorem(_i) now also takes "interactive" flag as argument
berghofe
parents:
24219
diff
changeset
|
354 |
|
18620
fc8b5f275359
Theory specifications --- with type-inference, but no internal polymorphism.
wenzelm
parents:
diff
changeset
|
355 |
end; |
21036 | 356 |
|
357 |
end; |