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