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