src/Pure/more_thm.ML
author wenzelm
Wed Jan 21 22:26:49 2009 +0100 (2009-01-21)
changeset 29605 f2924219125e
parent 29579 cb520b766e00
child 30210 225fa48756b2
permissions -rw-r--r--
eliminated obsolete var morphism;
     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 THM =
    10 sig
    11   include THM
    12   val aconvc: cterm * cterm -> bool
    13   val add_cterm_frees: cterm -> cterm list -> cterm list
    14   val mk_binop: cterm -> cterm -> cterm -> cterm
    15   val dest_binop: cterm -> cterm * cterm
    16   val dest_implies: cterm -> cterm * cterm
    17   val dest_equals: cterm -> cterm * cterm
    18   val dest_equals_lhs: cterm -> cterm
    19   val dest_equals_rhs: cterm -> cterm
    20   val lhs_of: thm -> cterm
    21   val rhs_of: thm -> cterm
    22   val thm_ord: thm * thm -> order
    23   val is_reflexive: thm -> bool
    24   val eq_thm: thm * thm -> bool
    25   val eq_thms: thm list * thm list -> bool
    26   val eq_thm_thy: thm * thm -> bool
    27   val eq_thm_prop: thm * thm -> bool
    28   val equiv_thm: thm * thm -> bool
    29   val check_shyps: sort list -> thm -> thm
    30   val is_dummy: thm -> bool
    31   val plain_prop_of: thm -> term
    32   val fold_terms: (term -> 'a -> 'a) -> thm -> 'a -> 'a
    33   val add_thm: thm -> thm list -> thm list
    34   val del_thm: thm -> thm list -> thm list
    35   val merge_thms: thm list * thm list -> thm list
    36   val elim_implies: thm -> thm -> thm
    37   val forall_elim_var: int -> thm -> thm
    38   val forall_elim_vars: int -> thm -> thm
    39   val unvarify: thm -> thm
    40   val close_derivation: thm -> thm
    41   val add_axiom: binding * term -> theory -> thm * theory
    42   val add_def: bool -> bool -> binding * term -> theory -> thm * theory
    43   val rule_attribute: (Context.generic -> thm -> thm) -> attribute
    44   val declaration_attribute: (thm -> Context.generic -> Context.generic) -> attribute
    45   val theory_attributes: attribute list -> theory * thm -> theory * thm
    46   val proof_attributes: attribute list -> Proof.context * thm -> Proof.context * thm
    47   val no_attributes: 'a -> 'a * 'b list
    48   val simple_fact: 'a -> ('a * 'b list) list
    49   val tag_rule: Properties.property -> thm -> thm
    50   val untag_rule: string -> thm -> thm
    51   val tag: Properties.property -> attribute
    52   val untag: string -> attribute
    53   val position_of: thm -> Position.T
    54   val default_position: Position.T -> thm -> thm
    55   val default_position_of: thm -> thm -> thm
    56   val has_name_hint: thm -> bool
    57   val get_name_hint: thm -> string
    58   val put_name_hint: string -> thm -> thm
    59   val get_group: thm -> string option
    60   val put_group: string -> thm -> thm
    61   val group: string -> attribute
    62   val axiomK: string
    63   val assumptionK: string
    64   val definitionK: string
    65   val theoremK: string
    66   val lemmaK: string
    67   val corollaryK: string
    68   val internalK: string
    69   val has_kind: thm -> bool
    70   val get_kind: thm -> string
    71   val kind_rule: string -> thm -> thm
    72   val kind: string -> attribute
    73   val kind_internal: attribute
    74   val has_internal: Properties.property list -> bool
    75   val is_internal: thm -> bool
    76 end;
    77 
    78 structure Thm: THM =
    79 struct
    80 
    81 (** basic operations **)
    82 
    83 (* collecting cterms *)
    84 
    85 val op aconvc = op aconv o pairself Thm.term_of;
    86 
    87 fun add_cterm_frees ct =
    88   let
    89     val cert = Thm.cterm_of (Thm.theory_of_cterm ct);
    90     val t = Thm.term_of ct;
    91   in Term.fold_aterms (fn v as Free _ => insert (op aconvc) (cert v) | _ => I) t end;
    92 
    93 
    94 (* cterm constructors and destructors *)
    95 
    96 fun mk_binop c a b = Thm.capply (Thm.capply c a) b;
    97 fun dest_binop ct = (Thm.dest_arg1 ct, Thm.dest_arg ct);
    98 
    99 fun dest_implies ct =
   100   (case Thm.term_of ct of
   101     Const ("==>", _) $ _ $ _ => dest_binop ct
   102   | _ => raise TERM ("dest_implies", [Thm.term_of ct]));
   103 
   104 fun dest_equals ct =
   105   (case Thm.term_of ct of
   106     Const ("==", _) $ _ $ _ => dest_binop ct
   107   | _ => raise TERM ("dest_equals", [Thm.term_of ct]));
   108 
   109 fun dest_equals_lhs ct =
   110   (case Thm.term_of ct of
   111     Const ("==", _) $ _ $ _ => Thm.dest_arg1 ct
   112   | _ => raise TERM ("dest_equals_lhs", [Thm.term_of ct]));
   113 
   114 fun dest_equals_rhs ct =
   115   (case Thm.term_of ct of
   116     Const ("==", _) $ _ $ _ => Thm.dest_arg ct
   117   | _ => raise TERM ("dest_equals_rhs", [Thm.term_of ct]));
   118 
   119 val lhs_of = dest_equals_lhs o Thm.cprop_of;
   120 val rhs_of = dest_equals_rhs o Thm.cprop_of;
   121 
   122 
   123 (* thm order: ignores theory context! *)
   124 
   125 fun thm_ord (th1, th2) =
   126   let
   127     val {shyps = shyps1, hyps = hyps1, tpairs = tpairs1, prop = prop1, ...} = Thm.rep_thm th1;
   128     val {shyps = shyps2, hyps = hyps2, tpairs = tpairs2, prop = prop2, ...} = Thm.rep_thm th2;
   129   in
   130     (case TermOrd.fast_term_ord (prop1, prop2) of
   131       EQUAL =>
   132         (case list_ord (prod_ord TermOrd.fast_term_ord TermOrd.fast_term_ord) (tpairs1, tpairs2) of
   133           EQUAL =>
   134             (case list_ord TermOrd.fast_term_ord (hyps1, hyps2) of
   135               EQUAL => list_ord TermOrd.sort_ord (shyps1, shyps2)
   136             | ord => ord)
   137         | ord => ord)
   138     | ord => ord)
   139   end;
   140 
   141 
   142 (* equality *)
   143 
   144 fun is_reflexive th = op aconv (Logic.dest_equals (Thm.prop_of th))
   145   handle TERM _ => false;
   146 
   147 fun eq_thm ths =
   148   Context.joinable (pairself Thm.theory_of_thm ths) andalso
   149   is_equal (thm_ord ths);
   150 
   151 val eq_thms = eq_list eq_thm;
   152 
   153 val eq_thm_thy = Theory.eq_thy o pairself Thm.theory_of_thm;
   154 val eq_thm_prop = op aconv o pairself Thm.full_prop_of;
   155 
   156 
   157 (* pattern equivalence *)
   158 
   159 fun equiv_thm ths =
   160   Pattern.equiv (Theory.merge (pairself Thm.theory_of_thm ths)) (pairself Thm.full_prop_of ths);
   161 
   162 
   163 (* sort hypotheses *)
   164 
   165 fun check_shyps sorts raw_th =
   166   let
   167     val th = Thm.strip_shyps raw_th;
   168     val prt_sort = Syntax.pretty_sort_global (Thm.theory_of_thm th);
   169     val pending = Sorts.subtract sorts (Thm.extra_shyps th);
   170   in
   171     if null pending then th
   172     else error (Pretty.string_of (Pretty.block (Pretty.str "Pending sort hypotheses:" ::
   173       Pretty.brk 1 :: Pretty.commas (map prt_sort pending))))
   174   end;
   175 
   176 
   177 (* misc operations *)
   178 
   179 fun is_dummy thm =
   180   (case try Logic.dest_term (Thm.concl_of thm) of
   181     NONE => false
   182   | SOME t => Term.is_dummy_pattern t);
   183 
   184 fun plain_prop_of raw_thm =
   185   let
   186     val thm = Thm.strip_shyps raw_thm;
   187     fun err msg = raise THM ("plain_prop_of: " ^ msg, 0, [thm]);
   188     val {hyps, prop, tpairs, ...} = Thm.rep_thm thm;
   189   in
   190     if not (null hyps) then
   191       err "theorem may not contain hypotheses"
   192     else if not (null (Thm.extra_shyps thm)) then
   193       err "theorem may not contain sort hypotheses"
   194     else if not (null tpairs) then
   195       err "theorem may not contain flex-flex pairs"
   196     else prop
   197   end;
   198 
   199 fun fold_terms f th =
   200   let val {tpairs, prop, hyps, ...} = Thm.rep_thm th
   201   in fold (fn (t, u) => f t #> f u) tpairs #> f prop #> fold f hyps end;
   202 
   203 
   204 (* lists of theorems in canonical order *)
   205 
   206 val add_thm = update eq_thm_prop;
   207 val del_thm = remove eq_thm_prop;
   208 val merge_thms = merge eq_thm_prop;
   209 
   210 
   211 
   212 (** basic derived rules **)
   213 
   214 (*Elimination of implication
   215   A    A ==> B
   216   ------------
   217         B
   218 *)
   219 fun elim_implies thA thAB = Thm.implies_elim thAB thA;
   220 
   221 
   222 (* forall_elim_var(s) *)
   223 
   224 local
   225 
   226 fun forall_elim_vars_aux strip_vars i th =
   227   let
   228     val thy = Thm.theory_of_thm th;
   229     val {tpairs, prop, ...} = Thm.rep_thm th;
   230     val add_used = Term.fold_aterms
   231       (fn Var ((x, j), _) => if i = j then insert (op =) x else I | _ => I);
   232     val used = fold (fn (t, u) => add_used t o add_used u) tpairs (add_used prop []);
   233     val vars = strip_vars prop;
   234     val cvars = (Name.variant_list used (map #1 vars), vars)
   235       |> ListPair.map (fn (x, (_, T)) => Thm.cterm_of thy (Var ((x, i), T)));
   236   in fold Thm.forall_elim cvars th end;
   237 
   238 in
   239 
   240 val forall_elim_vars = forall_elim_vars_aux Term.strip_all_vars;
   241 
   242 fun forall_elim_var i th = forall_elim_vars_aux
   243   (fn Const ("all", _) $ Abs (a, T, _) => [(a, T)]
   244   | _ => raise THM ("forall_elim_vars", i, [th])) i th;
   245 
   246 end;
   247 
   248 
   249 (* unvarify: global schematic variables *)
   250 
   251 fun unvarify th =
   252   let
   253     val thy = Thm.theory_of_thm th;
   254     val cert = Thm.cterm_of thy;
   255     val certT = Thm.ctyp_of thy;
   256 
   257     val prop = Thm.full_prop_of th;
   258     val _ = map Logic.unvarify (prop :: Thm.hyps_of th)
   259       handle TERM (msg, _) => raise THM (msg, 0, [th]);
   260 
   261     val instT0 = rev (Term.add_tvars prop []) |> map (fn v as ((a, _), S) => (v, TFree (a, S)));
   262     val instT = map (fn (v, T) => (certT (TVar v), certT T)) instT0;
   263     val inst = rev (Term.add_vars prop []) |> map (fn ((a, i), T) =>
   264       let val T' = TermSubst.instantiateT instT0 T
   265       in (cert (Var ((a, i), T')), cert (Free ((a, T')))) end);
   266   in Thm.instantiate (instT, inst) th end;
   267 
   268 
   269 (* close_derivation *)
   270 
   271 fun close_derivation thm =
   272   if Thm.get_name thm = "" then Thm.put_name "" thm
   273   else thm;
   274 
   275 
   276 
   277 (** specification primitives **)
   278 
   279 fun add_axiom (b, prop) thy =
   280   let
   281     val b' = if Binding.is_empty b
   282       then Binding.name ("axiom_" ^ serial_string ()) else b;
   283     val thy' = thy |> Theory.add_axioms_i [(b', prop)];
   284     val axm = unvarify (Thm.axiom thy' (Sign.full_name thy' b'));
   285   in (axm, thy') end;
   286 
   287 fun add_def unchecked overloaded (b, prop) thy =
   288   let
   289     val tfrees = rev (map TFree (Term.add_tfrees prop []));
   290     val tfrees' = map (fn a => TFree (a, [])) (Name.invents Name.context Name.aT (length tfrees));
   291     val strip_sorts = tfrees ~~ tfrees';
   292     val recover_sorts = map (pairself (Thm.ctyp_of thy o Logic.varifyT)) (tfrees' ~~ tfrees);
   293 
   294     val prop' = Term.map_types (Term.map_atyps (perhaps (AList.lookup (op =) strip_sorts))) prop;
   295     val thy' = Theory.add_defs_i unchecked overloaded [(b, prop')] thy;
   296     val axm' = Thm.axiom thy' (Sign.full_name thy' b);
   297     val thm = unvarify (Thm.instantiate (recover_sorts, []) axm');
   298   in (thm, thy') end;
   299 
   300 
   301 
   302 (** attributes **)
   303 
   304 fun rule_attribute f (x, th) = (x, f x th);
   305 fun declaration_attribute f (x, th) = (f th x, th);
   306 
   307 fun apply_attributes mk dest =
   308   let
   309     fun app [] = I
   310       | app ((f: attribute) :: fs) = fn (x, th) => f (mk x, th) |>> dest |> app fs;
   311   in app end;
   312 
   313 val theory_attributes = apply_attributes Context.Theory Context.the_theory;
   314 val proof_attributes = apply_attributes Context.Proof Context.the_proof;
   315 
   316 fun no_attributes x = (x, []);
   317 fun simple_fact x = [(x, [])];
   318 
   319 
   320 
   321 (*** theorem tags ***)
   322 
   323 (* add / delete tags *)
   324 
   325 fun tag_rule tg = Thm.map_tags (insert (op =) tg);
   326 fun untag_rule s = Thm.map_tags (filter_out (fn (s', _) => s = s'));
   327 
   328 fun tag tg x = rule_attribute (K (tag_rule tg)) x;
   329 fun untag s x = rule_attribute (K (untag_rule s)) x;
   330 
   331 
   332 (* position *)
   333 
   334 val position_of = Position.of_properties o Thm.get_tags;
   335 
   336 fun default_position pos = Thm.map_tags (Position.default_properties pos);
   337 val default_position_of = default_position o position_of;
   338 
   339 
   340 (* unofficial theorem names *)
   341 
   342 fun the_name_hint thm = the (AList.lookup (op =) (Thm.get_tags thm) Markup.nameN);
   343 
   344 val has_name_hint = can the_name_hint;
   345 val get_name_hint = the_default "??.unknown" o try the_name_hint;
   346 
   347 fun put_name_hint name = untag_rule Markup.nameN #> tag_rule (Markup.nameN, name);
   348 
   349 
   350 (* theorem groups *)
   351 
   352 fun get_group thm = Properties.get (Thm.get_tags thm) Markup.groupN;
   353 
   354 fun put_group name = if name = "" then I else Thm.map_tags (Properties.put (Markup.groupN, name));
   355 
   356 fun group name = rule_attribute (K (put_group name));
   357 
   358 
   359 (* theorem kinds *)
   360 
   361 val axiomK = "axiom";
   362 val assumptionK = "assumption";
   363 val definitionK = "definition";
   364 val theoremK = "theorem";
   365 val lemmaK = "lemma";
   366 val corollaryK = "corollary";
   367 val internalK = Markup.internalK;
   368 
   369 fun the_kind thm = the (Properties.get (Thm.get_tags thm) Markup.kindN);
   370 
   371 val has_kind = can the_kind;
   372 val get_kind = the_default "" o try the_kind;
   373 
   374 fun kind_rule k = tag_rule (Markup.kindN, k) o untag_rule Markup.kindN;
   375 fun kind k x = if k = "" then x else rule_attribute (K (kind_rule k)) x;
   376 fun kind_internal x = kind internalK x;
   377 fun has_internal tags = exists (fn tg => tg = (Markup.kindN, internalK)) tags;
   378 val is_internal = has_internal o Thm.get_tags;
   379 
   380 
   381 open Thm;
   382 
   383 end;
   384 
   385 val op aconvc = Thm.aconvc;
   386 
   387 structure Thmtab = TableFun(type key = thm val ord = Thm.thm_ord);