| author | paulson <lp15@cam.ac.uk> | 
| Wed, 24 Feb 2016 16:00:57 +0000 | |
| changeset 62398 | a4b68bf18f8d | 
| parent 61853 | fb7756087101 | 
| child 69988 | 6fa51a36b7f7 | 
| permissions | -rw-r--r-- | 
| 11897 | 1 | (* Title: Pure/Isar/object_logic.ML | 
| 2 | Author: Markus Wenzel, TU Muenchen | |
| 3 | ||
| 4 | Specifics about common object-logics. | |
| 5 | *) | |
| 6 | ||
| 7 | signature OBJECT_LOGIC = | |
| 8 | sig | |
| 59970 | 9 | val get_base_sort: Proof.context -> sort option | 
| 25497 | 10 | val add_base_sort: sort -> theory -> theory | 
| 30344 
10a67c5ddddb
more uniform handling of binding in targets and derived elements;
 wenzelm parents: 
29606diff
changeset | 11 | val add_judgment: binding * typ * mixfix -> theory -> theory | 
| 
10a67c5ddddb
more uniform handling of binding in targets and derived elements;
 wenzelm parents: 
29606diff
changeset | 12 | val add_judgment_cmd: binding * string * mixfix -> theory -> theory | 
| 59970 | 13 | val judgment_name: Proof.context -> string | 
| 14 | val is_judgment: Proof.context -> term -> bool | |
| 15 | val drop_judgment: Proof.context -> term -> term | |
| 16 | val fixed_judgment: Proof.context -> string -> term | |
| 17 | val ensure_propT: Proof.context -> term -> term | |
| 18 | val dest_judgment: Proof.context -> cterm -> cterm | |
| 19 | val judgment_conv: Proof.context -> conv -> conv | |
| 20 | val elim_concl: Proof.context -> thm -> term option | |
| 18728 | 21 | val declare_atomize: attribute | 
| 22 | val declare_rulify: attribute | |
| 59970 | 23 | val atomize_term: Proof.context -> term -> term | 
| 54742 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
53171diff
changeset | 24 | val atomize: Proof.context -> conv | 
| 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
53171diff
changeset | 25 | val atomize_prems: Proof.context -> conv | 
| 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
53171diff
changeset | 26 | val atomize_prems_tac: Proof.context -> int -> tactic | 
| 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
53171diff
changeset | 27 | val full_atomize_tac: Proof.context -> int -> tactic | 
| 59970 | 28 | val rulify_term: Proof.context -> term -> term | 
| 54742 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
53171diff
changeset | 29 | val rulify_tac: Proof.context -> int -> tactic | 
| 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
53171diff
changeset | 30 | val rulify: Proof.context -> thm -> thm | 
| 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
53171diff
changeset | 31 | val rulify_no_asm: Proof.context -> thm -> thm | 
| 18728 | 32 | val rule_format: attribute | 
| 33 | val rule_format_no_asm: attribute | |
| 11897 | 34 | end; | 
| 35 | ||
| 35625 | 36 | structure Object_Logic: OBJECT_LOGIC = | 
| 11897 | 37 | struct | 
| 38 | ||
| 59970 | 39 | (** context data **) | 
| 11897 | 40 | |
| 25497 | 41 | datatype data = Data of | 
| 42 |  {base_sort: sort option,
 | |
| 43 | judgment: string option, | |
| 44 | atomize_rulify: thm list * thm list}; | |
| 45 | ||
| 46 | fun make_data (base_sort, judgment, atomize_rulify) = | |
| 47 |   Data {base_sort = base_sort, judgment = judgment, atomize_rulify = atomize_rulify};
 | |
| 11897 | 48 | |
| 59970 | 49 | structure Data = Generic_Data | 
| 22846 | 50 | ( | 
| 25497 | 51 | type T = data; | 
| 52 | val empty = make_data (NONE, NONE, ([], [])); | |
| 16449 | 53 | val extend = I; | 
| 11897 | 54 | |
| 25497 | 55 | fun merge_opt eq (SOME x, SOME y) = | 
| 56 | if eq (x, y) then SOME x else error "Attempt to merge different object-logics" | |
| 41493 | 57 | | merge_opt _ data = merge_options data; | 
| 11897 | 58 | |
| 33522 | 59 | fun merge | 
| 25497 | 60 |      (Data {base_sort = base_sort1, judgment = judgment1, atomize_rulify = (atomize1, rulify1)},
 | 
| 61 |       Data {base_sort = base_sort2, judgment = judgment2, atomize_rulify = (atomize2, rulify2)}) =
 | |
| 62 | make_data (merge_opt (op =) (base_sort1, base_sort2), merge_opt (op =) (judgment1, judgment2), | |
| 24039 
273698405054
renamed Drule.add/del/merge_rules to Thm.add/del/merge_thms;
 wenzelm parents: 
23602diff
changeset | 63 | (Thm.merge_thms (atomize1, atomize2), Thm.merge_thms (rulify1, rulify2))); | 
| 22846 | 64 | ); | 
| 15801 | 65 | |
| 37216 
3165bc303f66
modernized some structure names, keeping a few legacy aliases;
 wenzelm parents: 
36610diff
changeset | 66 | fun map_data f = Data.map (fn (Data {base_sort, judgment, atomize_rulify}) =>
 | 
| 25497 | 67 | make_data (f (base_sort, judgment, atomize_rulify))); | 
| 68 | ||
| 59970 | 69 | fun get_data ctxt = Data.get (Context.Proof ctxt) |> (fn Data args => args); | 
| 25497 | 70 | |
| 11897 | 71 | |
| 72 | ||
| 73 | (** generic treatment of judgments -- with a single argument only **) | |
| 74 | ||
| 25497 | 75 | (* base_sort *) | 
| 76 | ||
| 77 | val get_base_sort = #base_sort o get_data; | |
| 78 | ||
| 59970 | 79 | fun add_base_sort S = | 
| 80 | (Context.theory_map o map_data) (fn (base_sort, judgment, atomize_rulify) => | |
| 81 | if is_some base_sort then error "Attempt to redeclare object-logic base sort" | |
| 82 | else (SOME S, judgment, atomize_rulify)); | |
| 25497 | 83 | |
| 84 | ||
| 18825 | 85 | (* add judgment *) | 
| 11897 | 86 | |
| 87 | local | |
| 88 | ||
| 30344 
10a67c5ddddb
more uniform handling of binding in targets and derived elements;
 wenzelm parents: 
29606diff
changeset | 89 | fun gen_add_judgment add_consts (b, T, mx) thy = | 
| 61255 
15865e0c5598
eliminated separate type Theory.dep: use typeargs uniformly for consts/types;
 wenzelm parents: 
61246diff
changeset | 90 | let | 
| 
15865e0c5598
eliminated separate type Theory.dep: use typeargs uniformly for consts/types;
 wenzelm parents: 
61246diff
changeset | 91 | val c = Sign.full_name thy b; | 
| 
15865e0c5598
eliminated separate type Theory.dep: use typeargs uniformly for consts/types;
 wenzelm parents: 
61246diff
changeset | 92 | val thy' = thy |> add_consts [(b, T, mx)]; | 
| 
15865e0c5598
eliminated separate type Theory.dep: use typeargs uniformly for consts/types;
 wenzelm parents: 
61246diff
changeset | 93 | val T' = Logic.unvarifyT_global (Sign.the_const_type thy' c); | 
| 
15865e0c5598
eliminated separate type Theory.dep: use typeargs uniformly for consts/types;
 wenzelm parents: 
61246diff
changeset | 94 | in | 
| 
15865e0c5598
eliminated separate type Theory.dep: use typeargs uniformly for consts/types;
 wenzelm parents: 
61246diff
changeset | 95 | thy' | 
| 
15865e0c5598
eliminated separate type Theory.dep: use typeargs uniformly for consts/types;
 wenzelm parents: 
61246diff
changeset | 96 | |> Theory.add_deps_global "" (Theory.const_dep thy' (c, T')) [] | 
| 59970 | 97 | |> (Context.theory_map o map_data) (fn (base_sort, judgment, atomize_rulify) => | 
| 25497 | 98 | if is_some judgment then error "Attempt to redeclare object-logic judgment" | 
| 99 | else (base_sort, SOME c, atomize_rulify)) | |
| 14226 | 100 | end; | 
| 11897 | 101 | |
| 102 | in | |
| 103 | ||
| 56239 | 104 | val add_judgment = gen_add_judgment Sign.add_consts; | 
| 105 | val add_judgment_cmd = gen_add_judgment Sign.add_consts_cmd; | |
| 11897 | 106 | |
| 107 | end; | |
| 108 | ||
| 109 | ||
| 23566 
b65692d4adcd
replaced HOLogic.Trueprop_conv by ObjectLogic.judgment_conv;
 wenzelm parents: 
23540diff
changeset | 110 | (* judgments *) | 
| 11897 | 111 | |
| 59970 | 112 | fun judgment_name ctxt = | 
| 113 | (case #judgment (get_data ctxt) of | |
| 25497 | 114 | SOME name => name | 
| 11897 | 115 |   | _ => raise TERM ("Unknown object-logic judgment", []));
 | 
| 116 | ||
| 59970 | 117 | fun is_judgment ctxt (Const (c, _) $ _) = c = judgment_name ctxt | 
| 11897 | 118 | | is_judgment _ _ = false; | 
| 119 | ||
| 59970 | 120 | fun drop_judgment ctxt (Abs (x, T, t)) = Abs (x, T, drop_judgment ctxt t) | 
| 121 | | drop_judgment ctxt (tm as (Const (c, _) $ t)) = | |
| 122 | if (c = judgment_name ctxt handle TERM _ => false) then t else tm | |
| 11897 | 123 | | drop_judgment _ tm = tm; | 
| 124 | ||
| 59970 | 125 | fun fixed_judgment ctxt x = | 
| 11897 | 126 | let (*be robust wrt. low-level errors*) | 
| 59970 | 127 | val c = judgment_name ctxt; | 
| 24848 | 128 | val aT = TFree (Name.aT, []); | 
| 11897 | 129 | val T = | 
| 59970 | 130 | the_default (aT --> propT) (Sign.const_type (Proof_Context.theory_of ctxt) c) | 
| 60443 | 131 | |> Term.map_type_tvar (fn ((a, _), S) => TFree (a, S)); | 
| 11897 | 132 | val U = Term.domain_type T handle Match => aT; | 
| 133 | in Const (c, T) $ Free (x, U) end; | |
| 134 | ||
| 59970 | 135 | fun ensure_propT ctxt t = | 
| 13376 | 136 | let val T = Term.fastype_of t | 
| 59970 | 137 | in if T = propT then t else Const (judgment_name ctxt, T --> propT) $ t end; | 
| 13376 | 138 | |
| 59970 | 139 | fun dest_judgment ctxt ct = | 
| 140 | if is_judgment ctxt (Thm.term_of ct) | |
| 23586 | 141 | then Thm.dest_arg ct | 
| 142 |   else raise CTERM ("dest_judgment", [ct]);
 | |
| 143 | ||
| 59970 | 144 | fun judgment_conv ctxt cv ct = | 
| 145 | if is_judgment ctxt (Thm.term_of ct) | |
| 23566 
b65692d4adcd
replaced HOLogic.Trueprop_conv by ObjectLogic.judgment_conv;
 wenzelm parents: 
23540diff
changeset | 146 | then Conv.arg_conv cv ct | 
| 
b65692d4adcd
replaced HOLogic.Trueprop_conv by ObjectLogic.judgment_conv;
 wenzelm parents: 
23540diff
changeset | 147 |   else raise CTERM ("judgment_conv", [ct]);
 | 
| 
b65692d4adcd
replaced HOLogic.Trueprop_conv by ObjectLogic.judgment_conv;
 wenzelm parents: 
23540diff
changeset | 148 | |
| 11897 | 149 | |
| 19261 | 150 | (* elimination rules *) | 
| 151 | ||
| 59970 | 152 | fun elim_concl ctxt rule = | 
| 19261 | 153 | let | 
| 154 | val concl = Thm.concl_of rule; | |
| 59970 | 155 | val C = drop_judgment ctxt concl; | 
| 19261 | 156 | in | 
| 41581 
72a02e3dec7e
clarified pretty_statement: more robust treatment of fixes and conclusion of elimination (e.g. for classical rule);
 wenzelm parents: 
41493diff
changeset | 157 | if Term.is_Var C andalso | 
| 19261 | 158 | exists (fn prem => concl aconv Logic.strip_assums_concl prem) (Thm.prems_of rule) | 
| 41581 
72a02e3dec7e
clarified pretty_statement: more robust treatment of fixes and conclusion of elimination (e.g. for classical rule);
 wenzelm parents: 
41493diff
changeset | 159 | then SOME C else NONE | 
| 19261 | 160 | end; | 
| 161 | ||
| 162 | ||
| 11897 | 163 | |
| 164 | (** treatment of meta-level connectives **) | |
| 165 | ||
| 166 | (* maintain rules *) | |
| 167 | ||
| 25497 | 168 | val get_atomize = #1 o #atomize_rulify o get_data; | 
| 169 | val get_rulify = #2 o #atomize_rulify o get_data; | |
| 11897 | 170 | |
| 25497 | 171 | fun add_atomize th = map_data (fn (base_sort, judgment, (atomize, rulify)) => | 
| 61092 | 172 | (base_sort, judgment, (Thm.add_thm (Thm.trim_context th) atomize, rulify))); | 
| 25497 | 173 | |
| 174 | fun add_rulify th = map_data (fn (base_sort, judgment, (atomize, rulify)) => | |
| 61092 | 175 | (base_sort, judgment, (atomize, Thm.add_thm (Thm.trim_context th) rulify))); | 
| 11897 | 176 | |
| 59970 | 177 | val declare_atomize = Thm.declaration_attribute add_atomize; | 
| 178 | val declare_rulify = Thm.declaration_attribute add_rulify; | |
| 22846 | 179 | |
| 59970 | 180 | val _ = Theory.setup (fold (Context.theory_map o add_rulify) Drule.norm_hhf_eqs); | 
| 11897 | 181 | |
| 182 | ||
| 183 | (* atomize *) | |
| 184 | ||
| 59970 | 185 | fun atomize_term ctxt = | 
| 186 | drop_judgment ctxt o | |
| 187 | Raw_Simplifier.rewrite_term (Proof_Context.theory_of ctxt) (get_atomize ctxt) []; | |
| 12729 | 188 | |
| 59970 | 189 | fun atomize ctxt = Raw_Simplifier.rewrite ctxt true (get_atomize ctxt); | 
| 14743 | 190 | |
| 54742 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
53171diff
changeset | 191 | fun atomize_prems ctxt ct = | 
| 23602 
361e9c3a5e3a
tuned interfaces: atomize, atomize_prems, atomize_prems_tac;
 wenzelm parents: 
23586diff
changeset | 192 | if Logic.has_meta_prems (Thm.term_of ct) then | 
| 54742 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
53171diff
changeset | 193 | Conv.params_conv ~1 (Conv.prems_conv ~1 o atomize) ctxt ct | 
| 23602 
361e9c3a5e3a
tuned interfaces: atomize, atomize_prems, atomize_prems_tac;
 wenzelm parents: 
23586diff
changeset | 194 | else Conv.all_conv ct; | 
| 11897 | 195 | |
| 54742 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
53171diff
changeset | 196 | val atomize_prems_tac = CONVERSION o atomize_prems; | 
| 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
53171diff
changeset | 197 | val full_atomize_tac = CONVERSION o atomize; | 
| 11897 | 198 | |
| 199 | ||
| 200 | (* rulify *) | |
| 201 | ||
| 59970 | 202 | fun rulify_term ctxt = | 
| 203 | Raw_Simplifier.rewrite_term (Proof_Context.theory_of ctxt) (get_rulify ctxt) []; | |
| 204 | ||
| 205 | fun rulify_tac ctxt = rewrite_goal_tac ctxt (get_rulify ctxt); | |
| 18807 | 206 | |
| 54742 
7a86358a3c0b
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
 wenzelm parents: 
53171diff
changeset | 207 | fun gen_rulify full ctxt = | 
| 59970 | 208 | Conv.fconv_rule (Raw_Simplifier.rewrite ctxt full (get_rulify ctxt)) | 
| 60822 | 209 | #> Variable.gen_all ctxt | 
| 59647 
c6f413b660cf
clarified Drule.gen_all: observe context more carefully;
 wenzelm parents: 
56239diff
changeset | 210 | #> Thm.strip_shyps | 
| 
c6f413b660cf
clarified Drule.gen_all: observe context more carefully;
 wenzelm parents: 
56239diff
changeset | 211 | #> Drule.zero_var_indexes; | 
| 11897 | 212 | |
| 213 | val rulify = gen_rulify true; | |
| 214 | val rulify_no_asm = gen_rulify false; | |
| 215 | ||
| 61853 
fb7756087101
rule_attribute and declaration_attribute implicitly support abstract closure, but mixed_attribute implementations need to be aware of Thm.is_free_dummy;
 wenzelm parents: 
61255diff
changeset | 216 | val rule_format = Thm.rule_attribute [] (rulify o Context.proof_of); | 
| 
fb7756087101
rule_attribute and declaration_attribute implicitly support abstract closure, but mixed_attribute implementations need to be aware of Thm.is_free_dummy;
 wenzelm parents: 
61255diff
changeset | 217 | val rule_format_no_asm = Thm.rule_attribute [] (rulify_no_asm o Context.proof_of); | 
| 11897 | 218 | |
| 219 | end; |