src/Pure/Isar/theory_target.ML
author wenzelm
Thu Dec 14 15:31:22 2006 +0100 (2006-12-14)
changeset 21845 da545169fe06
parent 21825 6f6bf23f4c1e
child 22203 efc0cdc01307
permissions -rw-r--r--
defs/notes: more robust transitivity reasoning;
wenzelm@20894
     1
(*  Title:      Pure/Isar/theory_target.ML
wenzelm@20894
     2
    ID:         $Id$
wenzelm@20894
     3
    Author:     Makarius
wenzelm@20894
     4
wenzelm@21594
     5
Common theory/locale targets.
wenzelm@20894
     6
*)
wenzelm@20894
     7
wenzelm@20894
     8
signature THEORY_TARGET =
wenzelm@20894
     9
sig
wenzelm@21006
    10
  val peek: local_theory -> string option
wenzelm@21804
    11
  val fork_mixfix: bool -> bool -> mixfix -> mixfix * mixfix
wenzelm@21804
    12
  val begin: bool -> bstring -> Proof.context -> local_theory
wenzelm@20894
    13
  val init: xstring option -> theory -> local_theory
wenzelm@20894
    14
  val init_i: string option -> theory -> local_theory
wenzelm@20894
    15
end;
wenzelm@20894
    16
wenzelm@20894
    17
structure TheoryTarget: THEORY_TARGET =
wenzelm@20894
    18
struct
wenzelm@20894
    19
wenzelm@20894
    20
(** locale targets **)
wenzelm@20894
    21
wenzelm@21006
    22
(* context data *)
wenzelm@21006
    23
wenzelm@21006
    24
structure Data = ProofDataFun
wenzelm@21006
    25
(
wenzelm@21006
    26
  val name = "Isar/theory_target";
wenzelm@21006
    27
  type T = string option;
wenzelm@21006
    28
  fun init _ = NONE;
wenzelm@21006
    29
  fun print _ _ = ();
wenzelm@21006
    30
);
wenzelm@21006
    31
wenzelm@21006
    32
val _ = Context.add_setup Data.init;
wenzelm@21006
    33
val peek = Data.get;
wenzelm@21006
    34
wenzelm@21006
    35
wenzelm@20894
    36
(* pretty *)
wenzelm@20894
    37
wenzelm@20894
    38
fun pretty loc ctxt =
wenzelm@20894
    39
  let
wenzelm@20894
    40
    val thy = ProofContext.theory_of ctxt;
wenzelm@20894
    41
    val fixes = map (fn (x, T) => (x, SOME T, NoSyn)) (#1 (ProofContext.inferred_fixes ctxt));
wenzelm@21585
    42
    val assumes = map (fn A => (("", []), [(A, [])])) (map Thm.term_of (Assumption.assms_of ctxt));
wenzelm@20894
    43
    val elems =
wenzelm@20894
    44
      (if null fixes then [] else [Element.Fixes fixes]) @
wenzelm@20894
    45
      (if null assumes then [] else [Element.Assumes assumes]);
wenzelm@20894
    46
  in
wenzelm@20984
    47
    if loc = "" then
wenzelm@20984
    48
      [Pretty.block
wenzelm@20984
    49
        [Pretty.str "theory", Pretty.brk 1, Pretty.str (Context.theory_name thy),
wenzelm@20984
    50
          Pretty.str " =", Pretty.brk 1, ThyInfo.pretty_theory thy]]
wenzelm@20984
    51
    else if null elems then [Pretty.str ("locale " ^ Locale.extern thy loc)]
wenzelm@20984
    52
    else
wenzelm@20984
    53
      [Pretty.big_list ("locale " ^ Locale.extern thy loc ^ " =")
wenzelm@20984
    54
        (map (Pretty.chunks o Element.pretty_ctxt ctxt) elems)]
wenzelm@20894
    55
  end;
wenzelm@20894
    56
wenzelm@20894
    57
wenzelm@21804
    58
(* consts *)
wenzelm@21804
    59
wenzelm@21804
    60
fun fork_mixfix is_class is_loc mx =
wenzelm@21804
    61
  let
wenzelm@21804
    62
    val mx' = Syntax.unlocalize_mixfix mx;
wenzelm@21804
    63
    val mx1 = if is_class orelse (is_loc andalso mx = mx') then NoSyn else mx';
wenzelm@21804
    64
    val mx2 = if is_loc then mx else NoSyn;
wenzelm@21804
    65
  in (mx1, mx2) end;
wenzelm@21804
    66
wenzelm@21825
    67
fun internal_abbrev prmode ((c, mx), t) =
wenzelm@21825
    68
  LocalTheory.term_syntax (ProofContext.target_abbrev prmode ((c, mx), t)) #>
wenzelm@21825
    69
  ProofContext.add_abbrev Syntax.internalM (c, t) #> snd;
wenzelm@21804
    70
wenzelm@21804
    71
fun consts is_class is_loc depends decls lthy =
wenzelm@21804
    72
  let
wenzelm@21804
    73
    val xs = filter depends (#1 (ProofContext.inferred_fixes (LocalTheory.target_of lthy)));
wenzelm@21747
    74
wenzelm@21804
    75
    fun const ((c, T), mx) thy =
wenzelm@21804
    76
      let
wenzelm@21804
    77
        val U = map #2 xs ---> T;
wenzelm@21804
    78
        val t = Term.list_comb (Const (Sign.full_name thy c, U), map Free xs);
wenzelm@21804
    79
        val (mx1, mx2) = fork_mixfix is_class is_loc mx;
wenzelm@21804
    80
        val thy' = Sign.add_consts_authentic [(c, U, mx1)] thy;
wenzelm@21804
    81
      in (((c, mx2), t), thy') end;
wenzelm@21804
    82
wenzelm@21804
    83
    val (abbrs, lthy') = lthy |> LocalTheory.theory_result (fold_map const decls);
wenzelm@21804
    84
    val defs = map (apsnd (pair ("", []))) abbrs;
wenzelm@21804
    85
  in
wenzelm@21804
    86
    lthy'
wenzelm@21825
    87
    |> is_loc ? fold (internal_abbrev Syntax.default_mode) abbrs
wenzelm@21804
    88
    |> LocalDefs.add_defs defs |>> map (apsnd snd)
wenzelm@21804
    89
  end;
wenzelm@21804
    90
wenzelm@21804
    91
wenzelm@21804
    92
(* abbrev *)
wenzelm@21747
    93
wenzelm@21747
    94
fun occ_params ctxt ts =
wenzelm@21747
    95
  #1 (ProofContext.inferred_fixes ctxt)
wenzelm@21747
    96
  |> filter (member (op =) (fold (Variable.add_fixed ctxt) ts []));
wenzelm@21747
    97
wenzelm@21804
    98
fun abbrev is_class is_loc prmode ((raw_c, mx), raw_t) lthy =
wenzelm@21747
    99
  let
wenzelm@21747
   100
    val thy_ctxt = ProofContext.init (ProofContext.theory_of lthy);
wenzelm@21747
   101
    val target = LocalTheory.target_of lthy;
wenzelm@21747
   102
    val target_morphism = LocalTheory.target_morphism lthy;
wenzelm@21747
   103
wenzelm@21747
   104
    val c = Morphism.name target_morphism raw_c;
wenzelm@21747
   105
    val t = Morphism.term target_morphism raw_t;
wenzelm@21747
   106
    val xs = map Free (occ_params target [t]);
wenzelm@21747
   107
    val u = fold_rev Term.lambda xs t;
wenzelm@21804
   108
    val U = Term.fastype_of u;
wenzelm@21747
   109
    val u' = singleton (Variable.export_terms (Variable.declare_term u target) thy_ctxt) u;
wenzelm@21747
   110
wenzelm@21804
   111
    val ((lhs as Const (full_c, _), rhs), lthy1) = lthy
wenzelm@21747
   112
      |> LocalTheory.theory_result (Sign.add_abbrev (#1 prmode) (c, u'));
wenzelm@21804
   113
    val (mx1, mx2) = fork_mixfix is_class is_loc mx;
wenzelm@21747
   114
  in
wenzelm@21747
   115
    lthy1
wenzelm@21804
   116
    |> LocalTheory.theory (Sign.add_notation prmode [(lhs, mx1)])
wenzelm@21825
   117
    |> is_loc ? internal_abbrev prmode ((c, mx2), Term.list_comb (Const (full_c, U), xs))
wenzelm@21804
   118
    |> ProofContext.local_abbrev (c, rhs)
wenzelm@21747
   119
  end;
wenzelm@21747
   120
wenzelm@21747
   121
wenzelm@20894
   122
(* defs *)
wenzelm@20894
   123
wenzelm@21845
   124
wenzelm@21594
   125
wenzelm@21845
   126
local
wenzelm@21570
   127
wenzelm@21594
   128
fun expand_term ctxt t =
wenzelm@21570
   129
  let
wenzelm@21570
   130
    val thy = ProofContext.theory_of ctxt;
wenzelm@21570
   131
    val thy_ctxt = ProofContext.init thy;
wenzelm@21570
   132
    val ct = Thm.cterm_of thy t;
wenzelm@21594
   133
    val (defs, ct') = LocalDefs.export ctxt thy_ctxt (Drule.mk_term ct) ||> Drule.dest_term;
wenzelm@21708
   134
  in (Thm.term_of ct', MetaSimplifier.rewrite true defs ct) end;
wenzelm@21570
   135
wenzelm@20894
   136
fun add_def (name, prop) =
wenzelm@20894
   137
  Theory.add_defs_i false false [(name, prop)] #>
wenzelm@20894
   138
  (fn thy => (Drule.unvarify (Thm.get_axiom_i thy (Sign.full_name thy name)), thy));
wenzelm@20894
   139
wenzelm@20894
   140
in
wenzelm@20894
   141
wenzelm@21570
   142
fun defs kind args lthy0 =
wenzelm@20894
   143
  let
wenzelm@21533
   144
    fun def ((c, mx), ((name, atts), rhs)) lthy1 =
wenzelm@20894
   145
      let
wenzelm@21594
   146
        val (rhs', rhs_conv) = expand_term lthy0 rhs;
wenzelm@21570
   147
        val xs = Variable.add_fixed (LocalTheory.target_of lthy0) rhs' [];
wenzelm@20894
   148
wenzelm@21570
   149
        val ([(lhs, lhs_def)], lthy2) = lthy1
wenzelm@21570
   150
          |> LocalTheory.consts (member (op =) xs) [((c, Term.fastype_of rhs), mx)];
wenzelm@21570
   151
        val lhs' = #2 (Logic.dest_equals (Thm.prop_of lhs_def));
wenzelm@21533
   152
wenzelm@21570
   153
        val name' = Thm.def_name_optional c name;
wenzelm@21570
   154
        val (def, lthy3) = lthy2
wenzelm@20894
   155
          |> LocalTheory.theory_result (add_def (name', Logic.mk_equals (lhs', rhs')));
wenzelm@20894
   156
wenzelm@21845
   157
        val eq = LocalDefs.trans_terms lthy3
wenzelm@21845
   158
          [(*c == loc.c xs*) lhs_def,
wenzelm@21845
   159
           (*lhs' == rhs'*)  def,
wenzelm@21845
   160
           (*rhs' == rhs*)   Thm.symmetric rhs_conv];
wenzelm@20894
   161
      in ((lhs, ((name', atts), [([eq], [])])), lthy3) end;
wenzelm@20894
   162
wenzelm@21570
   163
    val ((lhss, facts), lthy') = lthy0 |> fold_map def args |>> split_list;
wenzelm@21433
   164
    val (res, lthy'') = lthy' |> LocalTheory.notes kind facts;
wenzelm@20894
   165
  in (lhss ~~ map (apsnd the_single) res, lthy'') end;
wenzelm@20894
   166
wenzelm@20894
   167
end;
wenzelm@20894
   168
wenzelm@20894
   169
wenzelm@21611
   170
(* axioms *)
wenzelm@21611
   171
wenzelm@21611
   172
local
wenzelm@21611
   173
wenzelm@21611
   174
fun add_axiom hyps (name, prop) thy =
wenzelm@21611
   175
  let
wenzelm@21611
   176
    val name' = if name = "" then "axiom_" ^ serial_string () else name;
wenzelm@21611
   177
    val prop' = Logic.list_implies (hyps, prop);
wenzelm@21611
   178
    val thy' = thy |> Theory.add_axioms_i [(name', prop')];
wenzelm@21611
   179
    val axm = Drule.unvarify (Thm.get_axiom_i thy' (Sign.full_name thy' name'));
wenzelm@21611
   180
    val prems = map (Thm.assume o Thm.cterm_of thy') hyps;
wenzelm@21611
   181
  in (Drule.implies_elim_list axm prems, thy') end;
wenzelm@21611
   182
wenzelm@21611
   183
in
wenzelm@21611
   184
wenzelm@21611
   185
fun axioms kind specs lthy =
wenzelm@21611
   186
  let
wenzelm@21611
   187
    val hyps = map Thm.term_of (Assumption.assms_of lthy);
wenzelm@21611
   188
    fun axiom ((name, atts), props) thy = thy
wenzelm@21611
   189
      |> fold_map (add_axiom hyps) (PureThy.name_multi name props)
wenzelm@21611
   190
      |-> (fn ths => pair ((name, atts), [(ths, [])]));
wenzelm@21611
   191
  in
wenzelm@21611
   192
    lthy
wenzelm@21611
   193
    |> fold (fold Variable.declare_term o snd) specs
wenzelm@21611
   194
    |> LocalTheory.theory_result (fold_map axiom specs)
wenzelm@21611
   195
    |-> LocalTheory.notes kind
wenzelm@21611
   196
  end;
wenzelm@21611
   197
wenzelm@21611
   198
end;
wenzelm@21611
   199
wenzelm@21611
   200
wenzelm@20894
   201
(* notes *)
wenzelm@20894
   202
wenzelm@21611
   203
fun import_export_proof ctxt (name, raw_th) =
wenzelm@21594
   204
  let
wenzelm@21611
   205
    val thy = ProofContext.theory_of ctxt;
wenzelm@21611
   206
    val thy_ctxt = ProofContext.init thy;
wenzelm@21611
   207
    val certT = Thm.ctyp_of thy;
wenzelm@21611
   208
    val cert = Thm.cterm_of thy;
wenzelm@21594
   209
wenzelm@21611
   210
    (*export assumes/defines*)
wenzelm@21611
   211
    val th = Goal.norm_result raw_th;
wenzelm@21611
   212
    val (defs, th') = LocalDefs.export ctxt thy_ctxt th;
wenzelm@21708
   213
    val concl_conv = MetaSimplifier.rewrite true defs (Thm.cprop_of th);
wenzelm@21708
   214
    val assms = map (MetaSimplifier.rewrite_rule defs o Thm.assume) (Assumption.assms_of ctxt);
wenzelm@21611
   215
    val nprems = Thm.nprems_of th' - Thm.nprems_of th;
wenzelm@21611
   216
wenzelm@21611
   217
    (*export fixes*)
wenzelm@21611
   218
    val tfrees = map TFree (Drule.fold_terms Term.add_tfrees th' []);
wenzelm@21611
   219
    val frees = map Free (Drule.fold_terms Term.add_frees th' []);
wenzelm@21611
   220
    val (th'' :: vs) = (th' :: map (Drule.mk_term o cert) (map Logic.mk_type tfrees @ frees))
wenzelm@21611
   221
      |> Variable.export ctxt thy_ctxt
wenzelm@21611
   222
      |> Drule.zero_var_indexes_list;
wenzelm@21611
   223
wenzelm@21611
   224
    (*thm definition*)
wenzelm@21644
   225
    val result = th''
wenzelm@21644
   226
      |> PureThy.name_thm true true ""
wenzelm@21644
   227
      |> Goal.close_result
wenzelm@21644
   228
      |> PureThy.name_thm true true name;
wenzelm@21611
   229
wenzelm@21611
   230
    (*import fixes*)
wenzelm@21611
   231
    val (tvars, vars) =
wenzelm@21611
   232
      chop (length tfrees) (map (Thm.term_of o Drule.dest_term) vs)
wenzelm@21611
   233
      |>> map Logic.dest_type;
wenzelm@21611
   234
wenzelm@21611
   235
    val instT = map_filter (fn (TVar v, T) => SOME (v, T) | _ => NONE) (tvars ~~ tfrees);
wenzelm@21611
   236
    val inst = filter (is_Var o fst) (vars ~~ frees);
wenzelm@21611
   237
    val cinstT = map (pairself certT o apfst TVar) instT;
wenzelm@21611
   238
    val cinst = map (pairself (cert o Term.map_types (TermSubst.instantiateT instT))) inst;
wenzelm@21611
   239
    val result' = Thm.instantiate (cinstT, cinst) result;
wenzelm@21611
   240
wenzelm@21611
   241
    (*import assumes/defines*)
wenzelm@21594
   242
    val assm_tac = FIRST' (map (fn assm => Tactic.compose_tac (false, assm, 0)) assms);
wenzelm@21611
   243
    val result'' =
wenzelm@21611
   244
      (case SINGLE (Seq.INTERVAL assm_tac 1 nprems) result' of
wenzelm@21611
   245
        NONE => raise THM ("Failed to re-import result", 0, [result'])
wenzelm@21845
   246
      | SOME res => LocalDefs.trans_props ctxt [res, Thm.symmetric concl_conv])
wenzelm@21644
   247
      |> Goal.norm_result
wenzelm@21644
   248
      |> PureThy.name_thm false false name;
wenzelm@21611
   249
wenzelm@21611
   250
  in (result'', result) end;
wenzelm@21611
   251
wenzelm@21594
   252
fun notes loc kind facts lthy =
wenzelm@21585
   253
  let
wenzelm@21594
   254
    val is_loc = loc <> "";
wenzelm@21594
   255
    val thy = ProofContext.theory_of lthy;
wenzelm@21615
   256
    val full = LocalTheory.full_name lthy;
wenzelm@21585
   257
wenzelm@21594
   258
    val facts' = facts
wenzelm@21615
   259
      |> map (fn (a, bs) => (a, PureThy.burrow_fact (PureThy.name_multi (full (fst a))) bs))
wenzelm@21615
   260
      |> PureThy.map_facts (import_export_proof lthy);
wenzelm@21611
   261
    val local_facts = PureThy.map_facts #1 facts'
wenzelm@21611
   262
      |> Attrib.map_facts (Attrib.attribute_i thy);
wenzelm@21611
   263
    val target_facts = PureThy.map_facts #1 facts'
wenzelm@21611
   264
      |> is_loc ? Element.facts_map (Element.morph_ctxt (LocalTheory.target_morphism lthy));
wenzelm@21611
   265
    val global_facts = PureThy.map_facts #2 facts'
wenzelm@21594
   266
      |> Attrib.map_facts (if is_loc then K I else Attrib.attribute_i thy);
wenzelm@21594
   267
  in
wenzelm@21611
   268
    lthy |> LocalTheory.theory
wenzelm@21611
   269
      (Sign.qualified_names
wenzelm@21611
   270
        #> PureThy.note_thmss_i kind global_facts #> snd
wenzelm@21611
   271
        #> Sign.restore_naming thy)
wenzelm@21585
   272
wenzelm@21611
   273
    |> is_loc ? LocalTheory.target (Locale.add_thmss loc kind target_facts)
wenzelm@21594
   274
wenzelm@21433
   275
    |> ProofContext.set_stmt true
wenzelm@20894
   276
    |> ProofContext.qualified_names
wenzelm@21611
   277
    |> ProofContext.note_thmss_i kind local_facts
wenzelm@20894
   278
    ||> ProofContext.restore_naming lthy
wenzelm@21433
   279
    ||> ProofContext.restore_stmt lthy
wenzelm@20894
   280
  end;
wenzelm@20894
   281
wenzelm@20894
   282
wenzelm@21533
   283
(* target declarations *)
wenzelm@20894
   284
wenzelm@21611
   285
fun target_decl _ "" f =
wenzelm@21533
   286
      LocalTheory.theory (Context.theory_map (f Morphism.identity)) #>
wenzelm@21533
   287
      LocalTheory.target (Context.proof_map (f Morphism.identity))
wenzelm@21668
   288
  | target_decl add loc f = LocalTheory.target (add loc f);
wenzelm@20894
   289
wenzelm@21611
   290
val type_syntax = target_decl Locale.add_type_syntax;
wenzelm@21611
   291
val term_syntax = target_decl Locale.add_term_syntax;
wenzelm@21611
   292
val declaration = target_decl Locale.add_declaration;
wenzelm@20894
   293
wenzelm@21533
   294
fun target_morphism loc lthy =
wenzelm@21594
   295
  ProofContext.export_morphism lthy (LocalTheory.target_of lthy) $>
wenzelm@21613
   296
  Morphism.thm_morphism Goal.norm_result;
wenzelm@21533
   297
wenzelm@21668
   298
fun target_name loc lthy =
wenzelm@21668
   299
  (if loc = "" then Sign.naming_of (ProofContext.theory_of lthy)
wenzelm@21668
   300
   else ProofContext.naming_of (LocalTheory.target_of lthy))
wenzelm@21668
   301
  |> NameSpace.qualified_names
wenzelm@21668
   302
  |> NameSpace.full;
wenzelm@21533
   303
wenzelm@20894
   304
wenzelm@20894
   305
(* init and exit *)
wenzelm@20894
   306
wenzelm@21804
   307
fun begin is_class loc =
wenzelm@21747
   308
  let val is_loc = loc <> "" in
wenzelm@21747
   309
    Data.put (if is_loc then SOME loc else NONE) #>
wenzelm@21747
   310
    LocalTheory.init (NameSpace.base loc)
wenzelm@21747
   311
     {pretty = pretty loc,
wenzelm@21804
   312
      consts = consts is_class is_loc,
wenzelm@21747
   313
      axioms = axioms,
wenzelm@21804
   314
      abbrev = abbrev is_class is_loc,
wenzelm@21747
   315
      defs = defs,
wenzelm@21747
   316
      notes = notes loc,
wenzelm@21747
   317
      type_syntax = type_syntax loc,
wenzelm@21747
   318
      term_syntax = term_syntax loc,
wenzelm@21747
   319
      declaration = declaration loc,
wenzelm@21747
   320
      target_morphism = target_morphism loc,
wenzelm@21747
   321
      target_name = target_name loc,
wenzelm@21804
   322
      reinit = fn _ =>
wenzelm@21804
   323
        begin is_class loc o (if is_loc then Locale.init loc else ProofContext.init),
wenzelm@21747
   324
      exit = LocalTheory.target_of}
wenzelm@21747
   325
  end;
wenzelm@20894
   326
wenzelm@21804
   327
fun init_i NONE thy = begin false "" (ProofContext.init thy)
wenzelm@21804
   328
  | init_i (SOME loc) thy =
wenzelm@21804
   329
      begin (can (Sign.certify_class thy) loc)  (* FIXME approximation *)
wenzelm@21804
   330
        loc (Locale.init loc thy);
wenzelm@20894
   331
wenzelm@21276
   332
fun init (SOME "-") thy = init_i NONE thy
wenzelm@21276
   333
  | init loc thy = init_i (Option.map (Locale.intern thy) loc) thy;
wenzelm@20894
   334
wenzelm@20894
   335
end;