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