src/Pure/more_thm.ML
author wenzelm
Thu Sep 02 00:48:07 2010 +0200 (2010-09-02)
changeset 38980 af73cf0dc31f
parent 36744 6e1f3d609a68
child 39133 70d3915c92f0
permissions -rw-r--r--
turned show_question_marks into proper configuration option;
show_question_marks only affects regular type/term pretty printing, not raw Term.string_of_vname;
tuned;
     1 (*  Title:      Pure/more_thm.ML
     2     Author:     Makarius
     3 
     4 Further operations on type ctyp/cterm/thm, outside the inference kernel.
     5 *)
     6 
     7 infix aconvc;
     8 
     9 signature BASIC_THM =
    10 sig
    11   include BASIC_THM
    12   structure Ctermtab: TABLE
    13   structure Thmtab: TABLE
    14   val aconvc: cterm * cterm -> bool
    15 end;
    16 
    17 signature THM =
    18 sig
    19   include THM
    20   structure Ctermtab: TABLE
    21   structure Thmtab: TABLE
    22   val aconvc: cterm * cterm -> bool
    23   val add_cterm_frees: cterm -> cterm list -> cterm list
    24   val all_name: string * cterm -> cterm -> cterm
    25   val all: cterm -> cterm -> cterm
    26   val mk_binop: cterm -> cterm -> cterm -> cterm
    27   val dest_binop: cterm -> cterm * cterm
    28   val dest_implies: cterm -> cterm * cterm
    29   val dest_equals: cterm -> cterm * cterm
    30   val dest_equals_lhs: cterm -> cterm
    31   val dest_equals_rhs: cterm -> cterm
    32   val lhs_of: thm -> cterm
    33   val rhs_of: thm -> cterm
    34   val thm_ord: thm * thm -> order
    35   val cterm_cache: (cterm -> 'a) -> cterm -> 'a
    36   val thm_cache: (thm -> 'a) -> thm -> 'a
    37   val is_reflexive: thm -> bool
    38   val eq_thm: thm * thm -> bool
    39   val eq_thms: thm list * thm list -> bool
    40   val eq_thm_thy: thm * thm -> bool
    41   val eq_thm_prop: thm * thm -> bool
    42   val equiv_thm: thm * thm -> bool
    43   val class_triv: theory -> class -> thm
    44   val of_sort: ctyp * sort -> thm list
    45   val check_shyps: sort list -> thm -> thm
    46   val is_dummy: thm -> bool
    47   val plain_prop_of: thm -> term
    48   val add_thm: thm -> thm list -> thm list
    49   val del_thm: thm -> thm list -> thm list
    50   val merge_thms: thm list * thm list -> thm list
    51   val full_rules: thm Item_Net.T
    52   val intro_rules: thm Item_Net.T
    53   val elim_rules: thm Item_Net.T
    54   val elim_implies: thm -> thm -> thm
    55   val forall_elim_var: int -> thm -> thm
    56   val forall_elim_vars: int -> thm -> thm
    57   val certify_inst: theory ->
    58     ((indexname * sort) * typ) list * ((indexname * typ) * term) list ->
    59     (ctyp * ctyp) list * (cterm * cterm) list
    60   val certify_instantiate:
    61     ((indexname * sort) * typ) list * ((indexname * typ) * term) list -> thm -> thm
    62   val forall_intr_frees: thm -> thm
    63   val unvarify_global: thm -> thm
    64   val close_derivation: thm -> thm
    65   val add_axiom: binding * term -> theory -> (string * thm) * theory
    66   val add_def: bool -> bool -> binding * term -> theory -> (string * thm) * theory
    67   type binding = binding * attribute list
    68   val empty_binding: binding
    69   val rule_attribute: (Context.generic -> thm -> thm) -> attribute
    70   val declaration_attribute: (thm -> Context.generic -> Context.generic) -> attribute
    71   val theory_attributes: attribute list -> theory * thm -> theory * thm
    72   val proof_attributes: attribute list -> Proof.context * thm -> Proof.context * thm
    73   val no_attributes: 'a -> 'a * 'b list
    74   val simple_fact: 'a -> ('a * 'b list) list
    75   val tag_rule: Properties.property -> thm -> thm
    76   val untag_rule: string -> thm -> thm
    77   val tag: Properties.property -> attribute
    78   val untag: string -> attribute
    79   val def_name: string -> string
    80   val def_name_optional: string -> string -> string
    81   val def_binding: Binding.binding -> Binding.binding
    82   val def_binding_optional: Binding.binding -> Binding.binding -> Binding.binding
    83   val has_name_hint: thm -> bool
    84   val get_name_hint: thm -> string
    85   val put_name_hint: string -> thm -> thm
    86   val definitionK: string
    87   val theoremK: string
    88   val lemmaK: string
    89   val corollaryK: string
    90   val get_kind: thm -> string
    91   val kind_rule: string -> thm -> thm
    92   val kind: string -> attribute
    93 end;
    94 
    95 structure Thm: THM =
    96 struct
    97 
    98 (** basic operations **)
    99 
   100 (* collecting cterms *)
   101 
   102 val op aconvc = op aconv o pairself Thm.term_of;
   103 
   104 fun add_cterm_frees ct =
   105   let
   106     val cert = Thm.cterm_of (Thm.theory_of_cterm ct);
   107     val t = Thm.term_of ct;
   108   in Term.fold_aterms (fn v as Free _ => insert (op aconvc) (cert v) | _ => I) t end;
   109 
   110 
   111 (* cterm constructors and destructors *)
   112 
   113 fun all_name (x, t) A =
   114   let
   115     val cert = Thm.cterm_of (Thm.theory_of_cterm t);
   116     val T = #T (Thm.rep_cterm t);
   117   in Thm.capply (cert (Const ("all", (T --> propT) --> propT))) (Thm.cabs_name (x, t) A) end;
   118 
   119 fun all t A = all_name ("", t) A;
   120 
   121 fun mk_binop c a b = Thm.capply (Thm.capply c a) b;
   122 fun dest_binop ct = (Thm.dest_arg1 ct, Thm.dest_arg ct);
   123 
   124 fun dest_implies ct =
   125   (case Thm.term_of ct of
   126     Const ("==>", _) $ _ $ _ => dest_binop ct
   127   | _ => raise TERM ("dest_implies", [Thm.term_of ct]));
   128 
   129 fun dest_equals ct =
   130   (case Thm.term_of ct of
   131     Const ("==", _) $ _ $ _ => dest_binop ct
   132   | _ => raise TERM ("dest_equals", [Thm.term_of ct]));
   133 
   134 fun dest_equals_lhs ct =
   135   (case Thm.term_of ct of
   136     Const ("==", _) $ _ $ _ => Thm.dest_arg1 ct
   137   | _ => raise TERM ("dest_equals_lhs", [Thm.term_of ct]));
   138 
   139 fun dest_equals_rhs ct =
   140   (case Thm.term_of ct of
   141     Const ("==", _) $ _ $ _ => Thm.dest_arg ct
   142   | _ => raise TERM ("dest_equals_rhs", [Thm.term_of ct]));
   143 
   144 val lhs_of = dest_equals_lhs o Thm.cprop_of;
   145 val rhs_of = dest_equals_rhs o Thm.cprop_of;
   146 
   147 
   148 (* thm order: ignores theory context! *)
   149 
   150 fun thm_ord (th1, th2) =
   151   let
   152     val {shyps = shyps1, hyps = hyps1, tpairs = tpairs1, prop = prop1, ...} = Thm.rep_thm th1;
   153     val {shyps = shyps2, hyps = hyps2, tpairs = tpairs2, prop = prop2, ...} = Thm.rep_thm th2;
   154   in
   155     (case Term_Ord.fast_term_ord (prop1, prop2) of
   156       EQUAL =>
   157         (case list_ord (prod_ord Term_Ord.fast_term_ord Term_Ord.fast_term_ord) (tpairs1, tpairs2) of
   158           EQUAL =>
   159             (case list_ord Term_Ord.fast_term_ord (hyps1, hyps2) of
   160               EQUAL => list_ord Term_Ord.sort_ord (shyps1, shyps2)
   161             | ord => ord)
   162         | ord => ord)
   163     | ord => ord)
   164   end;
   165 
   166 
   167 (* tables and caches *)
   168 
   169 structure Ctermtab = Table(type key = cterm val ord = Term_Ord.fast_term_ord o pairself Thm.term_of);
   170 structure Thmtab = Table(type key = thm val ord = thm_ord);
   171 
   172 fun cterm_cache f = Cache.create Ctermtab.empty Ctermtab.lookup Ctermtab.update f;
   173 fun thm_cache f = Cache.create Thmtab.empty Thmtab.lookup Thmtab.update f;
   174 
   175 
   176 (* equality *)
   177 
   178 fun is_reflexive th = op aconv (Logic.dest_equals (Thm.prop_of th))
   179   handle TERM _ => false;
   180 
   181 fun eq_thm ths =
   182   Context.joinable (pairself Thm.theory_of_thm ths) andalso
   183   is_equal (thm_ord ths);
   184 
   185 val eq_thms = eq_list eq_thm;
   186 
   187 val eq_thm_thy = Theory.eq_thy o pairself Thm.theory_of_thm;
   188 val eq_thm_prop = op aconv o pairself Thm.full_prop_of;
   189 
   190 
   191 (* pattern equivalence *)
   192 
   193 fun equiv_thm ths =
   194   Pattern.equiv (Theory.merge (pairself Thm.theory_of_thm ths)) (pairself Thm.full_prop_of ths);
   195 
   196 
   197 (* type classes and sorts *)
   198 
   199 fun class_triv thy c =
   200   Thm.of_class (Thm.ctyp_of thy (TVar ((Name.aT, 0), [c])), c);
   201 
   202 fun of_sort (T, S) = map (fn c => Thm.of_class (T, c)) S;
   203 
   204 fun check_shyps sorts raw_th =
   205   let
   206     val th = Thm.strip_shyps raw_th;
   207     val prt_sort = Syntax.pretty_sort_global (Thm.theory_of_thm th);
   208     val pending = Sorts.subtract sorts (Thm.extra_shyps th);
   209   in
   210     if null pending then th
   211     else error (Pretty.string_of (Pretty.block (Pretty.str "Pending sort hypotheses:" ::
   212       Pretty.brk 1 :: Pretty.commas (map prt_sort pending))))
   213   end;
   214 
   215 
   216 (* misc operations *)
   217 
   218 fun is_dummy thm =
   219   (case try Logic.dest_term (Thm.concl_of thm) of
   220     NONE => false
   221   | SOME t => Term.is_dummy_pattern t);
   222 
   223 fun plain_prop_of raw_thm =
   224   let
   225     val thm = Thm.strip_shyps raw_thm;
   226     fun err msg = raise THM ("plain_prop_of: " ^ msg, 0, [thm]);
   227     val {hyps, prop, tpairs, ...} = Thm.rep_thm thm;
   228   in
   229     if not (null hyps) then
   230       err "theorem may not contain hypotheses"
   231     else if not (null (Thm.extra_shyps thm)) then
   232       err "theorem may not contain sort hypotheses"
   233     else if not (null tpairs) then
   234       err "theorem may not contain flex-flex pairs"
   235     else prop
   236   end;
   237 
   238 
   239 (* collections of theorems in canonical order *)
   240 
   241 val add_thm = update eq_thm_prop;
   242 val del_thm = remove eq_thm_prop;
   243 val merge_thms = merge eq_thm_prop;
   244 
   245 val full_rules = Item_Net.init eq_thm_prop (single o Thm.full_prop_of);
   246 val intro_rules = Item_Net.init eq_thm_prop (single o Thm.concl_of);
   247 val elim_rules = Item_Net.init eq_thm_prop (single o Thm.major_prem_of);
   248 
   249 
   250 
   251 (** basic derived rules **)
   252 
   253 (*Elimination of implication
   254   A    A ==> B
   255   ------------
   256         B
   257 *)
   258 fun elim_implies thA thAB = Thm.implies_elim thAB thA;
   259 
   260 
   261 (* forall_elim_var(s) *)
   262 
   263 local
   264 
   265 fun forall_elim_vars_aux strip_vars i th =
   266   let
   267     val thy = Thm.theory_of_thm th;
   268     val {tpairs, prop, ...} = Thm.rep_thm th;
   269     val add_used = Term.fold_aterms
   270       (fn Var ((x, j), _) => if i = j then insert (op =) x else I | _ => I);
   271     val used = fold (fn (t, u) => add_used t o add_used u) tpairs (add_used prop []);
   272     val vars = strip_vars prop;
   273     val cvars = (Name.variant_list used (map #1 vars), vars)
   274       |> ListPair.map (fn (x, (_, T)) => Thm.cterm_of thy (Var ((x, i), T)));
   275   in fold Thm.forall_elim cvars th end;
   276 
   277 in
   278 
   279 val forall_elim_vars = forall_elim_vars_aux Term.strip_all_vars;
   280 
   281 fun forall_elim_var i th =
   282   forall_elim_vars_aux
   283     (fn Const ("all", _) $ Abs (a, T, _) => [(a, T)]
   284       | _ => raise THM ("forall_elim_vars", i, [th])) i th;
   285 
   286 end;
   287 
   288 
   289 (* certify_instantiate *)
   290 
   291 fun certify_inst thy (instT, inst) =
   292   (map (fn (v, T) => (Thm.ctyp_of thy (TVar v), Thm.ctyp_of thy T)) instT,
   293     map (fn (v, t) => (Thm.cterm_of thy (Var v), Thm.cterm_of thy t)) inst);
   294 
   295 fun certify_instantiate insts th =
   296   Thm.instantiate (certify_inst (Thm.theory_of_thm th) insts) th;
   297 
   298 
   299 (* forall_intr_frees: generalization over all suitable Free variables *)
   300 
   301 fun forall_intr_frees th =
   302   let
   303     val thy = Thm.theory_of_thm th;
   304     val {prop, hyps, tpairs, ...} = Thm.rep_thm th;
   305     val fixed = fold Term.add_frees (Thm.terms_of_tpairs tpairs @ hyps) [];
   306     val frees = Term.fold_aterms (fn Free v =>
   307       if member (op =) fixed v then I else insert (op =) v | _ => I) prop [];
   308   in fold (Thm.forall_intr o Thm.cterm_of thy o Free) frees th end;
   309 
   310 
   311 (* unvarify_global: global schematic variables *)
   312 
   313 fun unvarify_global th =
   314   let
   315     val prop = Thm.full_prop_of th;
   316     val _ = map Logic.unvarify_global (prop :: Thm.hyps_of th)
   317       handle TERM (msg, _) => raise THM (msg, 0, [th]);
   318 
   319     val instT = rev (Term.add_tvars prop []) |> map (fn v as ((a, _), S) => (v, TFree (a, S)));
   320     val inst = rev (Term.add_vars prop []) |> map (fn ((a, i), T) =>
   321       let val T' = Term_Subst.instantiateT instT T
   322       in (((a, i), T'), Free ((a, T'))) end);
   323   in certify_instantiate (instT, inst) th end;
   324 
   325 
   326 (* close_derivation *)
   327 
   328 fun close_derivation thm =
   329   if Thm.derivation_name thm = "" then Thm.name_derivation "" thm
   330   else thm;
   331 
   332 
   333 
   334 (** specification primitives **)
   335 
   336 (* rules *)
   337 
   338 fun stripped_sorts thy t =
   339   let
   340     val tfrees = rev (map TFree (Term.add_tfrees t []));
   341     val tfrees' = map (fn a => TFree (a, [])) (Name.invents Name.context Name.aT (length tfrees));
   342     val strip = tfrees ~~ tfrees';
   343     val recover = map (pairself (Thm.ctyp_of thy o Logic.varifyT_global) o swap) strip;
   344     val t' = Term.map_types (Term.map_atyps (perhaps (AList.lookup (op =) strip))) t;
   345   in (strip, recover, t') end;
   346 
   347 fun add_axiom (b, prop) thy =
   348   let
   349     val b' = if Binding.is_empty b then Binding.name ("unnamed_axiom_" ^ serial_string ()) else b;
   350 
   351     val _ = Sign.no_vars (Syntax.pp_global thy) prop;
   352     val (strip, recover, prop') = stripped_sorts thy prop;
   353     val constraints = map (fn (TFree (_, S), T) => (T, S)) strip;
   354     val of_sorts = maps (fn (T as TFree (_, S), _) => of_sort (Thm.ctyp_of thy T, S)) strip;
   355 
   356     val thy' =
   357       Theory.add_axiom (b', Logic.list_implies (maps Logic.mk_of_sort constraints, prop')) thy;
   358     val axm_name = Sign.full_name thy' b';
   359     val axm' = Thm.axiom thy' axm_name;
   360     val thm =
   361       Thm.instantiate (recover, []) axm'
   362       |> unvarify_global
   363       |> fold elim_implies of_sorts;
   364   in ((axm_name, thm), thy') end;
   365 
   366 fun add_def unchecked overloaded (b, prop) thy =
   367   let
   368     val _ = Sign.no_vars (Syntax.pp_global thy) prop;
   369     val prems = map (Thm.cterm_of thy) (Logic.strip_imp_prems prop);
   370     val (_, recover, concl') = stripped_sorts thy (Logic.strip_imp_concl prop);
   371 
   372     val thy' = Theory.add_def unchecked overloaded (b, concl') thy;
   373     val axm_name = Sign.full_name thy' b;
   374     val axm' = Thm.axiom thy' axm_name;
   375     val thm =
   376       Thm.instantiate (recover, []) axm'
   377       |> unvarify_global
   378       |> fold_rev Thm.implies_intr prems;
   379   in ((axm_name, thm), thy') end;
   380 
   381 
   382 
   383 (** attributes **)
   384 
   385 type binding = binding * attribute list;
   386 val empty_binding: binding = (Binding.empty, []);
   387 
   388 fun rule_attribute f (x, th) = (x, f x th);
   389 fun declaration_attribute f (x, th) = (f th x, th);
   390 
   391 fun apply_attributes mk dest =
   392   let
   393     fun app [] = I
   394       | app ((f: attribute) :: fs) = fn (x, th) => f (mk x, th) |>> dest |> app fs;
   395   in app end;
   396 
   397 val theory_attributes = apply_attributes Context.Theory Context.the_theory;
   398 val proof_attributes = apply_attributes Context.Proof Context.the_proof;
   399 
   400 fun no_attributes x = (x, []);
   401 fun simple_fact x = [(x, [])];
   402 
   403 
   404 
   405 (*** theorem tags ***)
   406 
   407 (* add / delete tags *)
   408 
   409 fun tag_rule tg = Thm.map_tags (insert (op =) tg);
   410 fun untag_rule s = Thm.map_tags (filter_out (fn (s', _) => s = s'));
   411 
   412 fun tag tg x = rule_attribute (K (tag_rule tg)) x;
   413 fun untag s x = rule_attribute (K (untag_rule s)) x;
   414 
   415 
   416 (* def_name *)
   417 
   418 fun def_name c = c ^ "_def";
   419 
   420 fun def_name_optional c "" = def_name c
   421   | def_name_optional _ name = name;
   422 
   423 val def_binding = Binding.map_name def_name;
   424 
   425 fun def_binding_optional b name =
   426   if Binding.is_empty name then def_binding b else name;
   427 
   428 
   429 (* unofficial theorem names *)
   430 
   431 fun the_name_hint thm = the (AList.lookup (op =) (Thm.get_tags thm) Markup.nameN);
   432 
   433 val has_name_hint = can the_name_hint;
   434 val get_name_hint = the_default "??.unknown" o try the_name_hint;
   435 
   436 fun put_name_hint name = untag_rule Markup.nameN #> tag_rule (Markup.nameN, name);
   437 
   438 
   439 (* theorem kinds *)
   440 
   441 val definitionK = "definition";
   442 val theoremK = "theorem";
   443 val lemmaK = "lemma";
   444 val corollaryK = "corollary";
   445 
   446 fun get_kind thm = the_default "" (Properties.get (Thm.get_tags thm) Markup.kindN);
   447 
   448 fun kind_rule k = tag_rule (Markup.kindN, k) o untag_rule Markup.kindN;
   449 fun kind k x = if k = "" then x else rule_attribute (K (kind_rule k)) x;
   450 
   451 
   452 open Thm;
   453 
   454 end;
   455 
   456 structure Basic_Thm: BASIC_THM = Thm;
   457 open Basic_Thm;
   458